| | |
Simple getline problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 89
Reputation:
Solved Threads: 7
I have this problem. This program runs well except that it skip the value of the first item in list. I think it is because of the cin.ignore(). But if I don't use the cin.ignore(). The cin will only read the first input and none after that. Help?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; class list // list class { private: string *Data; int size; int num; public: list() // constructor { size = 1; num = 0; } void add(); // member function void display(); }; void list :: add() { const int SIZE_INPUT = 50; char choice; char temp_input[SIZE_INPUT] = ""; do { Data = new string[size]; cout << "\n Enter item\t: "; if(num>0) { cin.ignore(INT_MAX, '\n' ); } cin.getline(temp_input, sizeof(temp_input)); Data[num] = temp_input; num++; size++; cout<<"Continue to Enter Item (Y=Yes/N=No)\t:\t"; cin >> choice; } while(tolower(choice) != 'n'); } //This function only display the last item. But if I try to display item one by one, no problem except that I cant display the first item (because of the cin.ignore?). void list :: display() { int i; cout << num << endl; for (i=0; i<num; i++) { cout << Data[i] << endl; } } int main() { list testing; testing.add(); testing.display(); system("PAUSE"); }
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
Read http://www.augustcouncil.com/~tgibso....html#problems and perhaps you need to call cin.clear() to reset some of the flags
Last edited by jencas; Sep 2nd, 2008 at 4:51 am.
If you are forced to reinvent the wheel at least try to invent a better one!
Please use code tags - Please mark solved threads as solved
Please use code tags - Please mark solved threads as solved
![]() |
Similar Threads
- Problem with "getline(cin, name)" (C++)
- getline function error (C++)
- weird problem on visual studio (C++)
- Dynamic Array of Structures, Loop problem! (C++)
- Header File.... simple (C++)
- Searching problem (C++)
- File array question (C++)
- getline function acts extrangly (C++)
- string input not working as expected (C++)
Other Threads in the C++ Forum
- Previous Thread: I can't get this to work, send keystroke too notepad
- Next Thread: how to import a .net dll to c++ version 6
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






