Thread: Beginner. Need help with for loop

  1. #1
    Registered User
    Join Date
    Apr 2015
    Location
    Staten Island, New York, United States
    Posts
    22

    Question Beginner. Need help with for loop

    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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2015
    Location
    Staten Island, New York, United States
    Posts
    22
    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;
    
    
    
    
    	}

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The first thing I see is that you declared two global variables:
    Code:
    char score;
    double z;
    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.

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2015
    Location
    Staten Island, New York, United States
    Posts
    22
    It worked perfectly. Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with While/Switch loop--beginner
    By Algebro in forum C Programming
    Replies: 3
    Last Post: 09-13-2013, 12:00 PM
  2. While loop with OR conditions, beginner
    By Paulsim in forum C++ Programming
    Replies: 5
    Last Post: 09-11-2012, 06:59 AM
  3. Beginner Problem with Loop
    By ToweyLeake in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2011, 10:12 AM
  4. beginner loop help
    By helloalyssa in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2010, 09:08 PM
  5. trouble with a loop (beginner)
    By Procta in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2003, 10:27 AM