| | |
Why does my code not take my input?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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++
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 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
I am using the win32 version of Dev c++
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; int main(){ int choice; cout << "What do you want to do?\n"; cout << "1) Write To File\n"; cout << "2) Read From File\n"; cout << "Enter a choice: "; cin >> choice; ofstream outputFile("file.txt", ios::app); ifstream inputFile; char myString[50]; switch (choice){ case 1: cout << "Enter a string: "; cin.get( myString, 50, '\n'); cout << "Writting string to file...\n"; outputFile << myString; outputFile.close(); inputFile.open("file.txt"); break; case 2: inputFile.open("file.txt"); cout << "Reading string from file...\n"; inputFile.getline(myString, 50, '\n'); cout << myString; break; default: cout << "Not a valid choice!"; break; } system("pause"); return 0; }
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)
cin
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
If i am helpful, please give me reputation points.
•
•
•
•
I remember hearing that you have to ask cin to throw away the enter or something?
C++ Syntax (Toggle Plain Text)
cin >> choice; cin.ignore( 80, '\n' );
The truth does not change according to our ability to stomach it.
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.
Read a whole line into a string (preferably a std::string) then extract information from the string in memory.
It seems to be working now using
thanks Hamrick.
Salem, what would be the proper way to do this then? (im kind of a n00b at c++)
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
#include <string> #include <sstream> #include <iostream> using namespace std; int main ( ) { string input; if ( getline(cin,input) ) { istringstream convert(input); int result; if ( convert >> result ) { cout << "Success = " << result << endl; } else { cout << "Not an integer" << endl; } } else { cout << "No input" << endl; } return 0; }
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.
![]() |
Similar Threads
- How to do Input in Python? (Python)
- how to input joystick in c++ to this code? (Game Development)
- input from file into class (C++)
- Please anyone help tweak my code The code tags are around it now (Java)
- string input not working as expected (C++)
- Please anyone help tweak my code (Java)
- Console-input echo trouble (C++)
- Keyboard input or "stuck in a loop" (C++)
- Need Help With Error Checking User Input (C)
- PHP Form Input (PHP)
Other Threads in the C++ Forum
- Previous Thread: josephus problem...
- Next Thread: Is it possible to create a square root program without math.h
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets







