Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have grades in array and I want the run them threw a letter grade I loop it so It can tell he how many A I got how many B I got and so on.

C++
void StatsList::PrintGradeCount()
{
	double gcount = 0;
	while (int j =! 1);
	{
		gcount = gcount + List[Listindex];
	
	
		



		if (gcount <= 100 && gcount >= 90) {
			cout << "A:" << gcount << endl;
		}
		else if (gcount <= 89 && gcount >= 80) {
			cout << "B:" << gcount << endl;
		}
		else if (gcount <= 79 && gcount >= 70) {
			cout << "C:" << gcount << endl;
		}
		else if (gcount <= 69 && gcount >= 60) {
			cout << "D:" << gcount << endl;
		}
		else if (gcount <= 59 && gcount >= 0) {
			cout << "F:" << gcount << endl;
		}
	}
	
	
	return;
}


What I have tried:

I had a for loop first then I tried while I think it's wrong but can some one help up.
Posted
Updated 13-Oct-16 15:29pm
Comments
jeron1 13-Oct-16 13:44pm    
I'm afraid
	while (int j =! 1); 
isn't going to loop anything.

You're probably going to need separate variables for the A count, the B count, ...

Is double type really necessary?
Member 12792145 13-Oct-16 13:52pm    
What I had first
for(int i = 0; i< Listindex; i++;){
    gcount = gcount + List[1]
    }

and I had it double because some of the variables in the array have decimals.
[no name] 13-Oct-16 14:00pm    
j would never equal 1 and adding the values together from whatever List is, would never tell you how many A's you got. It will just tell you a total. Write what you want to do on a sheet of paper step by step and then turn that into code.
jeron1 13-Oct-16 14:01pm    
for(int i = 0; i< Listindex; i++;){
    gcount = gcount + List[1]
    }


The last semi colon in the for statement is unnecessary.
The 'List[1]' should be 'List[i]'.

What is gcount supposed to represent? From what you have shown, gcount is a simple sum of all the grades, is this what you want?
Member 12792145 13-Oct-16 14:49pm    
void StatsList::PrintGradeCount()
{
	
	for (int i = 0; i < Listindex; i++)
	{
		List[i];
		cout << "grade: ";
	
		if (i <= 100 && i >= 90) {
			cout << "A:" << i << endl;
		}
		else if (i <= 89 && i >= 80) {
			cout << "B:" << i << endl;
		}
		else if (i <= 79 && i >= 70) {
			cout << "C:" << i << endl;
		}
		else if (i <= 69 && i >= 60) {
			cout << "D:" << i << endl;
		}
		else if (i <= 59 && i >= 0) {
			cout << "F:" << i << endl;
		}
	}
	
	
	return ;
}

This is what I got now but this only counting the number of arrays not the elements in the array

1 solution

What you do looks rather confuse, use the debugger to see what your code is doing.
C++
        if (gcount <= 100 && gcount >= 90) {
    cout << "A:" << gcount << endl;
}
else if (gcount <= 89 && gcount >= 80) {
    cout << "B:" << gcount << endl;
}
else if (gcount <= 79 && gcount >= 70) {
    cout << "C:" << gcount << endl;
}
else if (gcount <= 69 && gcount >= 60) {
    cout << "D:" << gcount << endl;
}
else if (gcount <= 59 && gcount >= 0) {
    cout << "F:" << gcount << endl;
}


Independently from other bugs, your code fail when gcount is 89.5 or 69.2
The code can handle this case with simplifying because all the if and else if us a single structure that you need to learn.
assuming gcount is between 0 and 100:
C++
        if (gcount >= 90) {
    cout << "A:" << gcount << endl;
}
else if (gcount >= 80) {
    cout << "B:" << gcount << endl;
}
else if (gcount >= 70) {
    cout << "C:" << gcount << endl;
}
else if (gcount >= 60) {
    cout << "D:" << gcount << endl;
}
else if (gcount >= 0) {
    cout << "F:" << gcount << endl;
}



You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.


Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900