954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Quick Question

ok well i started out with making my own source code then compiling and running with Dev-C++ and when i run it, its working fine until the last line, here let me past my code

// just a test
#include <iostream>
#include <string>

using namespace std;

int main ()
{
    string mystr;
    cout << "Hello, whats your name? ";
    getline (cin, mystr);
    cout << "Hello " << mystr << ".\n";
    cout << "What grade are you in" << mystr << "? ";
    getline (cin, mystr);
    cout << "Thats cool, I used to be in " << mystr << "too.\n";
    cout << "Well i'm going to go now, goodbye.";
    return 0;
}


i know its stupid, i just started and wanted to do something, but ok when i get to the part "what grade are you in << mystr << "? "; and when i enter my grade or w/e the box just closes, is there any way to fix this, would i have to put something like Press "q" to quit or something like that? Any help would be nice, thankyou

The Leprechaun
Newbie Poster
1 post since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

try putting cin.get(); before your return statement.

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

run the in a command prompt then ull be able to see all the output before it quits. or try placing system("PAUSE") before the return statement, but i think thats bad practise to do that

easy
Newbie Poster
21 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 
run the in a command prompt then ull be able to see all the output before it quits. or try placing system("PAUSE") before the return statement, but i think thats bad practise to do that


Correct .
Its bad practice to use system calls to achieve such a trivial function.
Better use getchar() if using C and cin.get() when using C++.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
Correct . Its bad practice to use system calls to achieve such a trivial function. Better use getchar() if using C and cin.get() when using C++.

is this because the code isnt portable to other systems?

easy
Newbie Poster
21 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Yes that and many other things to be considered like the kind of overhead introduced when you call system functions for trivial tasks.

A very good tutorial by my friend and forum member Mr. WaltP can be found here which introduces some of the things which should be avoided.
http://www.gidnetwork.com/b-61.html

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

getline() throws away the \n null character at the end of a string and therefore makes a mess when trying to fluch the buffer. Using cin.get() makes everything much more comfortable

may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 13
Solved Threads: 2
 
getline() throws away the \n null character at the end of a string and therefore makes a mess when trying to fluch the buffer.


More surprises...

'\n' is a newline character, not null character ( '\0' )

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Whoops! yep, i meant getline()'s terminating new line (\n) is read and thrown away. In get() this new line character is left in the input buffer

may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 13
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You