Why does my code not take my input?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 16,266
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 544
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Why does my code not take my input?

 
0
  #1
Jul 18th, 2007
Okay I have made a c++ console application to take user input and append it to a text file and then read it back.

I am using the win32 version of Dev c++

  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main(){
  9.  
  10. int choice;
  11.  
  12. cout << "What do you want to do?\n";
  13. cout << "1) Write To File\n";
  14. cout << "2) Read From File\n";
  15. cout << "Enter a choice: ";
  16. cin >> choice;
  17.  
  18.  
  19. ofstream outputFile("file.txt", ios::app);
  20. ifstream inputFile;
  21.  
  22. char myString[50];
  23.  
  24. switch (choice){
  25.  
  26. case 1:
  27.  
  28. cout << "Enter a string: ";
  29. cin.get( myString, 50, '\n');
  30.  
  31. cout << "Writting string to file...\n";
  32.  
  33. outputFile << myString;
  34. outputFile.close();
  35.  
  36. inputFile.open("file.txt");
  37.  
  38. break;
  39.  
  40. case 2:
  41.  
  42. inputFile.open("file.txt");
  43.  
  44. cout << "Reading string from file...\n";
  45.  
  46. inputFile.getline(myString, 50, '\n');
  47.  
  48. cout << myString;
  49.  
  50. break;
  51.  
  52. default:
  53. cout << "Not a valid choice!";
  54. break;
  55.  
  56. }
  57.  
  58. system("pause");
  59.  
  60. return 0;
  61.  
  62.  
  63. }

The problem is that it when i type in my choice and hit enter, it skips past asking me for a string.

I remember hearing that you have to ask
  1. cin
to throw away the enter or something?

The second part works fine by the way, if i add something to the text file using notepad, then it is displayed

Ive included the .exe in a .zip as well as a screenshot
Attached Thumbnails
cpp.JPG  
Attached Files
File Type: zip FileIO.zip (129.0 KB, 0 views)
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Why does my code not take my input?

 
1
  #2
Jul 18th, 2007
I remember hearing that you have to ask cin to throw away the enter or something?
Yeah something like that. Try adding cin.ignore like this.
  1. cin >> choice;
  2. cin.ignore( 80, '\n' );
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Why does my code not take my input?

 
0
  #3
Jul 18th, 2007
Mixing input methods is always a disaster waiting to happen, especially when you mix those which attempt to parse the input with those which just read the input. The different ways in which white-space and newlines get treated will catch you out.

Read a whole line into a string (preferably a std::string) then extract information from the string in memory.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,266
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 544
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Why does my code not take my input?

 
0
  #4
Jul 18th, 2007
It seems to be working now using
  1.  
  2. cin.ignore( 80, '\n' );

thanks Hamrick.

Salem, what would be the proper way to do this then? (im kind of a n00b at c++)
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Why does my code not take my input?

 
2
  #5
Jul 18th, 2007
  1. #include <string>
  2. #include <sstream>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main ( ) {
  7. string input;
  8.  
  9. if ( getline(cin,input) ) {
  10. istringstream convert(input);
  11. int result;
  12. if ( convert >> result ) {
  13. cout << "Success = " << result << endl;
  14. } else {
  15. cout << "Not an integer" << endl;
  16. }
  17. } else {
  18. cout << "No input" << endl;
  19. }
  20.  
  21. return 0;
  22. }
It might look like a lot of code compared to your 1-liner, but if you were to make your input bomb-proof, you'd be looking at a similar amount of code.

If your program does a lot of interactive I/O, then you'd create a number of wrapper functions to take care of some of the repetitiveness.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,266
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 544
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Why does my code not take my input?

 
0
  #6
Jul 18th, 2007
sounds good, i forgot about error handling

ill try that later im going to try and modify it to search for strings (i hope to use a text file to hold information for a simple text based game im making)
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,266
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 544
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Why does my code not take my input?

 
0
  #7
Jul 23rd, 2007
Okay heresa question, using the code above, how could i get it to print each string on its own line, automatically enter them at the end and display them all

I know i use a loop but its the I/o im confused about
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Why does my code not take my input?

 
0
  #8
Jul 24th, 2007
One example

std::vector< std::string > words;

Then in place of the cout,
words.push_back( result );

At the end, a for loop to iterate over the vector to output all the words.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC