I'm learning C++ in college and I need some help. I need to make a for loop to allow the user to enter ten test scores. How do I make my for loop repeat the question ten times exactly?
I'm learning C++ in college and I need some help. I need to make a for loop to allow the user to enter ten test scores. How do I make my for loop repeat the question ten times exactly?
What have you tried?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Code:#include <iostream> using namespace std; char score; double z; int main() { cout << "We are going to display the grades for 10 test scores\n"; for (int z = 0; z <11; z++) { cout << "Please enter a test score\n"; cin >> z; }
The first thing I see is that you declared two global variables:
Generally, you should avoid global variables as they can make it more difficult for you to reason about your program, though you will only see this later when your programs becomes more complex. Furthermore, score probably should be a double, not a char, and since you are declaring z in the loop, you don't need to declare it before the loop.Code:char score; double z;
Next, you have an off by one error in your loop condition: since you start from z = 0, the loop condition should be z < 10 in order to loop 10 times.
Instead of cin >> z you probably wanted to write cin >> score.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It worked perfectly. Thanks for the help