Member Avatar for bjaanes

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?

The size of the array has to be known at compile time. If it has to be decided at run-time please use dynamic arrays. When you used #Define the size was known at compile time hence it worked.

Member Avatar for bjaanes

Right, right.
Thanks!

But how do that for an array in the structure?

struct names
{
string namee;
int age;
} namess[antnames];

How to use dynamic memory on the namess array?

I have tried around for a bit, but seems to be rather stuck again.

like this,

#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";
        }
}
Member Avatar for bjaanes

I finally got i working =)

Very good! Thank you.

But why does it seem like cin.get() was one of the reasons it jumped over the first name entry? :S
(need to learn this stuff to ^^)

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:

#include <limits.h>

[...code...]
cin.ignore ( INT_MAX, '\n' );
cin.get();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.