I'm having an issue with my string array. It doesn't seem to be storing anything or it is only storing the last string typed in. Also, my while loop is
supposed to stop "-1" from being factored in for the lowest grade. Is there another way to do this? Any help would be appreciated, Thanks.
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
double total = 1;
int count = 0;
double average = 0;
string Names[8];
double Grades[8];
double max = Grades[0];
double low = Grades[0];
int MaxIndex = 0;
int MinIndex = 0;
for (int i = 0; i < 8; i++){
//Stop when Grade input is -1
while(Grades[i] != -1){
cout << "Enter name and grade (or DONE -1 to quit): ";
cin >> Names[i] >> Grades[i];
total+=Grades[i];
count++;
//Find Highest Grade
if(max < Grades[i]){
max = Grades[i];
MaxIndex = i;
}
//Find lowest grade
if (low > Grades[i])
low = Grades[i];
MinIndex = i;
}
cout << "There were " << count - 1 << " grades"<<endl;
cout << "The total was " << total <<endl;
average = total / (count - 1);
cout << "The average was " << average<<endl;
cout << "The highest grade was " << max << " made by "
<< Names[MaxIndex] <<endl;
cout << "The lowest grade was " << low << " made by "
<< Names[MinIndex] <<endl;
return 0;
}
}
For example if,
"Bill 9", "Ted 5", and "DONE -1" is typed in, the output is as follows:
Enter name and grade (or DONE -1 to quit): Bill 9
Enter name and grade (or DONE -1 to quit): Ted 5
Enter name and grade (or DONE -1 to quit): DONE -1
There were 2 grades
The total was 14
The average was 7
The highest grade was 9 and was made by DONE
The lowest grade was -1 and was made by DONE
The desired output would be:
Enter name and grade (or DONE -1 to quit): Bill 9
Enter name and grade (or DONE -1 to quit): Ted 5
Enter name and grade (or DONE -1 to quit): DONE -1
There were 2 grades
The total was 14
The average was 7
The highest grade was 9 and was made by Bill
The lowest grade was 5 and was made by Ted
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
double total = 1;
int count = 0;
double average = 0;
string Names[8];
double Grades[8];
double max = Grades[0];
double low = Grades[0];
int MaxIndex = 0;
int MinIndex = 0;
for (int i = 0; i < 8; i++)
{
//Stop when Grade input is -1
while(Grades[i] != -1)
{
cout << "Enter name and grade (or DONE -1 to quit): ";
cin >> Names[i] >> Grades[i];
total+=Grades[i];
count++;
//Find Highest Grade
if(max < Grades[i])
{
max = Grades[i];
MaxIndex = i;
}
//Find lowest grade
if (low > Grades[i])
low = Grades[i];
MinIndex = i;
}
cout << "There were " << count - 1 << " grades"<<endl;
cout << "The total was " << total <<endl;
average = total / (count - 1);
cout << "The average was " << average<<endl;
cout << "The highest grade was " << max << " made by "
<< Names[MaxIndex] <<endl;
cout << "The lowest grade was " << low << " made by "
<< Names[MinIndex] <<endl;
return 0;
}
}
In your while loop where do you ever increase the value of i?
How many times do you think your for() loop is going to execute?
In addition to what jlb said, what do you think is going to be stored in max and low when your program starts? hint: Grades[0] is uninitialized, so Grades[0] contains garbage.
If the user enters -1 for Grades[i] at line 22, you proceed to calcuate low and max using -1.
Why do you have both a for and a while loop? You want a single loop that terminates when Grade[i] == -1 OR i == 8.
Keep in mind at line 22, the user is going to have to enter a dummy name in order to enter -1 for Grade[i]. Not the most user friendly input style.
Thank you jlb and AbstractionAnon. Sorry for the indention mistakes. I think I fixed the problem with having both the for and while loop. However, I'm still having issues with excluding -1 from the low.
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
double total = 1;
int count = 0;
double average = 0;
string Names[8];
double Grades[8];
double max = Grades[0];
double low = Grades[0];
int MaxIndex = 0;
int MinIndex = 0;
int i = 0;
//Stop when Grade input is -1
while(Grades[i] != -1)
{
i++;
cout << "Enter name and grade (or DONE -1 to quit): ";
cin >> Names[i] >> Grades[i];
total+=Grades[i];
count++;
//Find Highest Grade
if(max < Grades[i])
{
max = Grades[i];
MaxIndex = i;
}
//Find lowest grade
if (low > Grades[i])
{
low = Grades[i];
MinIndex = i;
}
}
cout << "There were " << count - 1 << " grades"<<endl;
cout << "The total was " << total <<endl;
average = total / (count - 1);
cout << "The average was " << average<<endl;
cout << "The highest grade was " << max << " made by "
<< Names[MaxIndex] <<endl;
cout << "The lowest grade was " << low << " made by "
<< Names[MinIndex] <<endl;
return 0;
}
You may want to consider "pre-fetching" the first grade before the loop, then moving the input to the end of the loop. Also I would expect the user to enter the "-1" into the name, not the grade, because of the way your prompt is worded, I doubt they would think to enter "DONE" for name and "-1" for the grade.
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
double total = 1;
int count = 0;
double average = 0;
string Names[20];
double Grades[20];
double max = 1;
double low = 99;
int MaxIndex = 0;
int MinIndex = 0;
int i = 0;
//Stop when Grade input is -1
while(Grades[i] > 0)
{
i++;
cout << "Enter name and grade (or DONE -1 to quit): ";
cin >> Names[i] >> Grades[i];
total+=Grades[i];
count++;
//Find Highest Grade
if((max < Grades[i]) && (Grades[i] > 0))
{
max = Grades[i];
MaxIndex = i;
}
//Find lowest grade
if ((low > Grades[i]) && (Grades[i] > 0))
{
low = Grades[i];
MinIndex = i;
}
}
cout << "There were " << count - 1 << " grades"<<endl;
cout << "The total was " << total <<endl;
average = total / (count - 1);
cout << "The average was " << average<<endl;
cout << "The highest grade was " << max << " and was made by "
<< Names[MaxIndex] <<endl;
cout << "The lowest grade was " << low << " and was made by "
<< Names[MinIndex] <<endl;
return 0;
}