get average grade for 10 students c++ - c++

This program will need to handle all grades for 10 students.
Each student has a first and last name, an id number,
3 homework grades, 3 labs grades, 3 test grades, and 1 final exam grade.
display the final course grade in both numbered and letter version
The equation only works for the first set of grades then it adds a little bit to the next average I just can't figure out whats wrong.
/*This program will need to handle all grades for 10 students.
Each student has a first and last name, an id number,
3 homework grades, 3 labs grades, 3 test grades, and 1 final exam grade.
display the final course grade in both numbered and letter version
*/
#include <iostream> // main library
#include <string> //enables use of strings
#include <iomanip> // for setw
#include <Windows.h> // used to set console title
using namespace std; // for cout and cin
const int MAXSTUDENTS = 2;
const int MAXGRADES = 3;
const int MINGRADES = 1;
int main()
{// Get names form students
SetConsoleTitle("Gradebook");
double hwGrade[MAXSTUDENTS][MAXGRADES];
double labGrade[MAXSTUDENTS][MAXGRADES];
double testGrade[MAXSTUDENTS][MAXGRADES];
double feGrade[MAXSTUDENTS];
double final_num_grade[MAXSTUDENTS];
double hwAve =0, labAve=0, testAve=0; // this will be used to calculate the averages
string fName[MAXSTUDENTS];
string lName[MAXSTUDENTS];
string line; // to set the two string variables
string id[MAXSTUDENTS]; // id will be a whole number so int was apropiate
//first for statement. ensuere that the program is run 10 times
for (int s = 0; s < MAXSTUDENTS; s++) {
cout << "Enter student's first name: "; // ask the user for the first name
getline(cin, fName[s]); // accepts students first name
cout << "Enter stedent's last name: "; //ask the user for last name
getline(cin, lName[s]); // accepts students last name
cout << "Enter student's id: "; // ask user for student id
getline(cin, id[s]);
// this loop will ask for three homework grades
for (int n = 0; n < MAXGRADES; n++) {
cout << "Homework grade " << n + 1 << " is "; //tells the user waht the program needs
cin >> hwGrade[s][n]; //lets the user input the homework grades
hwAve += hwGrade[s][n];
}
hwAve = hwAve / 3;
// this loop will ask for three lab grades
for (int l = 0; l < MAXGRADES; l++) {
cout << "Lab grade " << l + 1 << " is ";
cin >> labGrade[s][l]; //lets the user input the LAB grades
labAve += labGrade[s][l];
}
labAve = labAve / 3;
//this loop will ask for three test grades
for (int t = 0; t < MAXGRADES; t++) {
cout << "Test grade " << t + 1 << " is ";
cin >> testGrade[s][t]; //lets the user input the test grades
testAve += testGrade[s][t]; // the average is calculated
}
testAve = testAve / 3;
cout << "Final exam grade: "; // asks user for final exam grade
cin >> feGrade[s];
// equation to get the final course grade
final_num_grade[s] = (hwAve * 0.20) + (labAve * 0.25) + (testAve * 0.30) + (feGrade[s] * 0.25);
line.assign(50, '-');
cout << line << endl;
}
for (int i = 0; i < MAXSTUDENTS; i++) {
cout << "Final Course Grade for " << fName[i] << " " << lName[i] << " with the id " << id[i] << " is " // displays name of student
<< showpoint << fixed << setprecision(1) << final_num_grade[i]; //set to 1 decimal place
//if statement shows the letter grade
if (final_num_grade[i] >= 89.5) { //A if student made 89.5 or more
cout << " (A)\n";
}
else if (final_num_grade[i] >= 79.5) { //B if student made 79.5 to 89.4
cout << " (B)\n";
}
else if (final_num_grade[i] >= 69.5) { // C if student made 69.5 yo 79.4
cout << " (C)\n";
}
else if (final_num_grade[i] >= 59.5) { // D if student made 59.5 to 69.4
cout << " (D)\n";
}
else { // F if student made less than 59.4
cout << " (F)\n";
}
}
return 0;
}

You haven't reset these variables to zero : hwAve, labAve, testAve which makes the second student's grade will be slightly higher

Related

Nested loops in C++ and user input

Pretty new here to programming, and I have an assignment where I need to achieve the following:
ask for total amount of people
get each of their names
allow user to enter up to 5 scores for each person
if there are less than 5 scores for a given person, inputting -100 will stop it
So far I have written this:
#include <iostream>
using namespace std;
int main() {
string personName;
int totalPerson, personScoreCounter;
double personGrade, personGradeTotal;
cout << "Input total amount of people: ";
cin >> totalPerson;
for (int person = 1; person <= totalPerson; person++)
{
cout << "Input name for person " << person << ": ";
getline(cin, personName);
cin.ignore();
while ( (personGrade != -100) && (personScoreCounter <= 5) )
{
cout << "Input up to 5 scores for " << personName << " (-100 to end): ";
cin >> personGrade;
if (personGrade >= 0 && personGrade <= 100) // valid range of scores
{
personGradeTotal += personGrade;
personScoreCounter++;
}
else
{
cout << "Input only scores from 0-100" << endl;
}
cout << "Input up to 5 scores for " << personName << " (-100 to end): ";
cin >> personGrade;
}
}
// calculate averages and other stuff in here.
return 0;
}
After getting their name, only the last cout inside the while loop seems to execute first, then it starts from the top and so on until the for loop hits the end depending on totalPerson. I know I'm missing a few things in here, probably in the order of operations and also the way I am executing my loops, but I just can't see it. Could any of you guys with experience in the language please give me any pointers as to what's happening here and how I can fix it? Thank you.
Inside your while group, you only want to use your cout line once (at the beginning looks good).
Your first check should be for ==-100 or similar, since as it is now, you'll get a "Input only scores from 0 to 100" message if you enter -100.
You should keep a cin.ignore(); call after each use of cin >> VARIABLE, since then you will drop the EoL character.
Example code:
#include <iostream>
using namespace std;
int main() {
int totalPerson;
cout << "Input total number of people: ";
cin >> totalPerson;
cin.ignore();
for (int person = 1; person <= totalPerson; person++)
{
int personScoreCounter=0;
double personGrade = -1, personGradeTotal=0;
string personName;
cout << "Input name for person " << person << ": ";
std::getline(cin, personName);
while ( (personGrade != -100) && (personScoreCounter < 5) )
{
cout << "Input up to 5 scores for " << personName << " (-100 to end): ";
cin >> personGrade;
cin.ignore();
if (personGrade == -100) {
break;
} else if (personGrade >= 0 && personGrade <= 100) {
personGradeTotal += personGrade;
personScoreCounter++;
} else {
cout << "Input only scores from 0-100" << endl;
}
}
// calculate averages and other stuff in here.
double avg = personGradeTotal / personScoreCounter;
cout << "Avg = " << avg << endl;
}
return 0;
}
Some of your variables also needed to move inside the for loop.
Additionally I changed the limits on the personScoreCounter to [0:4] rather than [1:5] - this way you can use it for averaging more easily.
You might also try cin.getline() instead of getline(std::cin , ... ):
int max_length = 30;
std::cin.getline(personName, max_length, '\n'); // \n is option termination.
This allows whitespaces in the input also.
http://www.cplusplus.com/reference/istream/istream/getline/

Average, Maximum and Minimum assignment for C++

My issue lies in the minimum function toward the end and the program pulls from a fill of about 25 names with 5 grades each. The average and maximum are running smoothly but I can't seem to hammer out the minimum. I managed to get it to pull a few names but then the program crashes. Can anyone help me understand what I am overlooking here? Please and thank you.
If you plug it in a compiler the min function is located between the 200-250 lines and a simple txt file is used to pull names and grades from.
// Headers
#include <iostream> // cout, cin
#include <cstdlib> // exit()
#include <string> // strings
#include <fstream> // file processing
#include <iomanip> // stream manipulation
using namespace std;
// Global variables
const int MAX_STUDENTS = 25; // We will not process more than 25 students even if the file contains more
const int MAX_GRADES = 5; // Each student has exactly 5 grades
const string FILENAME = "NamesGrades.txt"; // The name of the file that you will read
// Function declarations
int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int maxStudents);
void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount);
void displayMax(string students[], int grades[][MAX_GRADES], int studentCount);
void displayMin(string students[], int grades[][MAX_GRADES], int studentCount);
string getLetterGrade(double grade);
int getLongestNameLength(string students[], int studentCount);
int main()
{
int studentCount = 0; // You need one to keep up with the actual number of students
int grades[MAX_STUDENTS][MAX_GRADES]; // You need a two dimensional arry for the grades of the students
string students[MAX_STUDENTS]; // You need and array of strings for the student names
char choice; // You need a variable to hold the choice of the user for the menu
// Get students and grades
studentCount = loadStudentNamesGrades(students, grades, FILENAME, MAX_STUDENTS);
// Loop until user says to quit (Do loop suggested)
do
{
// present menu and get user's choice
cout << "\nGrade Report Program\n\n";
cout << "\t1. Display Average Grade\n";
cout << "\t2. Display Maximum Grade\n";
cout << "\t3. Display Minimum Grade\n";
cout << "\t4. Quit Program\n";
cout << "\nEnter your choice (1-4): ";
cin >> choice;
// Process the choice
switch( choice )
{
case '1': // Average
displayAverages(students, grades, studentCount);
break;
case '2': // Maximum
displayMax(students, grades, studentCount);
break;
case '3': // Minimum
displayMin(students, grades, studentCount);
break;
case '4': // Quit
break;
default:
cout << "Invalid option.\n\n";
}
if(choice != '4' )
{
cout << endl;
system("PAUSE");
system("CLS");
}
} while( choice != '4' );
cout << endl;
return 0;
}
/***********************************************************
loadStudentNameGrades opens and read fileName. It will read in two strings, concatenate them, and then save
to the students array. It then reads five integers and save each to the grades array. The function will return
the actual number of student/grade combinations read
PARAM: students is an array of strings that can hold up ot maxStudents values
grades is a two dimensional array for holding the grades of each student
fileName is the name of the file that will be opened and read
maxStudents is the maximum number of students that we will read from the file
PRE: students[] is large enough to contain up to maxStudents elements
grades[] is large enough ot contain up to maxStudents elements
POST: students[] contains the names of up to maxStudents
grades[][] contains the grades for up to maxStudents
The number of student/grade combinations actually read from the file is returned. This value can range
between 0 <= numStudents <= maxStudents
NOTE: students[] and grades[] are meant to be parralel arrays. students[0] and grades[0] are the same student
************************************************************/
// Check 16:15
int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int maxStudents)
{
ifstream inFile; // input file stream
string studentsName, letterGrade; // Name of the student and grade character (char)
int numStudents = 0; // number of students actually read
// Open the file
inFile.open(fileName.c_str());
if( inFile.fail())
{
cout << "Could not open file" << endl;
system("PAUSE");
exit(1);
}
for( int i = 0; i < maxStudents && (inFile >> studentsName >> letterGrade); i++, numStudents++)
{
for( int j = 0; j < MAX_GRADES; j++ )
{
inFile >> grades[i][j];
}
students[i] = studentsName + " " + letterGrade;
}
inFile.close();
return numStudents;
}
/***********************************************************
displayAverages calculates the average of each student and displays the
students name, average, and letter grade of the average in a table
PARAM: students[] is an array of strings that contains the names of studentCount students
grades[] is an array of integers that contains the grades of studentCount students
studentCount contains the value of the number of elements in the students[] and grades[] arrays
PRE: students[] and grades[] contain values for studentCount elements
POST: table of student names, averages, and letter grades is displayed
************************************************************/
// Check 16:15
void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount)
{
double average; // Average grades of students
int total; // total of all grades (accumulator)
int maxLength = getLongestNameLength(students,studentCount);
cout << setprecision(1) << fixed << showpoint;
// Setup table header
cout << "\n\nGrade Averages\n";
cout << setw(maxLength + 1) << left << "Name" << setw(4) << right << "Average" << setw(6) << "Grade" << endl;
for( int i = 0; i < studentCount; i++ )
{
cout << setw(maxLength + 1) << left << students[i];
total = 0;
for( int j = 0; j < MAX_GRADES; j++ )
{
total += grades [i][j];
}
average = (double)total / MAX_GRADES;
cout << setw(7) << right << average << setw(6) << getLetterGrade(average) << endl;
}
}
/***********************************************************
displayMax calculates the maximum grade of each student and displays the
students name, maximum grade, and letter grade of the maximum grade in a table
PARAM: students[] is an array of strings that contains the names of studentCount students
grades[] is an array of integers that contains the grades of studentCount students
studentCount contains the value of the number of elements in the students[] and grades[] arrays
PRE: students[] and grades[] contain values for studentCount elements
POST: table of student names, maximum grades, and letter grades is displayed
************************************************************/
// Check 16:15
void displayMax(string students[], int grades[][MAX_GRADES], int studentCount)
{
int maxGrade;
int maxLength = getLongestNameLength(students,studentCount);
cout << "\n\nGrade Maximums\n";
cout << setw(maxLength + 1) << left << "Student" << setw(4) << right << "Max" << setw(6) << "Grade" << endl;
for( int i = 0; i < studentCount; i++ )
{
cout << setw(maxLength + 1) << left << students[i];
maxGrade = 0;
for( int j = 0; j < MAX_GRADES; j++ )
{
if(maxGrade < grades [i][j]) maxGrade = grades [i][j];
}
cout << setw(4) << right << maxGrade << setw(6) << getLetterGrade(maxGrade) << endl;
}
}
/***********************************************************
displayMin calculates the minimum grade of each student and displays the
students name, minimum grade, and letter grade of the minimum grade in a table
PARAM: students[] is an array of strings that contains the names of studentCount students
grades[] is an array of integers that contains the grades of studentCount students
studentCount contains the value of the number of elements in the students[] and grades[] arrays
PRE: students[] and grades[] contain values for studentCount elements
POST: table of student names, minimum grades, and letter grades is displayed
************************************************************/
/* Notes:
Program seems to pull 2 names and freeze on the third. Unsure as to why?
Assigns Leslie Carter 36C and code seems to break down afterwords.
Issue is undetected any where else and seems to be isolated in this function.
*/
void displayMin(string students[], int grades[][MAX_GRADES], int studentCount)
{
int minGrade;
int maxLength = getLongestNameLength(students,studentCount);
cout << "\n\nGrade Minimums\n";
cout << setw(maxLength + 1) << left << "Student" << setw(4) << right << "Min" << setw(6) << "Grade" << endl;
for( int i = 0; i < studentCount; i++ ) // > is null result, < is result
{
cout << setw(maxLength + 1) << left << students[i];
minGrade = 101; // Produced a result in the correct direction
for( int j = 0; j < MAX_GRADES; j++ )
{
if(minGrade > grades [i][j]) minGrade = grades [i][j]; // Produced a result in the correct direction
}
cout << setw(4) << right << minGrade << setw(6) << getLetterGrade(minGrade) << endl;
}
}
/***********************************************************
getLetterGrade converts a numerical grade to a letter grade
PARAM: grade is the numerical grade to convert. Expected range is 0 <= grade <= 100
PRE: grade contains a value in the correct range
POST: The corresponding letter grade of the numerical grade is returned
************************************************************/
// Check 16:15
string getLetterGrade(double grade)
{
if(grade > 90 )
return "A";
else if( grade > 80)
return "B";
else if( grade > 70)
return "C";
else if( grade > 60)
return "D";
else if( grade > 50)
return "F";
}
/***********************************************************
getLongestNameLength returns the length of the longest string from a list of strings
PARAM: students[] is an array of strings that contains the name of students
studentCount is the size of the students[] array
PRE: students[] contains studentCount names
POST: The length of the longest string in students[] is returned
************************************************************/
// Check 16:15
int getLongestNameLength(string students[], int studentCount)
{
int maxLength = 0;
for( int i = 0; i < studentCount; i++)
{
if( students[i].length() > maxLength ) maxLength = students[i].length();
}
return maxLength;
}
Your getLetterGrade function doesn't return anything when grade is less than 50. Try to build with as many warnings turned on as possible. -Wall would have warned you about this mistake.

Parallel array in C++

I have an assignment where we need to have 2 parallel arrays one is a list of city names and the other is sales amounts. Here is a copy of the problem:
Program Description:
It needs to compile sales totals for various cities in the USA. Specifically, when the program is run, the user will be prompted to enter a city. If the city is correct, the user will then be prompted to enter a sales amount. If the city doesn’t exist on the list, the user will get an error message (and no sales amount prompt). If a sales amount is entered, it will accumulate into a total for that city. Either way (city exists on the list or not) , the user will then be asked to enter another city or quit.
Once the user quits, the city name and total should be displayed for all cities, one per line. Following that the program should stop.
There are only 8 cities to choose from. 2 parallel arrays must be used, initialized as follows:
City (String) Sales (Integer)
------------- ---------------
Atlanta 0
Buffalo 0
Chicago 0
Dallas 0
Houston 0
Honolulu 0
Miami 0
Reno 0
All input is guaranteed to be single-word followed by enter only. It may not match a city name, but there will be no spaces. This keeps your program simple as it lets you avoid using getline( ), which would be needed to deal with blanks between words.
Sales data is guaranteed good when input.
When I attempted to run my program, visual studios went crazy, and I've pulled out my hair trying to fix it. If someone could help give me some pointers on what I've done wrong, I would greatly appreciate it. Here is a copy of my program:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//declare city and sales array
string city[8] = {" "};
int sales[8] = {0};
//declare variables
string cityName = " ";
int cityTotal = 0;
int salesAmt = 0;
int i = 0;
char another = ' ';
//init city array
city[0] = "Atlanta";
city[1] = "Buffalo";
city[2] = "Chicago";
city[3] = "Dallas";
city[4] = "Houston";
city[5] = "Honololu";
city[6] = "Miami";
city[7] = "Reno";
do
{
//input city name and if found input sales amount
cout << "Enter a City: Atlanta, Buffalo, Chicago, Dallas, Houston, Honololu, Miami, or Reno: ";
cin >> cityName;
for(i = 0; i <= 8; i++)
{
if(cityName == city[i])
{cout << "Enter sales amount: ";
cin >> salesAmt;
salesAmt += sales[i];}
else
{cout << "ERROR: CITY NOT AVAILIABLE" << endl;
cout << endl;}
//end if
}
//end for loop
//ask if another city
cout << "Enter another city?: ";
cin >> another;
} //end do loop
while(another == 'Y' || another == 'y');
{
for(i = 0; i <= 8; i++)
{
cout << "City: " << " " << "Sales: " << endl;
cout << city[i] << " " << sales[i] << endl;
}
//end for loop
} //end while loop
system("pause");
return 0;
} //end of main
A clear error here is the way you have used the index to access the arrays, you can't have the for loop reach 8, as the array's index is only up to 7. change your for loops to:
for(i = 0; i < 8; i++)
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
//declare city and sales array
string city[8] = {" "};
int sales[8] = {0};
//declare variables
string cityName ="";
int cityTotal = 0;
int salesAmt = 0;
int i = 0;
char another = ' ';
//init city array
city[0] = "Atlanta";
city[1] = "Buffalo";
city[2] = "Chicago";
city[3] = "Dallas";
city[4] = "Houston";
city[5] = "Honololu";
city[6] = "Miami";
city[7] = "Reno";
do
{
//input city name and if found input sales amount
cout << "Enter a City: Atlanta, Buffalo, Chicago, Dallas, Houston, Honololu, Miami, or Reno: ";
cin >> cityName;
for(i = 0; i < 8; i++)
{
if(cityName == city[i])
{
cout << "Enter sales amount: ";
cin >> salesAmt;
sales[i] += salesAmt;
} else if (i==7)
{
cout << "ERROR: CITY NOT AVAILIABLE" << endl;
}//end if
}//end for loop
//ask if another city
cout << "Enter another city?: ";
cin >> another;
} //end do loop
while(another == 'Y' || another == 'y');
{
for(i = 0; i < 8; i++)
{
cout << "City: " << " " << "Sales: " << endl;
cout << city[i] << " " << sales[i] << endl;
}
//end for loop
} //end while loop
system("pause");
return 0;
} //end of main
error was for(i = 0; i <= 8; i++) change with for(i = 0; i < 8; i++) and also second for.
Next error changed to sales[i] += salesAmt; and Not salesAmt +=sales[i];.
And your city name is case sensitive when you input city name!

Trouble with an if else if statement c++

I am working on a grade book project that has 5 students that I want to read the names in for and then with an inner loop grab 4 grades for each student. Something is not working on this loop. This what I am getting:
Please enter the name for student 1: Dave
Please enter the grade number 1 for Dave: 100
Please enter the grade number 2 for Dave: 100
Please enter the grade number 3 for Dave: 100
Please enter the grade number 4 for Dave: 10
Please enter the name for student 2: James
Please enter the grade number 5 for James: 100
Please enter the name for student 3: Sam
Please enter the grade number 5 for Sam: 100
Please enter the name for student 4: Jack
Please enter the grade number 5 for Jack: 100
Please enter the name for student 5: Mike
Please enter the grade number 5 for Mike: 100
It should grab 4 grades before it jumps to the next student. I have not been able to figure this out for the last couple hours. Here is the code I have so far:
#include <iostream>
#include <string>
using namespace std;
const int STUDENTS = 5; //holds how many students we have
const int SCORES = 4;
void getNames(string names[], double student1[SCORES], double student2[SCORES],
double student3[SCORES], double student4[SCORES], double student5[SCORES], int SCORES, int STUDENTS);
int main()
{
string names[STUDENTS] = {""};
char grades[STUDENTS] = {""};
double student1[SCORES] = {0};
double student2[SCORES] = {0};
double student3[SCORES] = {0};
double student4[SCORES] = {0};
double student5[SCORES] = {0};
getNames(names, student1, student2, student3, student4, student5, SCORES, STUDENTS);
// Make sure we place the end message on a new line
cout << endl;
// The following is system dependent. It will only work on Windows
system("PAUSE");
return 0;
}
void getNames(string names[], double student1[SCORES], double student2[SCORES],
double student3[SCORES], double student4[SCORES], double student5[SCORES], int SCORES, int STUDENTS)
{
for (int i = 0; i < STUDENTS; i++)
{
cout << "Please enter the name for student " << i+1 << ": ";
cin >> names[i];
cout << endl;
if (i == 0)
{
int count1 = 0;
for (count1; count1 < SCORES; count1++)
{
cout << "Please enter the grade number " << count1+1 << " for " << names[i] <<": ";
cin >> student1[count1];
cout << endl;
}
}
else if (i == 1)
{
int count2 = 0;
for (count2; count2 < SCORES; count2++);
{
cout << "Please enter the grade number " << count2+1 << " for " << names[i] <<": ";
cin >> student2[count2];
cout << endl;
}
}
else if (i == 2)
{
int count3 = 0;
for (count3; count3 < SCORES; count3++);
{
cout << "Please enter the grade number " << count3+1 << " for " << names[i] <<": ";
cin >> student3[count3];
cout << endl;
}
}
else if (i == 3)
{
int count4 = 0;
for (count4; count4 < SCORES; count4++);
{
cout << "Please enter the grade number " << count4+1 << " for " << names[i] <<": ";
cin >> student4[count4];
cout << endl;
}
}
else
{
int count5 = 0;
for (count5; count5 < SCORES; count5++);
{
cout << "Please enter the grade number " << count5+1 << " for " << names[i] <<": ";
cin >> student5[count5];
cout << endl;
}
}
}
}
Thanks for any help on this!
There's some pretty rough stuff going on in here, but the problem is that you have a semi-colon on all your inner loops except the first one:
for (count2; count2 < SCORES; count2++);
Remove the semi-colon, and the stuff in the braces will become part of the loop.
I'm going to suggest you make your code a little tidier and less error-prone by chucking all those function arguments into their own array when you enter the function, like this:
double *scores[5] = { student1, student2, student3, student4, student5 };
Then you take OUT all that repetition - the copy/paste is what caused your problems to begin with:
for (int i = 0; i < STUDENTS; i++)
{
cout << "Please enter the name for student " << i+1 << ": ";
cin >> names[i];
cout << endl;
for (int s = 0; s < SCORES; s++)
{
cout << "Please enter the grade number " << s+1 << " for " << names[i] <<": ";
cin >> scores[i][s];
cout << endl;
}
}
Why can't you use two nested loops like
for (int studix=0, stduix<STUDENTS; studix++) {
//...
for (int gradix=0; gradix<SCORE; gradix++) {
//...
}
//....
}
BTW, the condition could be a more complex one, e.g. with the internal loop being
bool goodgrade=true;
for (int gradix=0; goodgrade && gradix<SCORE; gradix++) {
// you could modify goodgrade or use break; inside the loop
}
Don't forget the possible use of continue and break inside a loop.
And please, take time to read some good C++ programming book
Building on Basile's answer and my comments:
int main()
{
string names[STUDENTS] = {""};
char grades[STUDENTS] = {""};
double student1[SCORES] = {0};
double student2[SCORES] = {0};
double student3[SCORES] = {0};
double student4[SCORES] = {0};
double student5[SCORES] = {0};
double *gradeArray[STUDENTS];
gradeArray[0] = student1;
gradeArray[1] = student2;
gradeArray[2] = student3;
gradeArray[3] = student4;
gradeArray[4] = student5;
for (int studix=0, stduix<STUDENTS; studix++) {
// get the name of the student
for (int gradix=0; gradix<SCORE; gradix++) {
// put the grades in gradeArray[studix][gradix]...
}
//....
}
Yes, I know about 2 D arrays, but I am trying to make explicit how this can be done with "five individual arrays". Clumsy, but I believe this works.

Counter not working?

I am writing a program for a homework assignment that calculates rental car rates based on make, days rented and miles driven. Overall the program works except, when the user is prompted for the number of cars to be calculated, the program continues to prompt the user for input after the number has been exceeded. Also, the formatting for the miles is correct for the first vehicle entered but changes for subsequent entries.
Any help with these two issues would be greatly appreciated!
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
// Change the console's background color.
system ("color F0");
// Declare the variables.
char carType;
string brand, f("Ford"), c("Chevrolet");
int counter = 0, cars = 0;
double days, miles, cost_Day, cost_Miles, day_Total;
cout << "Enter the number of cars you wish to enter: ";
cin >> cars;
cin.ignore();
while (counter <= cars)
{
cout << "Enter the car type (F or C): ";
cin >> carType;
cin.ignore();
cout << "Enter the number of days rented: ";
cin >> days;
cin.ignore();
cout << "Enter the number of miles driven: ";
cin >> miles;
cin.ignore();
if (carType == 'F' || carType == 'f')
{
cost_Day = days * 40;
cost_Miles = miles * .35;
day_Total = cost_Miles + cost_Day;
brand = f;
}
else
{
cost_Day = days * 35;
cost_Miles = miles * .29;
day_Total = cost_Miles + cost_Day;
brand = c;
}
cout << "\nCar Days Miles Cost\n";
cout << left << setw(12) << brand << right << setw(6) << days << right << setw(8) << miles
<< fixed << showpoint << setprecision (2) << setw(8) << right << "$" << day_Total << "\n\n";
counter++;
}
system ("pause");
}
You have started counting from 0 int counter = 0, cars = 0;
You then count until you are equal to the number that was entered (the "or equal to" bit of while (counter <= cars)).
As a worked example, if I want 3 entries:
Start: counter = 0, cars = 3.
0 <= 3: true
End of first iteration: counter = 1
1 <= 3: true
End of second iteration: counter = 2
2 <= 3: true
End of third iteration: counter = 3
3 <= 3: true (the "or equal" part of this)
End of FORTH iteration: counter = 4
4 <= 3: false -> Stop
We have completed 4 iterations instead of 3. If we only checked for "strictly less than" (counter < cars), the condition at the end of the third iteration would be false, and we'd have ended there.
The heading of your while loop should be:
while(counter < cars)
rather than
while(counter <= cars)