944,192 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2290
  • C++ RSS
Oct 19th, 2006
0

Quick Question

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. // just a test
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9. string mystr;
  10. cout << "Hello, whats your name? ";
  11. getline (cin, mystr);
  12. cout << "Hello " << mystr << ".\n";
  13. cout << "What grade are you in" << mystr << "? ";
  14. getline (cin, mystr);
  15. cout << "Thats cool, I used to be in " << mystr << "too.\n";
  16. cout << "Well i'm going to go now, goodbye.";
  17. return 0;
  18. }
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
Last edited by The Leprechaun; Oct 19th, 2006 at 1:43 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
The Leprechaun is offline Offline
1 posts
since Oct 2006
Oct 19th, 2006
0

Re: Quick Question

try putting cin.get(); before your return statement.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Oct 22nd, 2006
0

Re: Quick Question

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
Last edited by easy; Oct 22nd, 2006 at 1:11 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
easy is offline Offline
21 posts
since Oct 2006
Oct 22nd, 2006
0

Re: Quick Question

Click to Expand / Collapse  Quote originally posted by easy ...
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++.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Oct 22nd, 2006
0

Re: Quick Question

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
easy is offline Offline
21 posts
since Oct 2006
Oct 23rd, 2006
0

Re: Quick Question

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
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 1st, 2006
0

Re: Quick Question

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
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
may4life is offline Offline
57 posts
since Oct 2006
Nov 1st, 2006
0

Re: Quick Question

Click to Expand / Collapse  Quote originally posted by may4life ...
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' )
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 2nd, 2006
0

Re: Quick Question

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
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
may4life is offline Offline
57 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Pulling data from vectors
Next Thread in C++ Forum Timeline: Help with Assignment





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC