943,934 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1354
  • C++ RSS
Jul 18th, 2007
0

Why does my code not take my input?

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

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Click image for larger version

Name:	cpp.JPG
Views:	36
Size:	14.5 KB
ID:	3751  
Attached Files
File Type: zip FileIO.zip (129.0 KB, 14 views)
Similar Threads
Moderator
Featured Poster
Reputation Points: 1784
Solved Threads: 574
Moderator
jbennet is offline Offline
16,520 posts
since Apr 2005
Jul 18th, 2007
1

Re: Why does my code not take my input?

Quote ...
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.
C++ Syntax (Toggle Plain Text)
  1. cin >> choice;
  2. cin.ignore( 80, '\n' );
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Jul 18th, 2007
0

Re: Why does my code not take my input?

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 18th, 2007
0

Re: Why does my code not take my input?

It seems to be working now using
C++ Syntax (Toggle Plain Text)
  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++)
Moderator
Featured Poster
Reputation Points: 1784
Solved Threads: 574
Moderator
jbennet is offline Offline
16,520 posts
since Apr 2005
Jul 18th, 2007
2

Re: Why does my code not take my input?

C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 18th, 2007
0

Re: Why does my code not take my input?

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)
Moderator
Featured Poster
Reputation Points: 1784
Solved Threads: 574
Moderator
jbennet is offline Offline
16,520 posts
since Apr 2005
Jul 23rd, 2007
0

Re: Why does my code not take my input?

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
Moderator
Featured Poster
Reputation Points: 1784
Solved Threads: 574
Moderator
jbennet is offline Offline
16,520 posts
since Apr 2005
Jul 24th, 2007
0

Re: Why does my code not take my input?

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

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: C++ Vs Java
Next Thread in C++ Forum Timeline: help me





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


Follow us on Twitter


© 2011 DaniWeb® LLC