Cant figure out how to do this!

This is a discussion on Cant figure out how to do this! within the C++ Programming forums, part of the General Programming Boards category; Im not too good at C++, plus, I am not good at anything. Anyways, I am trying to make a ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    10

    Cant figure out how to do this!

    Im not too good at C++, plus, I am not good at anything. Anyways, I am trying to make a prog that takes a number from the user and then adds all the numbers that lead up to it together. For example

    cin>>5 (num)

    This is the part I dont know how to do. I know i have to use a loop, and I know how to make it count up to the number, but how do i get it to take the number it gets and put it in some kind of queue that will be added up later (0+1+2+3+4+5)?

    cout<<15 (sum)

    any help would be great.

  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    If all you're doing is adding all of the numbers before the number entered then you can easily do this with a simple loop.
    Code:
    int total;
    for ( int i = 0; i < num; i++ ) {
        total += i;
    }
    cout << total;
    pointer = NULL

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include<iostream.h>
    int main()
    {
    
    	int num;
    	int sum = 0;
    
    	cout << "Enter a number: ";
    	cin >> num;
    	while(num > 0)
    	{ 
    		sum += num;
    		--num;
    	}
    	cout << "Sum is: " << sum << endl;
    
    	return 0;
    }
    This can be made into a function. Here is one solution.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    10
    ok, i understand now, i didnt know about that += what is that?

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    13
    total += i

    is the same as

    total = total + i

    It's just one of those short-hand things that makes life easier. You can use that "trick" for any normal operator, like:

    total -= i; (total = total - i; )
    total *=i; (total = total * i; )
    total /=i; (total = total / i; )
    Last edited by DanMan; 10-16-2001 at 04:40 PM.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    10
    ahh, i see

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  2. Can't figure out why code is hanging up
    By Panserbjorn in forum C Programming
    Replies: 3
    Last Post: 10-28-2007, 06:09 PM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 11:57 AM
  4. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 01:47 AM
  5. I can't figure out this compiler error
    By lime in forum C Programming
    Replies: 7
    Last Post: 07-25-2003, 06:09 PM

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