Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,230 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,874 people online right now. Registration is fast and FREE... Join Now!




Reading into a structure array using a sentinel value read loop

 
Reply to this topicStart new topic

Reading into a structure array using a sentinel value read loop

destroyerbear
19 Mar, 2007 - 09:50 PM
Post #1

New D.I.C Head
*

Joined: 7 Feb, 2007
Posts: 2


My Contributions
Hello. Im having trouble with my structure array in c++. i have to read ,from a file, a name and an average into it and this is what i have so far:

CODE

struct Student
{
    string name;
    double average;
};




void fillStructArray(ifstream &infile, Student group[],int &count)   //count is 0
{
    int i = 0;
    infile >> group[i].name >> group[i].average;
    
    while (group[i].average > 0)
    {
        i++;
        group[i].name;
        group[i].average;
        
    }
    count = i;
}




it is supposed to read in until it hits a name and a negative average. then that line is not supposed to be counted. this is my first attempt at using structures and any help would be appreciated.

thanks to anyone who responds.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Reading Into A Structure Array Using A Sentinel Value Read Loop
20 Mar, 2007 - 07:27 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
I suggest you walk though the code line by line. There are some things that don't really make sence and since I don't know exactly what you are trying to do it is hard to tell what you should do.

The function starts by reading in group[0].name and group[0].average. Then it checks to see if group[0].average > 0, if not it increments i, and... well these next two lines must spit out errors as group[i].name and group[i].average are not functions... then it loops back up and checks to see if group[1].average > 0... which we can't see since we only know what group[0].average is.

I *think* you may have meant to do the following:
CODE

void fillStructArray(ifstream &infile, Student& group[], int &count)  
{
    int i = 0;
    string sName;
    int iAverage;
    while (infile)
   {
    infile >> sName >> iAverage;
    
    if (iAverage > 0)
    {
        group[i].name = sName;
        group[i].average = iAverage;
        count++;

    }
    i++;
    }
    return;
}
Of course I am shooting from the hip here and I don't even know if that will compile, but I think the logic may be more of what you are looking for. This will fill an array up with names and averages as long as the average is above 0.
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Reading Into A Structure Array Using A Sentinel Value Read Loop
20 Mar, 2007 - 10:34 PM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE
group[i].name = sName;

Since you are copying a string, you'll need to use the strcpy() function to copy all the characters to the destination string.

CODE
strcpy(group[i].name,sName);

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:16PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month