![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Posts: 22
Rep Power: 0
![]() |
Arrays in data structures
Hey again, im learning about data structures in c++ and all was fine until i attempted to make a program using arrays in data structures. Anyways it's a program that asks for input for names and ages and displays it when 5 names and ages have been input. i think my problem is in the for loop that asks for input. (MAX = 5):
for(i = 0; i < MAX; i++)
{
cout <<" Enter Name: ";
cin.getline(member[i].name, 99);
cout << endl;
cout <<" Enter Age: ";
cin >> member[i].age;
cout << endl << endl;
} for(i = 0; i < MAX; i++)
{
cout <<" Enter Name: ";
cin >> member[i].name;
cout << endl;
cout <<" Enter Age: ";
cin >> member[i].age;
cout << endl << endl;
}"cin >> member[i].name;" and it works fine except it only displays one field, becuase of cin i guess. I have tried to change member into a 2d array with no success (maybe i did it wrong) please help!! ty. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,253
Rep Power: 5
![]() |
The reason for what you see in the first snipper is that the cin>>member[i].age reads an integer, and stops when it encounters input that is not an integer .... i.e. the '\n' you have hit. On the next time through the loop, the cin.getline() call sees that newline, reads it, and assumes it has received the whole line.
In the second snippet, the cin >> .... operations read until they get the next whitespace. It will work fine unless the user enters a name that contains whitespace. You might also find it entertaining to see what happens with the second snippet if you enter a name with a space in it (eg "Joe Bloggs"). The better approach would be to use the getline() method and parse the string you receive for data (eg the name and age). |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jan 2006
Posts: 22
Rep Power: 0
![]() |
Sry not sure what you mean by parse (only learned some definitions for it 2 minutes ago)
for(i = 0; i < MAX; i++)
{
cout <<" Enter Name: ";
cin.getline(member[i].name, 99);
cout << endl;
cout <<" Enter Age: ";
cin.getline(member[i].age, 99);
atoi(member[i].age);
cout << endl << endl;
}But while i was offline i changed a few things by asking for input in the form of a character array and then changing it into an integer, and it works fine ty for your help. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Some input functions are also required to perform conversions. If a conversion is not possible, they are going to run off into the weeds and puke in their cuff. When you use such a function you should ask it immediately if it has barfed. Anything less is just foolish and schlocky, unless you hold absolute power over your users.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,253
Rep Power: 5
![]() |
Computer science definition of "parse" : To analyze or separate input into more easily processed components.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|