Can someone run this to see if its wrong.
Dec 21, 2012 at 9:02pm UTC
I'm trying to get this this switch statement to run and its not doing anything it just keeps looping whatever I put in. It doesn't execute anything inside the switch statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
// Week 13: In Class Exercise - 5 Solution
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
// Declared constants
const int NUM_EXAMS = 3;
const int NUMBER_STUDENTS = 6;
// Structure definition
struct StuData
{
string firstName;
string lastName;
float ID;
float exam[NUM_EXAMS];
float avgGrade;
char letterGrade;
};
// Function prototypes
void programDescription();
void getStuData(StuData[]);
void letterGrade(StuData[]);
void writeStuInfo(ofstream&, StuData[]);
void readStuInfo(ifstream&, StuData[]);
void calcGrades(StuData[]);
void displayStuInfo(const StuData[]);
float calcClassAverage(const StuData[]);
int main()
{
// Program description
programDescription();
// Declare variables
StuData stu1[NUMBER_STUDENTS];
ofstream outFile;
ifstream inFile;
char choice;
string filename;
// open file for output
outFile.open("results.txt" );
if (outFile.fail())
{
cout << "Error opening file" << endl;
exit(1);
}
do {
// Process data as read and display in tabular format
cout << "Enter 1-7 to select one of the following choices: " << endl;
cout << "\t1. Enter the student data file name." << endl;
cout << "\t2. Display the average and the letter grade for each student" << endl;
cout << "\t3. Sort data from highest to lowest grade." << endl;
cout << "\t4. Display the sorted data." << endl;
cout << "\t5. Search for a student record by last name." << endl;
cout << "\t6. Write the sorted data to the 'results.txt' file " << endl;
cout << "\t7. EXIT." << endl;
cin >> choice;
switch (choice){
case 1:
cout << "\nEnter the file name: " ;
cin >> filename;
// Opens file for input
inFile.open(filename.c_str());
if (inFile.fail())
{
cout << "Error opening file" << endl;
exit(1);
}
break ;
case 2:
readStuInfo(inFile, stu1);
break ;
case 3:
break ;
}
}while (choice !=3);
//Calculations and handle results
calcGrades(stu1);
displayStuInfo(stu1);
writeStuInfo(outFile, stu1);
cout << "\nThe class average is " << calcClassAverage(stu1) << endl;
// Close data files
inFile.close();
outFile.close();
return 0;
}
void programDescription()
{
cout << "This program reads student grade information from" << endl
<< "a file, displays it in a tabular format, and" << endl
<< "writes the data to another file." << endl;
}
// Modify function to write contents of entire array of structures to file
void writeStuInfo(ofstream& outFile, StuData stu1[])
{
for (int i = 0; i < NUMBER_STUDENTS; i++)
outFile << endl << left << setw(15) << stu1[i].lastName
<< setw(20) << stu1[i].avgGrade << stu1[i].letterGrade;
}
// Modify function to read contents of entire array of structures from file
void readStuInfo(ifstream& inFile, StuData stu1[])
{
int i = 0;
while (i < NUMBER_STUDENTS && inFile >> stu1[i].firstName >> stu1[i].lastName)
{
inFile >> stu1[i].ID >> stu1[i].exam[0] >> stu1[i].exam[1] >> stu1[i].exam[2];
i++;
}
}
void calcGrades(StuData stu1[])
{
for (int i = 0; i < NUMBER_STUDENTS; i++)
stu1[i].avgGrade = (stu1[i].exam[0] + stu1[i].exam[1] + stu1[i].exam[2]) / 3;
letterGrade(stu1);
}
void letterGrade(StuData stu[])
{
for ( int i = 0; i < NUMBER_STUDENTS; i++)
{
if (stu[i].avgGrade >= 90)
stu[i].letterGrade = 'A' ;
else if (stu[i].avgGrade >= 80)
stu[i].letterGrade = 'B' ;
else if (stu[i].avgGrade >= 70)
stu[i].letterGrade = 'c' ;
else if (stu[i].avgGrade >= 60)
stu[i].letterGrade = 'D' ;
else
stu[i].letterGrade = 'F' ;
}
}
void displayStuInfo(const StuData stu1[])
{
// Display header line
cout << endl << endl
<< "First Last ID Exam Exam Exam Overall Letter" << endl
<< "Name Name # 1 2 3 Average Grade" << endl
<< "---- ---- ---- ---- ---- ----- ------- -----" << endl;
// Need loop to display contents of array to screen.
for (int i = 0; i < NUMBER_STUDENTS; i++)
{
cout << left << setw(8);
cout <<stu1[i].firstName <<setw(10)<< stu1[i].lastName << setw(7)<<stu1[i].ID << setw(10) << stu1[i].exam[0] << setw(10)
<< stu1[i].exam[1] << setw(10) << stu1[i].exam[2];
cout << setw(13) << stu1[i].avgGrade << setw(10) << stu1[i].letterGrade << endl;
}
}
float calcClassAverage(const StuData stu1[])
{
float total = 0;
for (int i = 0; i < NUMBER_STUDENTS; i++)
total += stu1[i].avgGrade;
return total/NUMBER_STUDENTS;
}
Dec 21, 2012 at 9:04pm UTC
You're inputting a char, but you're treating it as an integer. The integral representation of char '1' is 49, not 1. (read up on ASCII characters)
You have two options to resolve this. Make
char choice
an int instead (which is what I recommend), or in your switch statement, use characters for the comparison:
1 2 3 4 5 6 7 8 9
switch (choice)
{
case '1' : //wrap 1 in single quotation marks to tell the compiler you want the char value
//do something
case '2' :
//do something else
default :
break ;
}
Last edited on Dec 21, 2012 at 9:07pm UTC
Topic archived. No new replies allowed.