Starting out on my C++ foot

Started by
26 comments, last by Cyncro 17 years, 10 months ago
Quote:Original post by Cyncro
No it still won't work. I know I'm doing something wrong. Here's the new code.
#include <string><iostream>
using namespace std;

int main() {
string imBlank;
string heyMom("Where are my socks?");
string standardReply = "Beamed into deep "
"space on wide angle dispersion?";
string useThisOneAgain(standardReply);
cin.get ();
} ///:~


First thing's first. Whenever you paste code in gamedev, be sure to do it between [_source_] [_/source_] tags (without the underscores)

Second of all. it should be

#include <string>#include <iostream>


I've never seen them on the same line, it may be valid, but i've never seen it, so i'm going to assume its invalid.

the standard reply thing, I didn't think that you could have two strings like that either. on different lines. instead, put them between the same quotes with a \n like so
string standardReply = "Beamed into deep\n space on wide angle dispersion?"; 

assuming you wanted a new line there.

hope that helps
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
Advertisement
The standardReply string spaced over two lines works fine on g++ 4.x -- no idea what would happen on VC++, and I assume MinGW would be the same...

never seen a string declared that way, but it seems it works -- oh, and it outputs as a single line (no newline inbetween).


~Shiny, on a mac.
------------'C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, it blows away your whole leg.' -Bjarne Stroustrup
Interesting, I never knew that. I wonder if thats standards compliant or not, it seems strange since the c++ language really doesnt do a whole lot of catering to character arrays AS STRINGS, if you know what i mean. Regardless, if it works it works, but when you post your questions Cyncro, be sure to post the associated errors as well (copied from your compiler)

cheers
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
Thanks but I banished the e book and found a pretty great site that explains everything. but im having a problem keeping my file opened when i run it. even with the cin.get it wont look. please take a look.
// Operating with Variables#include <iostream>using namespace std;int main (){    //declaring variables    int a, b;    int result;        //process    a = 5;    b = 2;    a = a + 1;    result = a - b;        //print out the result    cout << result;        //terminate the program    return 0;    cin.get ();}
Wait forget it I just had the cin.get below the return..lol sorry silly mistake
Quote:Original post by Cyncro
Thanks but I banished the e book and found a pretty great site that explains everything. but im having a problem keeping my file opened when i run it. even with the cin.get it wont look. please take a look.
*** Source Snippet Removed ***
you need to put cin.get() before the return 0;. As it is, your program is exiting before cin.get() is called.

Edit: Awww man, too late! [smile]
F-R-E-D F-R-E-D-B-U-R...G-E-R! - Yes!
Quote:Original post by bschneid
EDIT: I looked at the first tutorial for C++ (Intro to C++, I think) and the fact that I saw the words preprocesser directive tells me that it moves way to fast and/or its got its priorities all wrong. There isn't a reason to try to explain what #include does before showing you how to make the console say hello world.


Explaining first what #include does accomplishes at least two very important things:

1) It gives the beginning programmer another opportunity to rethink starting in C++. :)
2) It makes it a lot less likely that the beginning programmer would come up with something like "#include <string><iostream>".

And Ademan, putting "string literals together " "like this" is definitely standards compliant. (And yes, the defined behaviour is that they are interpreted as a single joined string literal, with no inserted newline or anything.)
Ok, I'm learning, I'm atschool using the computer so I Can't do no C++ now but the website I fround is a lot more useful then the others. It's cplusplus.com

This topic is closed to new replies.

Advertisement