Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 16th, 2006, 1:40 AM   #1
d_heyzie
Newbie
 
Join Date: Jan 2006
Posts: 22
Rep Power: 0 d_heyzie is on a distinguished road
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;
    }
The problem is that when i input the first name and age it prompts again for name and age but it seems to have automattically put in a blank name so it only really asks for age input.
 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;
    }
In this snippet i have replaced "cin.getline(member[i].name, 99);" with
"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.
d_heyzie is offline   Reply With Quote
Old Apr 16th, 2006, 1:53 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,253
Rep Power: 5 grumpy will become famous soon enough
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).
grumpy is offline   Reply With Quote
Old Apr 16th, 2006, 9:31 AM   #3
d_heyzie
Newbie
 
Join Date: Jan 2006
Posts: 22
Rep Power: 0 d_heyzie is on a distinguished road
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.
d_heyzie is offline   Reply With Quote
Old Apr 16th, 2006, 9:47 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 16th, 2006, 10:01 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,253
Rep Power: 5 grumpy will become famous soon enough
Computer science definition of "parse" : To analyze or separate input into more easily processed components.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC