Jump to content
  • Advertisement
Sign in to follow this  
noobster12345

C/C++ examples

This topic is 5410 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey, i was just wondering how you would make an input$ program, in c++ of course. let me give you an example in a basic language so you get a better idea what i mean, ;demo-print IQ = Input$("What's your IQ ") If IQ >= 140 Then Print "Congrats you are a genius" EndIf WaitKey like that, i just want to see the code simalarities and difs, thanks noobster;)

Share this post


Link to post
Share on other sites
Advertisement

#include <iostream>
using namespace std;

int main()
{
cout << "What's your IQ? ";
int iq;
cin >> iq;

if(iq >= 140)
cout << "Congrats you are a genius" << endl;

return 0;
}

Share this post


Link to post
Share on other sites

#include <iostream>
#include <cstdlib>
using namespace std;

int main(int argc, char **argv)
{
int IQ;

cout << "What's your IQ?:";
cin >> IQ;

if(IQ >= 140)
cout << "Congrats, you are a genius" << endl;

return EXIT_SUCCESS;
}




EDIT: Damnit, you were quicker than me ontheheap :)

Share this post


Link to post
Share on other sites
Quote:
Original post by noobster12345
cool thanks,
look scomplicated but ive never even seen it before so when i learn it will probably be easier.
BTW, what does "cout" mean?
thanks
noobster


Console out?
It outputs a string to the console, I know that much. I'm sure you can guess what cin means... [wink]

Share this post


Link to post
Share on other sites
It outputs almost any built-in data type to the console (and I think you can tell it how to do out/in on custom datatypes, too).

Share this post


Link to post
Share on other sites
Quote:
Original post by noobster12345
cool thanks,
look scomplicated but ive never even seen it before so when i learn it will probably be easier.
BTW, what does "cout" mean?
thanks
noobster


cout is a variable, which is predefined by the Standard C++ Library.

cout is actually an object, whose "operator <<" (meaning a function which belongs to cout whose name is "<<") is used to output text.

so, in fact, you are doing something like cout.operator<<("Hello World").

This "operator overloading" (you can change for your own classes even the meaning of + - * / whatever) is one of the things that makes C++ powerful and hard to grasp at the beginning.

I think to show a cout << "whatever" program to a C++ newcomer isnt very helpful because of that issue. Perhaps better would be to code a "Hello World" class which relies on printf (yes, i mean that).

Thermo

Share this post


Link to post
Share on other sites
Quote:
Original post by noobster12345
hey,
i was just wondering how you would make an input$ program, in c++ of course. let me give you an example in a basic language so you get a better idea what i mean,
;demo-print
IQ = Input$("What's your IQ ")
If IQ >= 140 Then
Print "Congrats you are a genius"
EndIf
WaitKey

like that, i just want to see the code simalarities and difs,
thanks
noobster;)


Hmm, how about a line by line translation? I don't know what programming languange you have there, but I guess I can decipher it:

;demo-print
This is VB, no? I think their ";" means "comment", though I'm not sure. If I'm right, c++ equivalent:
// demo-print

IQ = Input$("What's your IQ")
OK, well, this line takes a few more in c++, but be not afraid. Commented C++ equivalent:
int IQ; // Declare a variable, called IQ, in which you will store the user's input.
cout << "What's your IQ?: "; // Output the string to prompt the user for his IQ
cin >> IQ; // Actually ask the user for his IQ


If IQ >= 140 Then
C++:
if( IQ >= 140 )
If there is only one line in the if statement, as there is here, there's no need to denote where the if starts and ends. Otherwise, you surround it in {}'s.

Print "Congrats, you are a genius!"
C++:
cout << "\nCongrats, you are a genius!\n";
The "\n" just means "start a new line".

WaitKey
C++:
system( "Pause" );

I think the hardest part about learning the language is the concept itself. Once you learn one, you can learn any rather easily.

Share this post


Link to post
Share on other sites
File input and output in C++ is accomplished by "streams". The idea is that every streamable object knows how to serialise itself into and out of a text stream. The console is just another "stream" object - a group of them actually, cin, cout, and cerr. The reason they use the << and >> operators is because of operator overloading - each object can reimplement their own operator overload for << and >>, but its still done with no dynamic method calling (all static calling, so its optimisable) because the object types are known at compile-time.

Share this post


Link to post
Share on other sites
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

This'll teach you to work in C++ - not just treating it as "the new C" but as a super-optimisable oop language. Its a little over-obsessed with conventions (ignore exception specifications, and remember that stringstreams are inefficient) but otherwise excelent and free.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!