loop

This is a discussion on loop within the C++ Programming forums, part of the General Programming Boards category; i can't get this program to work. I'm trying to get 2 numbers from the user input and then add ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    loop

    i can't get this program to work. I'm trying to get 2 numbers from the user input and then add the numbers inbetween the 2 number from the input. Please help me. By the way, here's my code.

    #include <iostream.h>
    int main()
    {
    int x,y; // two numbers
    int hold; // holds the screen

    cout << "what is number 1 " << endl;
    cin >> x;
    cout << "what is number 2 " << endl;
    cin >> y;

    for (x>=1; x<=y; x++)

    cin >> hold;
    return 0;
    }

    Any help would by appreciated.
    Thanks.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: loop

    Originally posted by sonict
    Code:
    #include <iostream.h>
    int main()
    {
    	int x,y;	// two numbers
    	int hold;	// holds the screen
    
    	cout << "what is number 1 " << endl;
    	cin  >> x;
    	cout << "what is number 2 " << endl;
    	cin  >> y;
    
    	for (x>=1; x<=y; x++)
    	
    	cin >> hold;
    	return 0;
    }
    
    Any help would by appreciated.
    Thanks.
    make that:
    for(int i=x; i<y; i++) TotalSum += i;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Hmm donīt really understand you question. If you could giva an example it would defintly help. Anyway there are some syntax errors in your code

    Code:
    for (x>=1; x<=y; x++)
    I belive it should be
    Code:
    for (x=1; x<=y; x++)
    {
    //statement(s)
    }
    //for(initialization; check condition; statement(s))
    Donīt forget to use code tags

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    Originally posted by ripper079
    Hmm donīt really understand you question. If you could giva an example it would defintly help. Anyway there are some syntax errors in your code

    Code:
    for (x>=1; x<=y; x++)
    I belive it should be
    Code:
    for (x=1; x<=y; x++)
    {
    //statement(s)
    }
    //for(initialization; check condition; statement(s))
    Donīt forget to use code tags
    You enter 10 for first number and 20 for second number it should print 165 because i'm trying to find out the sum between the first and second numbers. so, you add the numbers like this: 10+11+12+13+14+15+16+17+18+19+20
    and it should print 165.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    Re: Re: loop

    Originally posted by Magos
    make that:
    for(int i=x; i<y; i++) TotalSum += i;
    my output is:
    */
    what is number 1
    10
    what is number 2
    20
    -858993315
    /*

  6. #6
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Magos code is almost right then. Make a minor change and it should work.

    Code:
    int TotalSum = 0;
    for(int i=x; i<=y; i++) 
    TotalSum += i;
    //print TotalSum

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    Smile it worked.

    thank you everyone. it works.
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21