| | |
Struct problems?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2008
Posts: 6
Reputation:
Solved Threads: 0
Hi =)
I am very new to c++ and have started messing around with some lines of code, but I am totaly stuck on this one:
When the codes execute and hits this point:
for (n = 0; n<antnames; n++)
{
cout << "Enter name: ";
getline(cin,namess[n].namee);
cout << "Enter age for this person: ";
getline(cin,mystr);
stringstream(mystr) >> namess[n].age;
}
In the FIRST round in the loop it does not let me enter the first name.
In the second and so on it works just fine.
There was no problem when i used #DEFINE antnames instead of user input.
But this is not the desired effect for me =)
Can anyone help me out here?
I am very new to c++ and have started messing around with some lines of code, but I am totaly stuck on this one:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { int antnames; cout << "How many names do you want to put in the base? \n"; cin >> antnames; struct names { string namee; int age; } namess[antnames]; int n; string mystr; for (n = 0; n<antnames; n++) { cout << "Enter name: "; getline(cin,namess[n].namee); cout << "Enter age for this person: "; getline(cin,mystr); stringstream(mystr) >> namess[n].age; } int nn; for (nn = 0; nn<antnames; nn++) { cout << "Name: " << namess[nn].namee << "\n"; cout << "Age: " << namess[nn].age << "\n"; } cin.get(); }
When the codes execute and hits this point:
for (n = 0; n<antnames; n++)
{
cout << "Enter name: ";
getline(cin,namess[n].namee);
cout << "Enter age for this person: ";
getline(cin,mystr);
stringstream(mystr) >> namess[n].age;
}
In the FIRST round in the loop it does not let me enter the first name.
In the second and so on it works just fine.
There was no problem when i used #DEFINE antnames instead of user input.
But this is not the desired effect for me =)
Can anyone help me out here?
•
•
Join Date: Mar 2008
Posts: 41
Reputation:
Solved Threads: 7
like this,
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { int antnames; cout << "How many names do you want to put in the base : "; cin >> antnames; struct names { string namee; int age; }; names *details = new names[antnames]; string mystr; cin.get(); for (int n = 0; n<antnames; n++) { cout << "\nEnter name: "; getline(cin, details[n].namee); cout << "\nEnter age for this person: "; getline(cin,mystr); stringstream(mystr) >> details[n].age; } int nn; for (nn = 0; nn<antnames; nn++) { cout << "Name: " << details[nn].namee << "\n"; cout << "Age: " << details[nn].age << "\n"; } }
Last edited by RenjithVR; Jul 2nd, 2008 at 6:52 am.
That's because you're mixing cin << with cin.get();
cin << ... leaves a "\n" (end of line) character on the stream and when you then call cin.get(), that character is still on the stream, so you aren't able to type anything. (cin.get only takes 1 char)
To avoid this problem, use:
cin << ... leaves a "\n" (end of line) character on the stream and when you then call cin.get(), that character is still on the stream, so you aren't able to type anything. (cin.get only takes 1 char)
To avoid this problem, use:
cpp Syntax (Toggle Plain Text)
#include <limits.h> [...code...] cin.ignore ( INT_MAX, '\n' ); cin.get();
Last edited by niek_e; Jul 2nd, 2008 at 7:15 am.
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- copy constructor & assignment operator overload problems (C++)
- Link List Insert problems (C++)
- struct array and enum problems (C++)
- Problems with Linked Lists in C (C)
- please help with these error problems (C++)
- problems w/ program involving structures and arrays (C++)
Other Threads in the C++ Forum
- Previous Thread: while loop dont seem to work
- Next Thread: Fstream Help
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







