C++ beginner question with structure
May 24, 2012 at 8:14am UTC
Hello, noobie programmer here.
basically i just learnt about structure and tried using it in different occasions.
I don't know what to do, but what i tried to do was to use cin to receive data from the myself and cin would store the data in to structure. so that later i can use it. I hope i made sense here but here are the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// Using structure
#include <iostream>
#include <cstring >
#include <string>
using namespace std;
struct dataPerson
{
char name[20] ;
int age ;
string address ;
string proffession ;
} ;
int main ()
{
char newname [20];
int newage ;
string newaddress, newprofession ;
cout << " lets try to have you input a new persondata. " << endl ;
cout << " Please give a name, then age, then address,"
" and profession in this order " << endl;
cin.get(newname,20).get() ;
cin >> newage ;
getline(cin, newaddress) ;
getline (cin, newprofession) ;
dataPerson newperson =
{
newname,
newage,
newaddress,
newprofession,
};
cout << newperson.age << endl ; // should give out newage ?
return 0 ;
}
Please help me, I've been trying for an hour, and i don't want to move on until i figure out how to do this .
Thank you :)
Last edited on May 24, 2012 at 8:38am UTC
May 24, 2012 at 8:28am UTC
Replace ur last few lines:
1 2 3 4 5 6
dataPerson newperson;
newperson.address=newaddress;
newperson.age=newage;
newperson.name=newname;
newperson.proffession=newprofession;
cout << newperson.age << endl ;
May 24, 2012 at 8:37am UTC
Oops I noticed i made a mistake when writting
near at the end. it should be
cout << newperson.age<< endl;
not cout << newperson.newage << endl ;
(edited in post)
And thanks Anirudh sn I will use what you gave me to make this work.
Topic archived. No new replies allowed.