need help with "for loop"

these are my instructions: a) [countup] Count up to a Limit specified by the user, starting at 1.

Output format: COUNT UP: 1 2 3 4 5 6 7 8 9 10

Pseudo-code:
Read a value for Limit (int)
Print the output label (no endline or \n).
Initialize count to 1;
Print count 1,2, ..., Limit // MUST USE for loop

Use following TEST DATA: 3, 0, 13

and this is what ive tried:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
int i = 0;


for (int i; i < 9999; i++)
   cin >> i;
   cout << i << endl;

return 0;
}
You want to ask the user for the limit then use that as the boundary in the loop.

So you don't want the 9999, nor do you want the cin >> i; inside of the loop.
i changed it to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
int i = 0;
cin >> i;

for (int i; i++; i < (i + 1) )
cout << i << endl;

return 0;
}


and now it just keeps counting without stopping
Variable i inside the control statement of the loop is not initialized. And as I think i is always less than i + 1 until i will not reach maximum value.:)
Last edited on
thanks i got it to countup correctly. but could you help me get the output like this:
COUNT UP: 1 2 3 4 5 6 7 8 9 10

instead of:
COUNT UP: 1
COUNT UP: 2
COUNT UP: 3
COUNT UP: 4
COUNT UP: 5
COUNT UP: 6
COUNT UP: 7
COUNT UP: 8
COUNT UP: 9
COUNT UP: 10
COUNT UP: 11
COUNT UP: 12
COUNT UP: 13

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
int i = 1, c;
cin >> c;

for (int i = 1; i < (c + 1); i++ )
cout << "COUNT UP: " << i << endl; 

return 0;
}
For that you'll want to place the "COUNT UP: " before the loop. Also, instead of endl have " ".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
int i = 1, c;
cout<<"Enter a Number : " ;
cin >> c;
cout << "COUNT UP: ";
for (int i = 1; i < (c + 1); i++ )
 cout<< i << " "; 

return 0;
}
Topic archived. No new replies allowed.