C++ Progression

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;
int main ()

{
    double d;
    double startnum;
    double n;

    cout <<"Starting number - " << endl; 
    cin >> startnum;

    cout <<"D - "<< endl;cout << endl;
    cin >> d; cout << endl;

    cout <<"N - "<< endl;cout << endl;
    cin >> n; cout << endl;

    for(double x = startnum, d = d; x<=n ; x++, d++)
    {
       cout << x << endl;
    }

    return 0;
}


Lets say I enter start number(startnum=2), d (d=2), n (n=5).
After i chosen the numbers i want the output to look like this.

4
6
8
10
12

At this moment i'm only getting

4 first number
5 |
6 |No d
7 |is added.
8 |

"d" is not being added after the first number.

Thanks in advance.


EDIT:

1
2
3
4
for(double x = startnum; x<=n; x++)
    {
       cout << x << endl;
    }


if the starting number is 66 , d is 2 and n is 4, it doesn't even output anything. I've no idea what am i doing wrong.
Last edited on
Looking at the program the output should be
3
4
5
6
for the given input

For what you need to achieve
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;
int main ()

{
  double d;
  double startnum;
  int n;

  cout <<"Starting number - " << endl;
  cin >> startnum;

  cout <<"D - "<< endl;cout << endl;
  cin >> d; cout << endl;

  cout <<"N - "<< endl;cout << endl;
  cin >> n; cout << endl;

  for(int x = 0; x < n ; x++)
    {
      cout << (startnum += d) << endl;
    }

  return 0;
}
closed account (3qX21hU5)
Im not really getting what you are trying to do. What is the exercise question? Im still also very new to programming so I might be missing something but could use alittle more info.
You do some weird things. No need for the semi colons to add more endl's. Can just keep inserting them into the stream. Not sure why you're using doubles? And your for loop is strange. Why do you have d involved in the loop at all? It doesn't do anything but just add 1 to itself. All your program does is print startum through n, one at a time.
Excuse me for not being more specific.
Basically, i have to make geometric progression program where the user enters all the numbers, starting number, the length of progression, etc.

@codewalker (49) Thanks!
Last edited on
Oh, and one quick question.
Can I somehow make an error saying that data which i entered was wrong?
For example.
I enter characters instead of numbers.
closed account (3qX21hU5)
http://www.cplusplus.com/reference/iostream/cerr/

that should help alittle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include <iostream>
using namespace std;
int main ()

{
    double q; 
    double startnum; 
    int ok; 

    do
    {

    cout <<"First number - " << endl; cout << endl;
    cin >> startnum; cout << endl;

    cout <<"Q - "<< endl; cout << endl;
    cin >> q; cout << endl;

    cout <<"First (5) numbers - "<< endl; cout << endl;
    for(int x = 0; x < 5 ; x++) 
    {

      cout << (startnum += q) << endl;

    }

    cout << endl;
    cout << " Continue  (1) End (0)?" << endl; 
    cin >> ok;
     } while (ok == 1);

    return 0;
} 


Could anyone by any chance help me to write
if/while error for both cin >>.
Basically, if user enters non-number there should be a "Wrong number!"
And then it jumps to cout << " Continue (1) End (0)?" << endl; part.

Again, appreciate the help.
Topic archived. No new replies allowed.