# Help with Data Structures for Functions

• (2 Pages)
• 1
• 2

## 24 Replies - 2103 Views - Last Post: 09 April 2012 - 11:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=273362&amp;s=e1b9e13a840970552358df02c56fa694&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 TommehTGOD

Reputation: 0
• Posts: 31
• Joined: 25-March 12

# Help with Data Structures for Functions

Posted 31 March 2012 - 08:35 PM

I'm getting errors in this, such as uninitialized local variables.. I clearly think I've initialized them but I may be wrong. Check it out!

```#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <stdlib.h>
using namespace std;

struct Student
{
int ID;
double points;
double hours;
double gpa;
};
//function prototypes go here//

void printfile(ofstream &OutputFile);
double findHours(double ch);
double findGPA(Student points, Student hours);
int main()
{
Student stud;
Student gpa, points, hours, ID;
//double gpa, points, hours;
//int ID;
double ch;

ofstream OutputFile;
OutputFile.open("Output.dat");

ifstream infile;
infile.open("Input.dat");

printfile(OutputFile);

OutputFile<<fixed<<showpoint<<setprecision(1)<<endl;

infile>>stud.ID;    //Reads in ID by itself
while (!infile.eof())
{
//initialize variables
ch=0;
stud.points=0;
stud.hours=0;

while(ch >=0)  //When flags or sentinel values are read, the loop terminates
{
Student stud;
stud.points = stud.points + findPoints(grade, ch);
stud.hours = stud.hours + ch;
stud.gpa = findGPA(points, hours);
}
OutputFile<<setw(15)<<stud.ID<<setw(18)<<stud.gpa<<endl;

}
return 0;

OutputFile.close();
infile.close();
}
//Function Definitions Go Here
void printfile(ofstream &OutputFile)
{
OutputFile<<setw(30)<<"Thomas Powe"<<endl;
OutputFile<<setw(29)<<"03-27-12"<<endl<<endl;
OutputFile<<setw(15)<<"ID"<<setw(18)<<"GPA"<<endl<<endl;
return;
}

{
Student stud;
Student points;
stud.points=0;
stud.points = stud.points + grade * ch;
return stud.points;
}

double findHours(double ch)
{
Student stud;
Student hours;
stud.hours=0;
stud.hours = ch +ch;
return stud.hours;
}
double findGPA(Student points, Student hours)
{
Student stud;
Student gpa;
stud.gpa= stud.points/stud.hours;
return stud.gpa;
}
```

These are the errors:
```c:\users\tommeh\desktop\program 5\program 5\main.cpp(58): warning C4101: 'gpa' : unreferenced local variable
c:\users\tommeh\desktop\program 5\program 5\main.cpp(85): warning C4101: 'points' : unreferenced local variable
c:\users\tommeh\desktop\program 5\program 5\main.cpp(94): warning C4101: 'hours' : unreferenced local variable
c:\users\tommeh\desktop\program 5\program 5\main.cpp(102): warning C4101: 'gpa' : unreferenced local variable
c:\users\tommeh\desktop\program 5\program 5\main.cpp(59): warning C4700: uninitialized local variable 'stud' used
c:\users\tommeh\desktop\program 5\program 5\main.cpp(61): warning C4700: uninitialized local variable 'hours' used
c:\users\tommeh\desktop\program 5\program 5\main.cpp(61): warning C4700: uninitialized local variable 'points' used
c:\users\tommeh\desktop\program 5\program 5\main.cpp(104): warning C4700: uninitialized local variable 'stud' used
```

Much help will be appreciated. Thanks!

Is This A Good Question/Topic? 0

## Replies To: Help with Data Structures for Functions

### #2 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Help with Data Structures for Functions

Posted 31 March 2012 - 08:53 PM

Those are all warnings, not errors (which doesn't mean you should ignore them).

Regarding the "unitialized" ones, I don't understand why your compiler is complaining about the ones marked as lines 59 and 61. The last one definitely is unitialized. It refers to line 99 of the code you posted. stud is a Student object that you declared locally on line 96 and although you seem to be assigning a value on line 98, the values points and hours that you use there are themselves unitialized.

The first 4 warnings are about unused local variables, and you should examine them carefully because you probably think that you are using them but you're actually (and probably unintentionally) using something else instead.

And, ps, on line 5, use the C++ header <cstdlib>. Use the C++ versions of the C headers whenever you need standard C functions in your C++ programs.

Edit: Now I see, the "line 59" warning refers to line 54, where you use stud.points on the RHS in the assignment to stud.points. But before that line, stud.points is unitialized, which means that after that assignment it may still end up with a garbage value. And "line 61" refers to line 56 where you use a variable called hours. That "hours" was declared on line 21 and never initialized.

All of these warnings spell out pretty clearly what they are complaining about -- you just have to read them carefully, and then carefully read the code they refer to and you should be able to figure them out.

This post has been edited by r.stiltskin: 31 March 2012 - 09:03 PM
Reason for edit:: correcting a line number

### #3 wizzard1200

Reputation: 1
• Posts: 12
• Joined: 10-October 11

## Re: Help with Data Structures for Functions

Posted 31 March 2012 - 09:18 PM

What compiler are you using? What IDE are you currently using? Most IDEs will allow you to configure what error messages display. r.stiltskin is right, you really don't need to be concerned about those unintialized variables.

### #4 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Help with Data Structures for Functions

Posted 31 March 2012 - 09:31 PM

wizzard1200, on 31 March 2012 - 11:18 PM, said:

r.stiltskin is right, you really don't need to be concerned about those unintialized variables.

I didn't say that. You absolutely DO have to be concerned about using unitialized variables. Unitialized variables can have any garbage values, and if you use them in calculations, the results of those calculations will be unreliable garbage too.

And I would never suggest turning off compiler warnings. I usually suggest increasing the warning level.

If you use the Gnu compilers (gcc, g++, MinGW) I recommend the compiler warning options -Wall -Wextra -pedantic at least. For Visual C++, warning level 4.

This post has been edited by r.stiltskin: 31 March 2012 - 09:34 PM

### #5 TommehTGOD

Reputation: 0
• Posts: 31
• Joined: 25-March 12

## Re: Help with Data Structures for Functions

Posted 03 April 2012 - 08:33 AM

Ok i fixed most of all of the warnings...now i'm getting this one small warning where line 61 is not being initialized..I can't seem to find the problem of initializing those..Maybe if this is fixed..the output would come out correct...guidance please?

here's the warning: c:\users\tommeh\desktop\program 5\program 5\main.cpp(61): warning C4700: uninitialized local variable 'stud' used

EDIT: I tried initializing stud.ID and stud.gpa like this
stud.ID=0;
stud.gpa=0;
i initialized those two in while loop along with the other variables that are initialized, but in the output i got all 0's in the ID and -1.\$ in the gpa.

```//Program Assignment 4
//Thomas Powe 3-20-12
//CS207

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <stdlib.h>
using namespace std;

struct Student
{
int ID;
double points;
double hours;
double gpa;
};
//function prototypes go here//

void printfile(ofstream &OutputFile);
//double findHours(double ch);
double findGPA(Student);
int main()
{
Student stud;
//double gpa, points, hours;
//int ID;
double ch;

ofstream OutputFile;
OutputFile.open("Output.dat");

ifstream infile;
infile.open("Input.dat");

printfile(OutputFile);

OutputFile<<fixed<<showpoint<<setprecision(1)<<endl;

infile>>stud.ID;    //Reads in ID by itself
while (!infile.eof())
{
//initialize variables
Student stud;
stud.points=0;
stud.hours=0;
ch=0;
while(ch >=0)  //When flags or sentinel values are read, the loop terminates
{
stud.points = stud.points + findPoints(grade, ch);
stud.hours = stud.hours + ch;
stud.gpa = findGPA(stud);
}
OutputFile<<setw(15)<<stud.ID<<setw(18)<<stud.gpa<<endl;

}
return 0;

OutputFile.close();
infile.close();
}
//Function Definitions Go Here
void printfile(ofstream &OutputFile)
{
OutputFile<<setw(30)<<"Thomas Powe"<<endl;
OutputFile<<setw(29)<<"03-27-12"<<endl<<endl;
OutputFile<<setw(15)<<"ID"<<setw(18)<<"GPA"<<endl<<endl;
return;
}

{
Student stud;
stud.points=0;
stud.points = stud.points + grade * ch;
return stud.points;
}

//double findHours(double ch)
//{
//Student stud;
//Student hours;
//stud.hours=0;
//stud.hours = ch +ch;
//return stud.hours;
//}
double findGPA(Student)
{
Student stud;
stud.points=0;
stud.hours=0;
stud.gpa= stud.points/stud.hours;
return stud.gpa;
}
```

This post has been edited by TommehTGOD: 03 April 2012 - 08:47 AM

### #6 jimblumberg

Reputation: 5420
• Posts: 16,897
• Joined: 25-December 09

## Re: Help with Data Structures for Functions

Posted 03 April 2012 - 08:56 AM

The only warnings I receive when I compile your code is a shadowed variable. On line 28 you create an instance of your Student structure named stud, and then on line 48 you create a different instance of your Student structure named stud. Also you have a couple of lines that will never be reached because of the return statement on line 65.

You also need to find an indentation style you like and use it consistently, your code as presented is very hard to read, a proper indentation style would improve the program remarkably.

Jim

### #7 TommehTGOD

Reputation: 0
• Posts: 31
• Joined: 25-March 12

## Re: Help with Data Structures for Functions

Posted 03 April 2012 - 09:16 AM

fixed the problem, but i''m still doing something wrong....i can't seem to pass off Student
is i'm calling in line 52 right?? because when the gpa calculates, i still get 1.\$ and this time, the whole file is getting read only once
i tried doing:
stud.gpa=findGPA(Student); but it won't let me call it through
the only one that works is stud.gpa=findGPA(stud); <----- I think this is giving me the problem
```struct Student
{
int ID;
double points;
double hours;
double gpa;
Student ()
{
ID=0;
points=0;
hours=0;
gpa=0;
}
};
//function prototypes go here//

void printfile(ofstream &OutputFile);
//double findHours(double ch);
double findGPA(Student);
int main()
{
Student stud;
//double gpa, points, hours;
//int ID;
double ch;

ofstream OutputFile;
OutputFile.open("Output.dat");

ifstream infile;
infile.open("Input.dat");

printfile(OutputFile);

OutputFile<<fixed<<showpoint<<setprecision(1)<<endl;

infile>>stud.ID;    //Reads in ID by itself
while (!infile.eof())
{
//initialize variables
//stud.points=0;
//stud.hours=0;
ch=0;
while(ch >=0)  //When flags or sentinel values are read, the loop terminates
{
stud.points = stud.points + findPoints(grade, ch);
stud.hours = stud.hours + ch;
stud.gpa = findGPA(stud);
}
OutputFile<<setw(15)<<stud.ID<<setw(18)<<stud.gpa<<endl;

OutputFile.close();
infile.close();
}
return 0;
}
//Function Definitions Go Here
void printfile(ofstream &OutputFile)
{
OutputFile<<setw(30)<<"Thomas Powe"<<endl;
OutputFile<<setw(29)<<"03-27-12"<<endl<<endl;
OutputFile<<setw(15)<<"ID"<<setw(18)<<"GPA"<<endl<<endl;
return;
}

{
Student stud;
//stud.points=0;
stud.points = stud.points + grade * ch;
return stud.points;
}

//double findHours(double ch)
//{
//Student stud;
//Student hours;
//stud.hours=0;
//stud.hours = ch +ch;
//return stud.hours;
//}
double findGPA(Student)
{
Student stud;
//stud.points=0;
//stud.hours=0;
stud.gpa= stud.points/stud.hours;
return stud.gpa;
}
```

### #8 jimblumberg

Reputation: 5420
• Posts: 16,897
• Joined: 25-December 09

## Re: Help with Data Structures for Functions

Posted 03 April 2012 - 09:31 AM

Why are you closing your file inside you read loop? What header files are you including? How are you scoping the functions from the std namespace?

Jim

### #9 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Help with Data Structures for Functions

Posted 03 April 2012 - 09:31 AM

The file is read only once because you close the file inside the loop. It would be easier to see that if you would format your code properly.

echo, echo, echo....

This post has been edited by r.stiltskin: 03 April 2012 - 09:33 AM

### #10 TommehTGOD

Reputation: 0
• Posts: 31
• Joined: 25-March 12

## Re: Help with Data Structures for Functions

Posted 03 April 2012 - 09:45 AM

oh wow...Thanks...my fault..I don't why i moved the close files back in there...great..almost finish..Now i just need to calculate this gpa correctly

```//Program Assignment 4
//Thomas Powe 3-20-12
//CS207

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <stdlib.h>
using namespace std;

struct Student
{
int ID;
double points;
double hours;
double gpa;
Student ()
{
ID=0;
points=0;
hours=0;
//gpa=0;
}
};
//function prototypes go here//

void printfile(ofstream &OutputFile);
//double findHours(double ch);
double findGPA(Student);
int main()
{
Student stud;
//double gpa, points, hours;
//int ID;
double ch;

ofstream OutputFile;
OutputFile.open("Output.dat");

ifstream infile;
infile.open("Input.dat");

printfile(OutputFile);

OutputFile<<fixed<<showpoint<<setprecision(1)<<endl;

infile>>stud.ID;    //Reads in ID by itself
while (!infile.eof())
{
//initialize variables
//stud.points=0;
//stud.hours=0;
ch=0;
while(ch >=0)  //When flags or sentinel values are read, the loop terminates
{
stud.points = stud.points + findPoints(grade, ch);
stud.hours = stud.hours + ch;
stud.gpa = findGPA(stud);
}
OutputFile<<setw(15)<<stud.ID<<setw(18)<<stud.gpa<<endl;

}
OutputFile.close();
infile.close();
return 0;
}
//Function Definitions Go Here
void printfile(ofstream &OutputFile)
{
OutputFile<<setw(30)<<"Thomas Powe"<<endl;
OutputFile<<setw(29)<<"03-27-12"<<endl<<endl;
OutputFile<<setw(15)<<"ID"<<setw(18)<<"GPA"<<endl<<endl;
return;
}

{
Student stud;
//stud.points=0;
stud.points = stud.points + grade * ch;
return stud.points;
}

//double findHours(double ch)
//{
//Student stud;
//Student hours;
//stud.hours=0;
//stud.hours = ch +ch;
//return stud.hours;
//}
double findGPA(Student)
{
Student stud;
//stud.points=0;
//stud.hours=0;
stud.gpa= stud.points/stud.hours;
return stud.gpa;
}
```

I hope this is better indentation

### #11 jimblumberg

Reputation: 5420
• Posts: 16,897
• Joined: 25-December 09

## Re: Help with Data Structures for Functions

Posted 03 April 2012 - 09:56 AM

Quote

I hope this is better indentation

In my opinion no it is not much better. Try something like;

```//Program Assignment 4
//Thomas Powe 3-20-12
//CS207

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

struct Student
{
int ID;
double points;
double hours;
double gpa;
Student ()
{
ID=0;
points=0;
hours=0;
//gpa=0;
}
};
//function prototypes go here//

void printfile(ofstream &OutputFile);
//double findHours(double ch);
double findGPA(Student);
int main()
{
Student stud;
//double gpa, points, hours;
//int ID;
double ch;

ofstream OutputFile;
OutputFile.open("Output.dat");

ifstream infile;
infile.open("Input.dat");

printfile(OutputFile);
OutputFile<<fixed<<showpoint<<setprecision(1)<<endl;

infile>>stud.ID;    //Reads in ID by itself
while (!infile.eof())
{
//initialize variables
//stud.points=0;
//stud.hours=0;
ch=0;
while(ch >=0)  //When flags or sentinel values are read, the loop terminates
{
stud.points = stud.points + findPoints(grade, ch);
stud.hours = stud.hours + ch;
stud.gpa = findGPA(stud);
}
OutputFile<<setw(15)<<stud.ID<<setw(18)<<stud.gpa<<endl;

}
OutputFile.close();
infile.close();
return 0;
}

//Function Definitions Go Here
void printfile(ofstream &OutputFile)
{
OutputFile<<setw(30)<<"Thomas Powe"<<endl;
OutputFile<<setw(29)<<"03-27-12"<<endl<<endl;
OutputFile<<setw(15)<<"ID"<<setw(18)<<"GPA"<<endl<<endl;
return;
}

{
Student stud;
//stud.points=0;
stud.points = stud.points + grade * ch;
return stud.points;
}

//double findHours(double ch)
//{
//Student stud;
//Student hours;
//stud.hours=0;
//stud.hours = ch +ch;
//return stud.hours;
//}
double findGPA(Student)
{
Student stud;
//stud.points=0;
//stud.hours=0;
stud.gpa= stud.points/stud.hours;
return stud.gpa;
}

```

Jim

### #12 TommehTGOD

Reputation: 0
• Posts: 31
• Joined: 25-March 12

## Re: Help with Data Structures for Functions

Posted 03 April 2012 - 12:31 PM

with the output of the gpa coming out like: -1.\$
does it have something to do with the variables, double grade, ch;??
because they are in the function prototype findPoints(double grade, double ch)
and stud.points is in the function definition, which the variable "points" is in the data structure

My professor ONLY wanted the variables gpa, points, hours, and ID to be in the structure, leaving the other variables "grade" and "ch" out. How will I go about fixing that problem?

### #13 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Help with Data Structures for Functions

Posted 03 April 2012 - 12:37 PM

What's in your input file? Post a few lines.

### #14 jimblumberg

Reputation: 5420
• Posts: 16,897
• Joined: 25-December 09

## Re: Help with Data Structures for Functions

Posted 03 April 2012 - 12:38 PM

Show your current code. There were a couple of errors in your findGPA() function in your last code post.

Jim

### #15 TommehTGOD

Reputation: 0
• Posts: 31
• Joined: 25-March 12

## Re: Help with Data Structures for Functions

Posted 03 April 2012 - 12:51 PM

oh snap, i fixed the problem (i think)
here's what i did
```//Program Assignment 4
//Thomas Powe 3-20-12
//CS207

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <stdlib.h>
using namespace std;

struct Student
{
int ID;
double points;
double hours;
double gpa;
Student ()
{
ID=0;
points=0;
hours=0;
gpa=0;
}
};
//function prototypes go here//

void printfile(ofstream &OutputFile);
//double findHours(double ch);
double findGPA(Student stud);
int main()
{
Student stud;
//double gpa, points, hours;
//int ID;
double ch;

ofstream OutputFile;
OutputFile.open("Output.dat");

ifstream infile;
infile.open("Input.dat");

printfile(OutputFile);

OutputFile<<fixed<<showpoint<<setprecision(1)<<endl;

infile>>stud.ID;    //Reads in ID by itself
while (!infile.eof())
{
//initialize variables
ch=0;
while(ch >=0)  //When flags or sentinel values are read, the loop terminates
{
stud.points = stud.points + findPoints(grade, ch);
stud.hours = stud.hours + ch;
stud.gpa = findGPA(stud);
}
OutputFile<<setw(15)<<stud.ID<<setw(18)<<stud.gpa<<endl;

}
OutputFile.close();
infile.close();
return 0;
}
//Function Definitions Go Here
void printfile(ofstream &OutputFile)
{
OutputFile<<setw(30)<<"Thomas Powe"<<endl;
OutputFile<<setw(29)<<"03-27-12"<<endl<<endl;
OutputFile<<setw(15)<<"ID"<<setw(18)<<"GPA"<<endl<<endl;
return;
}

{
Student stud;
stud.points = stud.points + grade * ch;
return stud.points;
}

//double findHours(double ch)
//{
//Student stud;
//Student hours;
//stud.hours=0;
//stud.hours = ch +ch;
//return stud.hours;
//}
double findGPA(Student stud)
{
//Student stud;
stud.gpa= stud.points/stud.hours;
return stud.gpa;
}
```

I saw that Student was a datatype, so i wasn't calling the rest of it (if i'm saying this right. where you see on line 97, I had to add stud along with Student and the rest of the other places.

so here's my current output: it's still not right though in terms of gpa calculation, but the -1.\$ is gone.

```       Thomas Powe
03-27-12

ID               GPA

30523               3.0
31536               3.2
22537               3.1
21345               3.0
19364               3.0
19335               2.7
18264               2.6
20135               2.6
22185               2.8

```

but this is supposed to be the REAL output
```ID         GPA
30523         3.0
31536         3.4
22537         2.8
21345         2.9
19364         2.5
19335         0.9
18264         2.4
20135         1.9
22185         4.0

```

And here's the input code:
```30523 3 3 3 2 3 4 2 3 4 3 -9 -9
31536 3 1 4 3 3 4 4 3 3 3 -9 -9
22537 4 4 3 3 2 3 2 3 -9 -9
21345 4 1.5 3 3 3 0.5 2 3 3 3 -9 -9
19364 2 3 3 3 -9 -9
19335 0 3 0 4 2 3 3 1.5 -9 -9
18264 2 3 3 0.5 3 0.5 3 3 2 3 -9 -9
20135 4 1 3 3 2 1.5 0 3 -9 -9
22185 4 3 4 4 4 3 4 3 4 3 -9 -9
```

This post has been edited by r.stiltskin: 03 April 2012 - 12:55 PM
Reason for edit:: Added code tags. You should use them whenever you want to preserve the formatting of some text.