C/C++ examples

Started by
13 comments, last by Miserable 19 years, 7 months ago
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;)
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;}
#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 :)
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
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]

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

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).
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
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.
.:<<-v0d[KA]->>:.
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.
-- Single player is masturbation.
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.
-- Single player is masturbation.

This topic is closed to new replies.

Advertisement