Hello World Issues. I R humbled N00b.

Greetings, friends.

Here's my issue. I've been having trouble getting the Hello World to launch and stay open. The console seems to close and I'm encountering unexpected errors that didn't exist in my working code, and others that don't seem like they should exist in the existing code.

As a typical n00b, I'm biting off more than I can chew by trying to make users have to have some form of input to gather another message and go through it to reach the end. Example of how the final product would look/act:

Hey there, I'm Frank's computer, nice to meet you! I have an awesome secret to tell you, Press Enter to continue and see what it is!

User Input "Enter"

"I am going to explode in approximately ten minutes! I hope you do lots of cardio because you will need to run fast!"

I've just done the stupid thing however, and edited code that was already somewhat working, and now I cannot get it working again the way I want it (With the command not closing)

Here's what my broken mess of code looks like, be gentle, I'm already hurting.


#include <iostream>
#include <string>
#include <limits>

using namespace std ;

;int main ()
{

string firstname = "";
cout << "Enter your first name and hit Enter:";
cin >> firstname;
cout << endl;
cout << "Hello, " << firstname << ", it is nice to meet you, I am Frank's CPU. I have something that I think is really neat. It's a secret! Hit Enter to find out! " << endl
cout << "Goodbye!";
cout << "Press ENTER to continue..."
cin.ignore( numeric_limits <streamsize> ::max(), '\n');

}


In regards to the last section, when I try to compile and run with that, it says that I need ";" before the cases of cout and cin.ignore, turning the bottom section of code into:

;cout << "Goodbye!";
cout << "Press ENTER to continue..."
;cin.ignore( numeric_limits <streamsize> ::max(), '\n');

}

After I do that, however, I'm getting the error where the console closes on it's own, which I thought that the cin.ignore was supposed to thwart.

Anyways, thanks for any comments and help with this. Feel free to shoot me off an e-mail or send me a message with info for Skype or what have you if you're interested in helping out a little more personally and hands-on.

Thanks again, I look forward to your input.
closed account (N36fSL3A)
You're making this way to hard on yourself :P.

First off, if you're gonna include iostream, there is no need for including the string header, as it's included for you.

Just use cout to display your message,

then at the last line, do

cin.ignore('\n'); // Don't take my word for it, I don't use cin functions a lot.

and lastly don't forget to return a integer value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string> // this is required
#include <limits>

using namespace std ;

int main ()
{

    string firstname ;
    cout << "Enter your first name and hit Enter:";
    cin >> firstname;
    cout << '\n' ;
    cout << "Hello, " << firstname << ", it is nice to meet you, I am Frank's CPU.\n"
         << "I have something that I think is really neat.\n"
         << "It's a secret! Hit Enter to find out!\n"
         << "Goodbye!\n"
         << "Press ENTER to continue..." ;
    cin.ignore( numeric_limits <streamsize> ::max(), '\n');
    std::cin.get() ; // wait for the user to press enter
}
Fredbill30 wrote:
First off, if you're gonna include iostream, there is no need for including the string header, as it's included for you.


That is not guaranteed.

If you're using std::string... then #include <string>.
closed account (N36fSL3A)
It isn't? I thought all compilers included std::string in the iostream header.

Well take Disch's advice and not mine for the iostream-string thing.
I get this meow, however what about using the namespace?

Also, just for my own knowledge, at the end for:

std::cin get() ;

If I'm using the std namespace then why does it matter if I have std there? I tried running it without and it wasn't working at all.

What is the process that's happening there?

I'm a much better learner if I know the going-ons and the how-it-duzzits than with just the knowledge (So far, FYI, you guys have been more helpful with clarification than previous research. Thanks for that! :) )
> std::cin get() ;
> If I'm using the std namespace then why does it matter if I have std there?

With using namespace std; in effect, it does not matter.
We can write either std::cin.get() ; or just cin.get() ;


> What is the process that's happening there?

std::cin.get() ; // wait for the user to press enter
Read the comment. for additional information, look it up.
http://www.cplusplus.com/reference/istream/istream/get/
Topic archived. No new replies allowed.