Thread: Could someone help me with this for loop I wrote?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    Could someone help me with this for loop I wrote?

    I've just started C++ this week, and I have an assignment where you have to create a console application that asked the user how many numbers they would like to sum together, and then loops that many times, taking in a number and adding it to the sum each time. This is what I have -

    Code:
    int main()
    {
    
    
    	int repeat_amount, sum = 0, entered_number;
    
    
    	cout << "Enter the amount of numbers you wish to add together: ";
    	cin >> repeat_amount;
    
    
    	for (int number = 0;number == repeat_amount; number ++) {
    		cout << "Enter a number: ";
    		cin >> entered_number;
    		sum = sum + entered_number;
    	}
    
    
    	cout << sum;
    }
    The result is it doesn't allow me to enter any numbers except the first one. This is what I get when I run it -

    Enter the amount of numbers you wish to add:
    (then i enter a number and hit enter)
    0Press any key to continue...

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Write
    for (int number = 0;number < repeat_amount; number ++)

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    I'll try that in a second, let me close out of this other program I started. Thanks for the response.

    Edit: Okay, that fixed it. That was such a small error and I had to ask for help, haha. But thanks man, I appreciate it very much.
    Last edited by English_Fire; 09-30-2011 at 12:28 PM.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    Can U explain why number == repeat_amount' statement is not correct?

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by meterpreter View Post
    Can U explain why number == repeat_amount' statement is not correct?
    Because, that does not evaluate to be true for the required no. of times.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    a for() loop repeats while the second parameter is true. if you initialize number to zero, it will not be equal to repeat_amount on the first iteration of the for loop, so it won't even run once.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    Cleared! th@nks for reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A little program I wrote for fun !
    By manasij7479 in forum C++ Programming
    Replies: 43
    Last Post: 07-25-2011, 02:53 AM
  2. So I wrote a class.
    By brewbuck in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 12-13-2008, 06:22 PM
  3. Can someone look over the program I wrote?
    By brooklyn in forum C++ Programming
    Replies: 10
    Last Post: 04-16-2006, 07:23 AM
  4. How to prove you wrote it
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 01-07-2004, 09:49 PM
  5. Check out this game I wrote
    By Eber Kain in forum Game Programming
    Replies: 4
    Last Post: 09-16-2003, 09:54 PM