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:
#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?