### Beginner Question

Hey so im new at c++...i'm not too sure on how to make a program where the user inputs a number and the number is added by a another fixed number.. lets say 2.
for example. if a user inputs a number 5, the program then displays all the sequence of the number after each addition of 2 starting from 5+2, (5+2)+2, (5+2+2)+2, and so forth. I'm stuck.. can someone please give me an example of the source code..

#include <iostream>
#include <cmath>
using namespace std;

int main ( ){

int num, new_num;

cout << "Enter number: ";
cin >> num ;

new_num = num + 2;

cout << new_num;

// how to add 2 after the new_num? i want to do this repetitively ( 30 times ) . Do i make a new variable 30 times? or is there any easier way?

}
If you want to repeat a set of instructions, you should use a loop. Since you want to repeat a fixed number of times, a for loop would be best. It's one of the basic control structures in C++, so your textbook should explain it, if you don't know about loops already.
Some things are too small to explain without doing it for you.

you can do it in a loop:

new_num = num; //don't forget this step
for(int j = 0; j < 30; j++)
//syntax: for (initial condition; until condition; code that usually makes the condition happen)
new_num += 2;

or you can be a smart* and say
new_num += 2*30; // point being that looping when you don't need to is not a great idea.
//but you will need to know how to loop, the times when it can be avoided are few.

Last edited on
jonnin wrote:
or you can be a smart* and say
`new_num += 2*30;`
SyasyAwesome wrote:
the program then displays all the sequence of the number after each addition of 2

It's not going to be possible to display the result of each consecutive addition, if you just leap to the end by adding 60.

BTW, jonnin, is there a reason you don't use code tags? You've surely been here long enough to be aware of them, and it would make your code so much easier to read.

For the OP's benefit, code tags are explained at:

http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
... and loops are explained at:

http://www.cplusplus.com/doc/tutorial/control/

Lets look at the original program for some other aspects:
 ``1234567891011`` ``````#include #include // #1 using namespace std; // #2 int main ( ) { int num, new_num; // #3 cout << "Enter number: "; cin >> num ; // #4 new_num = num + 2; cout << new_num; }``````

#1 This program does not use anything from cmath. Include what you need, no more.

#2 You did write 21 characters to avoid writing 15 characters in the code. Did you know that? The `using namespace std;` may seem convenient, but can lead to trouble, if not used carefully.

#3 Again, something that looks compact, but might lead to less clear code. One can delay variable declarations in C++.

#4 What if fails? You expect the user to enter a number. User's are naughty; they fail.

Lets put some of that in/out:
 ``123456789101112`` ``````#include int main ( ) { using std::cout; // we can use 'cout' in this function int num = 0; //initialization; we are certain that num == 0; cout << "Enter number: "; if ( std::cin >> num ) // we are explicit about the cin { // do these only if we got a number successfully int new_num = num + 2; // declaring new_num where it is used cout << new_num; } }``````

Last edited on
Topic archived. No new replies allowed.