Skip to main content
gamedev.net
🔒 Locked

C++ program

Started by borogirl09 Nov 19, 2010 at 10:32 AM 13 replies 2.2k views
Original Post
borogirl09
borogirl09
I am really confused at the moment. I wrote the program, it compiles fine, but when I run it nothing happens. I am not sure what is wrong. If you could take a look and see if notice where I goofed.

The data file named studentfile.txt contains the following information

CRYSTAL LEONARD 92
ANN MOORE 88
DAVID JONES 100
JOE CARSON 76

------------------------------------------------------------------

//// Author: Lauren Settle//// Program: Student Test Scores//// This program prompts the user to input the names of students followed by the corresponding test scores.// The program will then read and output the student name, test score, and the letter grade based on the score.// The program will also output the highest test score along with the student who recieved it.////#include <iostream>#include <fstream>#include <iomanip>#include <string>using namespace std;struct studentType{       string studentFName; //students first name       string studentLName; //students last name       int testScore; //students test score       char grade; //corresponding letter grade};void openFile (fstream& inFile);void getData (fstream& inFile, studentType studentData[], int listSize);void assignGrade (studentType studentData[], int listSize);void highScore (studentType studentData[], int listSize); void printResults (studentType studentData[]);int main(){        fstream inFile;    studentType studentData[20];    int listSize = 20;        openFile (inFile);    getData (inFile, studentData, listSize);    assignGrade (studentData, listSize);    highScore (studentData, listSize);    printResults (studentData);        /* Scaffolding code for testing purposes */ cin.ignore(256, '\n');cout << "Press ENTER to continue..." << endl;cin.get();/* End Scaffolding */     return 0;}void openFile (fstream& inFile){     inFile.open ("STUDENTFILE.TEXT", ios::in);     }void getData (fstream& inFile, studentType studentData[], int listSize){          int j;     while (!inFile.eof())     {      for (j = 0; j < listSize; j++)      {       inFile >> studentData[j].studentFName              >> studentData[j].studentLName              >> studentData[j].testScore;       }      }}void assignGrade (studentType studentData[], int listSize){     int j;          for (j = 0; j < listSize; j++)     {         if (studentData[j].testScore >= 90)        studentData[j].grade = 'A';     else if (studentData[j].testScore >= 80)        studentData[j].grade = 'B';     else if (studentData[j].testScore >= 70)        studentData[j].grade = 'C';     else if (studentData[j].testScore >= 60)        studentData[j].grade = 'D';     else         studentData[j].grade = 'F';     }}void highScore (studentType studentData[], int listSize) {     int i;     double maxScore = 0;     int m;               for (i = 1; i < listSize; i++)         if (studentData.testScore >= maxScore)            {               maxScore = studentData.testScore;               m = i;            }}void printResults (studentType studentData[]){     int j, m;          cout << left;     cout << studentData[j].studentLName << " , ";     cout << studentData[j].studentFName;     cout << right;     cout << setw(15) << studentData[j].testScore;     cout << setw(20) << studentData[j].grade;     cout << endl << endl << endl << endl;          cout << left;     cout << " Highest Test Score " << endl;     cout << studentData[m].testScore;     cout << setw(10) << studentData[m].studentFName << " " << studentData[m].studentLName;     cout << endl;}


[Edited by - Zahlman on November 20, 2010 2:32:16 AM]
rip-off
rip-off
Have you tried running it in a debugger? The printResults() function contains an uninitialised variable, which probably is causing your program to crash.
Sneftel
Sneftel
Moved to the For Beginners forum.
nobodynews
nobodynews
Your code says the file is a .TEXT file put your post says the file is named .txt. That's definitely not the only problem in your code(see rip-off's post), but fixing that would at least get you further along.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
borogirl09
borogirl09
I guess since it's my program I see what I want to see, because I am not seeing whats wrong with printResults. I did fix the file name, but it is still not working. I have tried the debugger, but since I dont really know what I'm doing with it, its not doin much good.
rip-off
rip-off
You are using the variables "j" and "m" to index the student array. These variables are uninitialised, they could have any value. In C++, variables in different scopes are unrelated. So the "m" in highScore is different from "m" in printResults. You could return the value of "m" in highScore to main(), and have main() pass it to printResults as a parameter.

Also, consider increasing your compiler's warning level, use of uninitialised variables should trigger a warning. You should write your code such that no warnings are generated when you compile.
borogirl09
borogirl09
UGHHHH, i'm getting super frustrated. I just can't see where the error/errors lie.

//// Author: Lauren Settle//// Program: Student Test Scores//// This program prompts the user to input the names of students followed by the corresponding test scores.// The program will then read and output the student name, test score, and the letter grade based on the score.// The program will also output the highest test score along with the student who recieved it.////#include <iostream>#include <fstream>#include <iomanip>#include <string>using namespace std;struct studentType{       string studentFName; //students first name       string studentLName; //students last name       int testScore; //students test score       char grade; //corresponding letter grade};void openFile (fstream& inFile);void getData (fstream& inFile, studentType studentData[], int listSize);void assignGrade (studentType studentData[], int listSize);double highScore (studentType studentData[], int listSize); void printResults (studentType studentData[], int m);int main(){        fstream inFile;    studentType studentData[20];    int listSize = 20;    int m;        openFile (inFile);    getData (inFile, studentData, listSize);    assignGrade (studentData, listSize);    highScore (studentData, listSize);    printResults (studentData, m);        /* Scaffolding code for testing purposes */ cin.ignore(256, '\n');cout << "Press ENTER to continue..." << endl;cin.get();/* End Scaffolding */     return 0;}void openFile (fstream& inFile){     inFile.open ("STUDENTFILE.TXT", ios::in);     }void getData (fstream& inFile, studentType studentData[], int listSize){          int j;     while (!inFile.eof())     {      for (j = 0; j < listSize; j++)      {       inFile >> studentData[j].studentFName              >> studentData[j].studentLName              >> studentData[j].testScore;       }      }}void assignGrade (studentType studentData[], int listSize){     int j;          for (j = 0; j < listSize; j++)     {         if (studentData[j].testScore >= 90)        studentData[j].grade = 'A';     else if (studentData[j].testScore >= 80)        studentData[j].grade = 'B';     else if (studentData[j].testScore >= 70)        studentData[j].grade = 'C';     else if (studentData[j].testScore >= 60)        studentData[j].grade = 'D';     else         studentData[j].grade = 'F';     }}double highScore (studentType studentData[], int listSize) {     int i;     double maxScore = 0;     int m;               for (i = 1; i < listSize; i++)         if (studentData.testScore >= maxScore)            {               maxScore = studentData.testScore;               m = i;            }     return m;}void printResults (studentType studentData[], int m){     int j;          cout << left;     cout << studentData[j].studentLName << " , ";     cout << studentData[j].studentFName;     cout << right;     cout << setw(15) << studentData[j].testScore;     cout << setw(20) << studentData[j].grade;     cout << endl << endl << endl << endl;          cout << left;     cout << " Highest Test Score " << endl;     cout << studentData[m].testScore;     cout << setw(10) << studentData[m].studentFName << " " << studentData[m].studentLName;     cout << endl;}


[Edited by - Zahlman on November 20, 2010 2:27:29 AM]
Molle85
Molle85
I am all for seeking help when you need it, but when you post your entire code and basically saying "this doesn't work, fix it for me", then you've given up.
Programming is problem solving, and if you don't even wanna try and figure out what is wrong then why bother ?

Go through each line and ask yourself what it does.
borogirl09
borogirl09
This is my first time using structs, and since i'm taking this class online, I don't have any real instruction, so its me teaching myself. I can do the program using just arrays, but doing it with structs is confusing me. I don't understand what I'm doing wrong, thats all. I'm not asking for you to fix it, but I don't know where my errors are. I just need direction
Hardcharger
Hardcharger
Lauren, you might want to check your getData function. You have an infinite loop going on and that's why your program is "doing nothing."


Also, I inserted a breakpoint after the code after line 61 and examined the contents of studentData[j].studentFName, studentData[j].StudentLName and studentData[j].testScore when j = 0. The code executed but all of the variables are blank. You might have an issue with your code reading in the values from the text file.

P.S. I copied your code and put the input data into a text file. I'm using Visual Studio 2010.
daminkz
daminkz
You should be getting an error regarding the fact that you have not forward-declared your functions.
Zahlman
Zahlman
Quote:
Original post by rip-off
You are using the variables "j" and "m" to index the student array. These variables are uninitialised, they could have any value. In C++, variables in different scopes are unrelated. So the "m" in highScore is different from "m" in printResults. You could return the value of "m" in highScore to main(), and have main() pass it to printResults as a parameter.

Also, consider increasing your compiler's warning level, use of uninitialised variables should trigger a warning. You should write your code such that no warnings are generated when you compile.


You addressed 'm' - sort of - but 'j' is still unaccounted for. And 'm' is not really dealt with, because you're just passing in the 'm' from main(), which is still uninitialized.

I think you don't really understand variables and scopes properly yet. You should review the concept of scope. It should be explained in your textbook.

Also, note that the problems here have nothing to do with the fact that you're using a struct now instead of "just" arrays. You have an array of structs, and indexing into an array of structs works the same as indexing into an array of anything else - and because of the variable scope problems, you aren't currently doing it right.
rip-off
rip-off
Also please post your actual code. The code you posted contains references to the identifiers "left" and "right", which aren't declared or defined.

Where possible, try to initialise variables in their declaration. This might mean moving them deeper into the function. For instance, it is idiomatic to declare a loop variable inside the for:
for(int i = 0 ; i < N ; ++i){   // ...}

Likewise, declare m as the return value of highScore.

And as I mentioned, try to increase your compilers warning level as high as it can go. If you don't know how to do this then hit Google.
Narishma
Narishma
Quote:
Original post by rip-off
Also please post your actual code. The code you posted contains references to the identifiers "left" and "right", which aren't declared or defined.


std::left and std::right are iostream manipulators (like std::endl) used to pad or justify the output. I personally always use the fully qualified names even if it's longer to type because it's easy to get confused with generic names like left, right, string and so on.
rip-off
rip-off
My bad. I rarely use complex I/O formatting in C++. I should really have compiled the code before making such a statement [embarrass]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.