I am having a huge problem with writing this program. Every time I ask the teacher for help she is too busy helping someone else. so she just tell me one thing and runs off to the next person without checking if it worked and what other help I might need. It has become very frustrating. I have been able to ask for a classmates help, but that is only when we have free time during class, if he's not too busy writing his own code. I am turning to you for help. I have written and adjusted this program so many times that I am so confused. Please take a look at my code and steer me in the right direction. I greatly appreciate it. Thank you. I also hope that I use these code tags the right way. Sorry if it doesn't work. Oh, and P.S. the teacher said we had to call a function and pass something by reference(what, I'm not sure, but I specifically remember her pointing it out) I'm thinking it's to do the calculations. I've tried to design this program using only function, but I'm not that great with those, so it's been difficult.

//chapter 7 lab p475 ex8 gradebook 
//Intructions:
//A teacher has five students who have taken four tests.
//The teacher uses the following grading scale to assign a letter 
//grade to a student, based on the average of his or her test scores.
//___________________________________
//	Test Score	Letter Grade
//-----------------------------------
//	90-100		A
//	80-89		B
//	70-79		C
//	60-69		D
//	0-59		F
//___________________________________
//Write a program that uses a two-dimensional array of charachters
//that hold the five student names, a single-dimensional array of 
//five characters to hold the five students' letter grades, and five
//single-dimensional arrays of four doubles to hold each student's 
//set of test scores.
//The program should allow the user to enter each student's name and
//his or her scores. It should then calculate and display each 
//student's average test score and a letter grade based on that average.
//Imput Validation: Do not accept test scores less than zero or greater
//than 100.

#include <iostream> 
using namespace std; 

//function prototype
void calcdata(double []);
void display data();


//start of main
int main() 
{ 

const int NUM_NAMES = 5;          //how many occurances 
const int NAMESIZE = 11;          //how long names can be, 10 letters 
const int NUM_TESTS = 4;          //how many tests 
char name[NUM_NAMES][NAMESIZE];   //two-dimensional name array 
char grade[NUM_NAMES];            //grade for each student array 
double average[NUM_NAMES];        //average for each student array
double student1[NUM_TESTS] 	  //student1 array
double student2[NUM_TESTS] 	  //student2 array
double student3[NUM_TESTS] 	  //student3 array
double student4[NUM_TESTS] 	  //student4 array
double student5[NUM_TESTS] 	  //student5 array

 
cout << “Enter the student’s name 
for (int count = 0; count < NUM_NAMES; count++)
 
{ // beginning of nested for loop
	cout << "Student"   << (count +1) <<": "; 
	cin >> name[count];
 
	for (i = 0; i < NUM_NAMES; i++)
	cout >> name [i];              //stub statement
} //end of for loop 

//Beginning of validation sequence taken from chapter 6 lab assignment
//********Need to change to fit with this lab assignment********

cout << "Please enter test 1, with scores between 0 and 100.\n"; 
cin >> val1; 
while (val1 < 0 || val1 > 100) 
{ 
cout << "You have not entered a number between 0 and 100. Please re-enter test 1.\n"; 
cin >> val1; 
}//end of while loop 

cout << "Please enter test 2, with scores between 0 and 100.\n"; 
cin >> val1; 
while (val2 < 0 || val2 > 100) 
{ 
cout << "You have not entered a number between 0 and 100. Please re-enter test 2.\n"; 
cin >> val2; 
}//end of while loop 

cout << "Please enter test 3, with scores between 0 and 100.\n"; 
cin >> val1; 
while (val3 < 0 || val3 > 100) 
{ 
cout << "You have not entered a number between 0 and 100. Please re-enter test 3.\n"; 
cin >> val3; 
}//end of while loop 

cout << "Please enter test 4, with scores between 0 and 100.\n"; 
cin >> val4; 
while (val4 < 0 || val4> 100) 
{ 
cout << "You have not entered a number between 0 and 100. Please re-enter test 4.\n"; 
cin >> val4; 
}//end of while loop 

cout << "Please enter test 5, with scores between 0 and 100.\n"; 
cin >> val5; 
while (val5 < 0 || val5 > 100) 
{ 
cout << "You have not entered a number between 0 and 100. Please re-enter test 5.\n"; 
cin >> val5; 
}//end of while loop 

//display output for validation
cout << "Test 1 is: " << val1 << endl;
cout << "Test 2 is: " << val2 << endl;
cout << "Test 3 is: " << val3 << endl;
cout << "Test 4 is: " << val4 << endl;
cout << "Test 5 is: " << val5 << endl; 


//call to function calcdata 
calcdata();


                                  
return 0; 
} //end of main

//************************************************
//function definition of calcdata 
// this function is going to get the test scores
// array and calculate the averages
// and calculate grade 
//i need to pass the student names to correspond
// with the test scores, avgs, and grade 
//************************************************
void calcdata()

const int NUM_STUDENTS = 5;
const int NUM_SCORES = 4;
double total, average;
double scores[NUM_STUDENTS][NUM_SCORES];

//get each students average score
for (int row = 0; row < NUM_STUDENTS; row++)
{
	//set the accumulator.
	total = 0;

	//sum a row
	for (int col = 0; col < NUM_SCORES; col++
		total += scores[row][column];

	//get the average
	average = total / NUM_SCORES;

	//

//**************************************************
// loop used to determine grade for each students average
//for (int index = 0; index < NUM_NAMES; index++)
//nested loop
//for (int count =0; index < NUM_NAMES; count++)


if (average[0] < 60) 
grade[0] = ‘F’ 
else if (average [0] < 70) 
grade[0] = ‘D’ 
else if (average [0] < 80) 
grade[0] = ‘C’ 
else if (average [0] < 90) 
grade[0] = ‘B’ 
else (average [0] <= 100) 
grade[0] = ‘A’ 

if (average[1] < 60) 
grade[1] = ‘F’ 
else if (average [1] < 70) 
grade[1] = ‘D’ 
else if (average [1] < 80) 
grade[1] = ‘C’ 
else if (average [1] < 90) 
grade[1] = ‘B’ 
else (average [1] <= 100) 
grade[1] = ‘A’ 

if (average[2] < 60) 
grade[2] = ‘F’ 
else if (average [2] < 70) 
grade[2] = ‘D’ 
else if (average [2] < 80) 
grade[2] = ‘C’ 
else if (average [2] < 90) 
grade[2] = ‘B’ 
else (average [2] <= 100) 
grade[2] = ‘A’ 

if (average[0] < 60) 
grade[3] = ‘F’ 
else if (average [3] < 70) 
grade[3] = ‘D’ 
else if (average [3] < 80) 
grade[3] = ‘C’ 
else if (average [3] < 90) 
grade[3] = ‘B’ 
else (average [3] <= 100) 
grade[3] = ‘A’ 

if (average[0] < 60) 
grade[4] = ‘F’ 
else if (average [4] < 70) 
grade[4] = ‘D’ 
else if (average [4] < 80) 
grade[4] = ‘C’ 
else if (average [4] < 90) 
grade[4] = ‘B’ 
else (average [4] <= 100) 
grade[4] = ‘A’ 

//call to function displaydata
displaydata();

return 0;
}//end of function calc data 


//***************************************************
//definition of function displaydata
//this function is designed to display the student's
// average and letter grade
//***************************************************
void displaydata() 

for (int i = 0; i < NUM_SIZE; i++) 
cout << "Student: " <<name [i] 
       <<"average: " << average [i] 
       <<"grade: " << grade [i] 
       << endl; 



return 0; 
} // end of main

P.S. I think it might be important to mention that I'm required to used Visual Studio.NET to do my programs. I couldn't get it to work on my home computer, so I have only been able to try to run my programs while I'm at school, which isn't very often. We also only run them without debugging. It builds, links, compiles and runs. I am on chapter 7 of my Starting Out with C++ 4th Edition Update. I've basically done looping, functions and arrays. We are not suposed to use any information from later chapters. Searching and sorting arrays is my next chapter, so that might give you an idea of where I'm at in my book. I hope this helps. Thank you.

hello madonnamama,
please try and be specific in your problem. whether it is a logical or whatever error you get point it out..try and shorten your program by using more for loops and switch statements..

You would do well to indent it better to make it more readable.

You would do well to indent it better to make it more readable.

I suppose it is NOT required in such a short post!!!

It is READABLE & COMPREHENSIBLE for the NORMAL!!


EDIT::
HEY AHLUKA
I AM SORRY ...REALLY SORRY :o FOR WHAT I TYPED...IN A HASTE I MISUNDERSTOOD IT FOR MY POST ..M REALLY SORRY!! MY MISTAKE..AND YOU MADONNAMAMA YOU SHOULD SURELY INDENT THE CODE!!! :cool:

Oh of course it is.
The "beginning of for loop" and "end of for loop" on the same line as the brackets is really good commenting too. I mean the loop is so short, how would I ever have noticed where it begins and ends.

Badly formatted code is one sure-fire way to shoot yourself in the foot, as is over-commenting.

I'm sorry about the indenting problem. I originally had the program written in word with indents and when I copied it to notepad so that I could open it from my email at school all the indents disappeared.

I also added the beginning of and end of loop comments, because my last program had so many brackets, I started getting confused as to what loops they belonged to. So I started adding the comments to prevent my confusion, not to annoy other readers.

I don't know how to change the if, else statement to a for loop.

When I have school tomorrow, I will check to see what errors I get. I haven't tried to run the program yet because I don't think I'm writing the code right to begin with.

You should probably be compiling and working on it at home. Dev-C++ is a good free compiler.

Sorry if you already are and I misread it.

I'm sorry about the indenting problem. I originally had the program written in word with indents and when I copied it to notepad so that I could open it from my email at school all the indents disappeared.

Dont use MS=Word to write code. Most of the the text formatting information in MS-word are lost when you copy t hem to notepad or another text editor.
Dont use notepad to write code unless you are writing a hello world program. If you are using Visual Studio there is a very good code editor in it that supports syntax highlighting, auto indenting... If you dont have Visual Studio, Dev-Cpp is a good free compiler with a code editor. Personally I use a seperate editor to write code as it loads quickly compared to onethat comes in a Development Environment. I use the IDE only for compiling, and even that I do in the command line when possible, but that depends on personal taste. Some good free editors that with syntax highlighting and auto indenting are Notepad++, TextPad. These are good even when you are programming in another language as most of the document classes are supported in them.

I also added the beginning of and end of loop comments, because my last program had so many brackets, I started getting confused as to what loops they belonged to. So I started adding the comments to prevent my confusion, not to annoy other readers.

If you use Auto indenting you will not get confused on what bracket belongs to what loop. Usually the starting brace should be in the same vertical line as the ending bracket. Then no confusion. As for indenting: there should be an option that says "replace tabs with spaces". If you set this option to on, then the indenting information will not be lost even if you open it in another text viewer. I have see that even if you use

tags the indenting goes crazy when it is done using tabs.

I don't know how to change the if, else statement to a for loop.

This is not possible and an unnecessary attempt. An if else statement is for condition checking. A For loop is for repetition.

Wolfpack has made an excellent point I think, especially about loads times when it comes to IDE's. Still I use one because I like all the nice features but could do without the 40 second wait while it loads larger projects :(

I want to thank you so much for your very useful comments. I actually have two cd's that came with my book. One is: "Borland Visual C++ Builder, v.5" and the other one is: Microsoft Visual C++ 6.0 Introductory Edition". I didn't realize that I could compile with another program besides the .NET, I figured it might have different errors, since I noticed how picky the .NET can be. I will install the Borland, since it seems like a full version.

Could you tell me what an IDE is? I'm guessing the Development Environment has something to do with it, but I don't know what that is either.

I got the chance to compile and work on my project in school with the help of several classmates, so I've been able to narrow down my issues to specifics. After I install the Borland I will try to get the .cpp file to open in it and then I can send a message with my specific question.

Ok, i installed the Borland and have it working. The tab to spacing is automatic selected but I found the option so I know it's on. I will try do make my problem clear and highlighted in red. I've also highlighted areas that are not a requirement of the program. The code is much shorter than the original.

#include <conio.h>      //for the getch() function (for pausing in Borland)
#include <iostream>
using namespace std;

const int NUM_NAMES = 5;          //how many occurances
const int NAMESIZE = 11;          //how long names can be, 10 letters
const int NUM_TESTS = 4;          //how many tests
char name[NUM_NAMES][NAMESIZE];   //two-dimensional name array
char grade[5];            //grade letter for each student array
double testscore[NUM_NAMES][NUM_TESTS];          //test score for each student
double average[NUM_NAMES];        //average for each student array


//function prototype
void calcdata(int, double[][NUM_TESTS]);


//start of main
int main()
{



cout <<  "Enter the student's name. \n";
for (int count = 0; count < NUM_NAMES; count++)
{
	cout << "Student  " << (count +1) <<": ";
	cin >> name[count];
}
	for (int student = 0; student < NUM_NAMES; student++)
	{
		for (int testnum = 0; testnum < NUM_TESTS; testnum++)
		{
			cout << "what is the test score for " << name[student];
			cout << " for test:" << testnum+1<< endl;
			cin >> testscore[student][testnum];

	
			while (testscore[student][testnum] < 0 || testscore[student][testnum] > 100)
			{
				cout << "Please enter a number between 0 and 100" << endl;
				cin >> testscore[student][testnum];
		
			}
		}
	}
                 //this section was just to validate data was imputting correctly	//display test scores for each student
	for (int i = 0; i < NUM_NAMES; i++)
	{
		for (int y = 0; y < NUM_TESTS; y++)
		{
			cout << testscore[i][y] << endl;
		}
	}  
//call to function calcdata
	calcdata(NUM_NAMES, testscore);

getch(); //for pause

return 0;
} //end of main

//************************************************
//function definition of calcdata
// this function is going to get the test scores
// array and calculate the averages
// and calculate grade
//i need to pass the student names to correspond
// with the test scores, avgs, and grade
//************************************************
void calcdata(int NUM_NAMES, double testscore[][NUM_TESTS])
{
	double total, average;
//get each students average score
for (int row = 0; row < NUM_NAMES; row++)
{
	//set the accumulator.
	total = 0;

	//sum a row
	for (int col = 0; col < NUM_TESTS; col++)
	
		total += testscore[row][col];
                 //added to validate data   
	//display total of scores
		cout << "Score total for student "
			<< (row + 1) << " is " << total << endl;
		//get the average
		average = total / NUM_TESTS;

                                 //added to validate data		//display the average
		cout << "Score average for student "
			<< (row + 1) << " is " << average << endl;	
		//get the grade
		if (average < 60)
			grade[row] = 'F';
		else if (average < 70)
			grade[row] = 'D';
		else if (average < 80)
			grade[row] = 'C';
		else if (average < 90)
			grade[row] = 'B';
		else if (average < 100)
			grade[row] = 'A';
			
			//added to validate data                                             //display the grade
		cout<< "The grade for student "
			<< (row + 1) << " is " << grade[row] << endl;}


//display data: student name with corresponding average and grade
for (int i = 0; i < 5; i++)
     cout << "Student: " << name[i]

      //  <<"average: " << average[i]     

            <<"grade: " << grade[i]
            << endl;



} //end of getdata

When I take out the comment slashes in front of the average the error says that I need a pointer or array(.NET error). borland error was similar, but didn't mention array (I can't use pointers yet, so array it is) i tried to change it to an array by adding NUM_NAMES in brackets to the definition:

void calcdata(int NUM_NAMES, double testscore[][NUM_TESTS])
{
	double total, average[NUM_NAMES];
//get each students average score
for (int row = 0; row < NUM_NAMES; row++)

and it told me the line I changed needed a constant expression, so I changed it to 5 (i don't understand why I would have to change that) and it removed that error.

but the other errors that remain, I don't know how to fix. they are as follows:

illegal use of floating point for line marked in red:

//get the average
		average = total / NUM_TESTS;

cannot convert int to double for lines marked in red:

//get the grade
		if (average < 60)
			grade[row] = 'F';
		else if (average < 70)

			grade[row] = 'D';
		else if (average < 80)

			grade[row] = 'C';
		else if (average < 90)

			grade[row] = 'B';
		else if (average < 100)

			grade[row] = 'A';

can figure out what these errors mean?

I compiled your program ( The one in your first post ) under the Visual Studio 2003 compiler and ran it. There was no problems for me. None of the changes in your last post were needed.

when I ran the first program I received quite a few compile errors. some things in it weren't needed for the program so I took them out. maybe I should keep the displaydata function and call it from get data to get my display to work the way it's required.

(I can't use pointers yet, so array it is)

>>actually later you will learn that array is a type of constant pointer..no need to worry right now.

and it told me the line I changed needed a constant expression, so I changed it to 5 (i don't understand why I would have to change that) and it removed that error.

>>when you define an array it reqiures the no of elements at the time of compilation.. that is u need to define array statically..not possible at runtime or dynamically..we have another method which you will learn later..
sanket

I tried and ran your program in my compiler which is turbo c++... your second code and it ran perfectly...

I fixed the code so that it would output the student name, average score, and grade letter. All I had to do was to use the average array the same way as the grade array in the last section of the code. So here is the final code as follows: Thanks for the help.

#include <conio.h>      //for the getch() function (for pausing in Borland)
#include <iostream>
using namespace std;

const int NUM_NAMES = 5;          //how many occurances
const int NAMESIZE = 11;          //how long names can be, 10 letters
const int NUM_TESTS = 4;          //how many tests
char name[NUM_NAMES][NAMESIZE];   //two-dimensional name array
char grade[5];            //grade letter for each student array
double testscore[NUM_NAMES][NUM_TESTS];          //test score for each student
double average[5];        //average for each student array


//function prototype
void calcdata(int, double[][NUM_TESTS]);


//start of main
int main()
{



cout <<  "Enter the student's name. \n";
for (int count = 0; count < NUM_NAMES; count++)
{
	cout << "Student  " << (count +1) <<": ";
	cin >> name[count];
}
	for (int student = 0; student < NUM_NAMES; student++)
	{
		for (int testnum = 0; testnum < NUM_TESTS; testnum++)
		{
			cout << "what is the test score for " << name[student];
			cout << " for test:" << testnum+1<< endl;
			cin >> testscore[student][testnum];

	
			while (testscore[student][testnum] < 0 || testscore[student][testnum] > 100)
			{
				cout << "Please enter a number between 0 and 100" << endl;
				cin >> testscore[student][testnum];
                    cout << endl;
		
			}
		}
	}


//call to function calcdata
	calcdata(NUM_NAMES, testscore);

getch(); //for pause

return 0;
} //end of main

//************************************************
//function definition of calcdata
// this function is going to get the test scores
// array and calculate the averages
// and calculate grade
//i need to pass the student names to correspond
// with the test scores, avgs, and grade
//************************************************
void calcdata(int NUM_NAMES, double testscore[][NUM_TESTS])
{
	double total;
//get each students average score
for (int row = 0; row < NUM_NAMES; row++)
{
	//set the accumulator.
	total = 0;

	//sum a row
	for (int col = 0; col < NUM_TESTS; col++)
	
		total += testscore[row][col];

		//get the average
		average[row] = total / NUM_TESTS;

	
		//get the grade
		if (average[row] < 60)
			grade[row] = 'F';
		else if (average[row] < 70)
			grade[row] = 'D';
		else if (average[row] < 80)
			grade[row] = 'C';
		else if (average[row] < 90)
			grade[row] = 'B';
		else if (average[row] < 100)
			grade[row] = 'A';
			

}



for (int i = 0; i < 5; i++)
cout << "Student: " << name[i]
      <<" average: " << average[i]
       <<" grade: " << grade[i]
       << endl;



} //end of getdata
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.