Code review and loop assistance

My code is 90% done but my brains fried

Page 1 of 1

4 Replies - 1261 Views - Last Post: 05 December 2006 - 04:34 PM Rate Topic: -----

#1 richiewinters  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 06-November 06

Code review and loop assistance

Posted 04 December 2006 - 11:47 AM

Ok. Been working on this one all week and I'm mostly finished.

Just stuck on writing up a loop and also help reading from the file created during program execution and displaying the info on screen.

Here's the code (the parts I need help with are near the bottom (but before the functions) and are commented:

//A student gradebook program that gets information from the user
//and then creates a text file based on that information. After that
//the information is input from the text file and displayed on screen.
//
//November 29, 2006
//
////////////////////////////////////////////////

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

using namespace std;

struct grades
{
	string firstName;
	string lastName;
	string idNumber;
	int programs[3];
	int exams[3];
	int finalExam;
	char courseGrade;
};

void printLine();
void calcGrade (grades &student);

int main()
{
	ifstream infile;
	ofstream outfile;
	int noOfStudents = 0;
	grades student[100];
	int count;
	int counter = 0;

	outfile.open("gradebook.txt");

	cout << "Enter the class size: ";
	cin >> noOfStudents;

	printLine();

	while(noOfStudents > counter)
	{
		cout << "Enter student's first and last name: ";
		cin >> student[counter].firstName >> student[counter].lastName;

		cout << "Enter student id number: ";
		cin >> student[counter].idNumber;

		cout << "Enter 3 programming grades (each seperated by a space)" << endl
			<< "Maximum grade for each program is 100: ";

		for(count = 0; count < 3; count++)
			cin >> student[counter].programs[count];

		cout << "Enter 3 exam grades (each seperated by a space)" << endl
			<< "Maximum grade for each exam is 100: ";
		for(count = 0; count < 3; count++)
			cin >> student[counter].exams[count];

		cout << "Enter final exam grade (max grade is 100): ";
		cin >> student[counter].finalExam;

		calcGrade(student[counter]);


//You need a loop here to output programs and exams
		
		outfile << student[counter].firstName << " " << student[counter].lastName << " " << student[counter].idNumber
			<< "	" << student[counter].programs[count]
			<< "	" << student[counter].exams[count]
			<< "	" << student[counter].finalExam << " " << student[counter].courseGrade << endl;

		counter++;

		printLine();
	}

	outfile.close();

//After that I need to read the input from the file and display it on screen

	infile.open("gradebook.txt");

	if(!infile)
		cout << "Could not open the file.";

	while(infile) //I know this is completely wrong since it's supposed to read
	{			 //from the file, so I can't use the variables I have below.

		cout << "Student name: " << student.firstName << student.lastName << endl
			<< "ID Number: " << student.idNumber << endl
			<< "Programs: " << student.programs[0] << student.programs[1] << student.programs[2] << endl
			<< "Exams: " << student.exams[0] << student.exams[1] << student.exams[2] << endl
			<< "Final Exam: " << student.finalExam << endl
			<< "Course Grade: " << student.courseGrade << endl << endl << endl;
	}





	return 0;

}

void printLine()
{
	cout << "----------------------------------------" << endl;
}

void calcGrade (grades &student)
{
	double programAverage = 0, examAverage = 0; 
	double semesterAverage;
	int grade;

	for (int count = 0; count < 3; count++)
		programAverage += student.programs[count];
	for (int exam = 0; exam < 3; exam++)
		examAverage += student.exams[exam];

	semesterAverage = programAverage/3 * .30 + examAverage/3 * .45 + student.finalExam * .25;
	grade = (int) semesterAverage/10;

	switch (grade)
	{
		case 10:
		case 9:	student.courseGrade = 'A';
				break;
		case 8:	student.courseGrade = 'B';
				break;
		case 7:	student.courseGrade = 'C';
				break;
		case 6:	student.courseGrade = 'D';
				break;
		default:student.courseGrade = 'F';
	}
}



Here's a sample of how the output/screen should ultimately look like (in case it helps):

Enter the class size: 3
----------------------------------------
Enter student's first and last name: richard winters
Enter student id number: 2004384
Enter 3 programming grades (each separated by a space)
 Maximum grade for each program is 100:
 100 100 80
Enter 3 exam grades (separated by a space)
 Maximum grade for each exam is 100: 96 95 93
Enter final exam grade
 Maximum grade for final exam is 100: 94
----------------------------------------
Enter student's first and last name: sue smith
Enter student id number: 244333
Enter 3 programming grades (each separated by a space)
 Maximum grade for each program is 100:
 80 95 95
Enter 3 exam grades (separated by a space)
 Maximum grade for each exam is 100: 93 92 92
Enter final exam grade
 Maximum grade for final exam is 100: 90
----------------------------------------
Enter student's first and last name: joe klein
Enter student id number: 3004222
Enter 3 programming grades (each separated by a space)
 Maximum grade for each program is 100:
 87 92 93
Enter 3 exam grades (separated by a space)
 Maximum grade for each exam is 100: 89 88 84
Enter final exam grade
 Maximum grade for final exam is 100: 90
----------------------------------------
Student Name: richard winters
ID Number: 2004384
Programs: 100 100 80
Exams:	96 95 93
Final Exam:   94
Course Grade: A

Student Name: sue smith
ID Number: 244333
Programs: 80 95 95
Exams:	93 92 92
Final Exam:   90
Course Grade: A

Student Name: joe klein
ID Number: 3004222
Programs: 87 92 93
Exams:	89 88 84
Final Exam:   90
Course Grade: B


Press Enter Key to Continue

This post has been edited by richiewinters: 04 December 2006 - 06:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Code review and loop assistance

#2 richiewinters  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 06-November 06

Re: Code review and loop assistance

Posted 04 December 2006 - 03:26 PM

Code updated.

I think I got the second loop figured out for outputting the data to the screen. Still fumbling with the other commented loop.
Was This Post Helpful? 0
  • +
  • -

#3 eXceed69  Icon User is offline

  • "Super Sentai Knight Of DawN"
  • member icon

Reputation: 7
  • View blog
  • Posts: 688
  • Joined: 12-November 06

Re: Code review and loop assistance

Posted 04 December 2006 - 05:00 PM

wow that was fast to figure out your program...

Nice...:D
Was This Post Helpful? 0
  • +
  • -

#4 richiewinters  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 06-November 06

Re: Code review and loop assistance

Posted 04 December 2006 - 06:56 PM

Just edited my code again because I spotted a couple other errors.

Still need help on those loops (these loops and arrays and whatnot are still very confusing to me).

With the code as it is now (that is, incomplete), I get this error when I try to build it:

Quote

c:\documents and settings\richie\my documents\visual studio 2005\projects\ch11_ex1_winters\ch11_ex1_winters\ch11_ex1_winters.cpp(101) : error C2228: left of '.firstName' must have class/struct/union
type is 'grades [100]'
c:\documents and settings\richie\my documents\visual studio 2005\projects\ch11_ex1_winters\ch11_ex1_winters\ch11_ex1_winters.cpp(101) : error C2228: left of '.lastName' must have class/struct/union
type is 'grades [100]'
c:\documents and settings\richie\my documents\visual studio 2005\projects\ch11_ex1_winters\ch11_ex1_winters\ch11_ex1_winters.cpp(102) : error C2228: left of '.idNumber' must have class/struct/union
type is 'grades [100]'
c:\documents and settings\richie\my documents\visual studio 2005\projects\ch11_ex1_winters\ch11_ex1_winters\ch11_ex1_winters.cpp(103) : error C2228: left of '.programs' must have class/struct/union
type is 'grades [100]'
c:\documents and settings\richie\my documents\visual studio 2005\projects\ch11_ex1_winters\ch11_ex1_winters\ch11_ex1_winters.cpp(103) : error C2228: left of '.programs' must have class/struct/union
type is 'grades [100]'
c:\documents and settings\richie\my documents\visual studio 2005\projects\ch11_ex1_winters\ch11_ex1_winters\ch11_ex1_winters.cpp(103) : error C2228: left of '.programs' must have class/struct/union
type is 'grades [100]'
c:\documents and settings\richie\my documents\visual studio 2005\projects\ch11_ex1_winters\ch11_ex1_winters\ch11_ex1_winters.cpp(104) : error C2228: left of '.exams' must have class/struct/union
type is 'grades [100]'
c:\documents and settings\richie\my documents\visual studio 2005\projects\ch11_ex1_winters\ch11_ex1_winters\ch11_ex1_winters.cpp(104) : error C2228: left of '.exams' must have class/struct/union
type is 'grades [100]'
c:\documents and settings\richie\my documents\visual studio 2005\projects\ch11_ex1_winters\ch11_ex1_winters\ch11_ex1_winters.cpp(104) : error C2228: left of '.exams' must have class/struct/union
type is 'grades [100]'
c:\documents and settings\richie\my documents\visual studio 2005\projects\ch11_ex1_winters\ch11_ex1_winters\ch11_ex1_winters.cpp(105) : error C2228: left of '.finalExam' must have class/struct/union
type is 'grades [100]'
c:\documents and settings\richie\my documents\visual studio 2005\projects\ch11_ex1_winters\ch11_ex1_winters\ch11_ex1_winters.cpp(106) : error C2228: left of '.courseGrade' must have class/struct/union
type is 'grades [100]'
Build log was saved at "file://c:\Documents and Settings\Richie\My Documents\Visual Studio 2005\Projects\Ch11_Ex1_Winters\Ch11_Ex1_Winters\Debug\BuildLog.htm"
Ch11_Ex1_Winters - 11 error(s), 0 warning(s)

Was This Post Helpful? 0
  • +
  • -

#5 richiewinters  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 06-November 06

Re: Code review and loop assistance

Posted 05 December 2006 - 04:34 PM

lol

I think I just made it worse trying to fix it. Oh well. It's an intro to C++ class anyway and I did good the rest of the semester, so turning in one half-assed program shouldn't affect my grade too much.

Here's how much worse the later part of my code is now:

//A student gradebook program that gets information from the user
//and then creates a text file based on that information. After that
//the information is input from the text file and displayed on screen.
//
//November 29, 2006
//
////////////////////////////////////////////////

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

using namespace std;

struct grades
{
	string firstName;
	string lastName;
	string idNumber;
	int programs[3];
	int exams[3];
	int finalExam;
	char courseGrade;
};

void printLine();
void calcGrade (grades &student);

int main()
{
	ifstream infile;
	ofstream outfile;
	int noOfStudents = 0;
	grades student[100];
	int count;
	int counter = 0;

	outfile.open("gradebook.txt");

	cout << "Enter the class size: ";
	cin >> noOfStudents;

	printLine();

	while(noOfStudents > counter)
	{
		cout << "Enter student's first and last name: ";
		cin >> student[counter].firstName >> student[counter].lastName;

		cout << "Enter student id number: ";
		cin >> student[counter].idNumber;

		cout << "Enter 3 programming grades (each seperated by a space)" << endl
			<< "Maximum grade for each program is 100: ";

		for(count = 0; count < 3; count++)
			cin >> student[counter].programs[count];

		cout << "Enter 3 exam grades (each seperated by a space)" << endl
			<< "Maximum grade for each exam is 100: ";
		for(count = 0; count < 3; count++)
			cin >> student[counter].exams[count];

		cout << "Enter final exam grade (max grade is 100): ";
		cin >> student[counter].finalExam;

		calcGrade(student[counter]);


//You need a loop here to output programs and exams
		
		outfile << student[counter].firstName << " " << student[counter].lastName << " " << student[counter].idNumber
			<< "	" << student[counter].programs[count]
			<< "	" << student[counter].exams[count]
			<< "	" << student[counter].finalExam << " " << student[counter].courseGrade << endl;

		counter++;

		printLine();
	}

	outfile.close();

//After that I need to read the input from the file and display it on screen

	infile.open("gradebook.txt");

	if(!infile)
		cout << "Could not open the file.";

	while (!infile.eof()) //this isn't gonna work
	{			

		getline(infile, name);
		infile >> pay >> time >> tax;
		inile.ignore(100, '\n');
	}





	return 0;

}

void printLine()
{
	cout << "----------------------------------------" << endl;
}

void calcGrade (grades &student)
{
	double programAverage = 0, examAverage = 0; 
	double semesterAverage;
	int grade;

	for (int count = 0; count < 3; count++)
		programAverage += student.programs[count];
	for (int exam = 0; exam < 3; exam++)
		examAverage += student.exams[exam];

	semesterAverage = programAverage/3 * .30 + examAverage/3 * .45 + student.finalExam * .25;
	grade = (int) semesterAverage/10;

	switch (grade)
	{
		case 10:
		case 9:	student.courseGrade = 'A';
				break;
		case 8:	student.courseGrade = 'B';
				break;
		case 7:	student.courseGrade = 'C';
				break;
		case 6:	student.courseGrade = 'D';
				break;
		default:student.courseGrade = 'F';
	}
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1