C++ Without Fear..

Pages: 12
Exercise 3.1.1

Use the for statment in a program that prints all the numbers from N1 to N2, where N1 and N2 are two numbers specified by the user. (Hint: You'll need to prompt for the two values; then, inside the for statement, initialize i to N1, and use N2 in the loop condition).

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

int main () {

cout << "Enter a number and press ENTER: ";
cin >> n;
for (i=1; i <= n; i++)
cout << i << " ";

system ("PAUSE");
return 0;
}


I'm not looking for anyone to do this work, this is the code that was given as an example and I don't know what is going on in the for statement or cin. Anyone mind explaining how I could get started on my own code? This book doesn't give you anything but this example.
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/doc/tutorial/control/

For loops along with the othere controls are explained with demos here. Use the gear wheel to run online. :)
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main () {
int n = 0; // <--
cout << "Enter a number and press ENTER: ";
cin >> n;
for (int i=1; i <= n; i++) //<--
cout << i << " ";

system ("PAUSE");
return 0;
}


I haven't done you homework but your code doesn't run. It does now.
Last edited on
Edited the OP. I'm just trying to get started on this.. >.<
closed account (48T7M4Gy)
Go for it. Yell out if you need help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int i;
int N1, N2;

int main () {

cout << "Enter a number and press ENTER: ";
cin >> n;
for (N1=10; N1 <= i; N1++)
cout << N1 << " ";

system ("PAUSE");
return 0;
}


Did I get the first part correct? Also how can I add another "for" statement?
Did I get the first part correct?

Did you compile it? What do you think the value of i is when you first enter your for loop?

edit: you should also move this:
1
2
3
int i;
int N1, N2;


into your main function. global variables are bad.
Last edited on
closed account (48T7M4Gy)
Did I get the first part correct?


As mutexe writes the answer is no.

Forget about global variables for a second and concentrate on the sample I gave you and the tutorial.

The key line is, which you need to carefully consider what it means
for (int i=1; i <= n; i++) //<--
i = 1 states the value of the starting number and n is the finishing number which the user inputs.

If xyz was an integer and it was the starting number then the line would be
for (int i = xyz; i <= n; i++) //<--

And you can use cin to get the user to input a value for xyz too, keeping in mind you have to declare xyz as an integer. ( Preferably inside main and therefore not global).

Get it? :)
Kinda lost...
what is n doing here?

 
for (int i = xyz; i <= n; i++) //<-- 



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

int main () {

cout << "Enter a number and press ENTER: ";
cin >> n;
for (int i=N1; i <= n; i++)
cout << N1 << " ";

system ("PAUSE");
return 0;
}
Last edited on
closed account (48T7M4Gy)
what is n doing here?

n is a variable name for the ending number. You can call it N2 (or even N1 depending on how you want to align directly with the original question) if you like.

The loop starts at N1 and ends at N2. Your job is to write a program to get the user to set values for each.
From your response, this is what I came up with. I'm not sure how to say "and" this in the code.


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

int main () {

cout << "Enter a number and press ENTER: ";
cin >> n1;
for (int i=10; i <= n1; i++)
cout << i << " ";

cout << "Enter a number and press ENTER: ";
cin >> n2;
for (int i=10; i <=n2; i++)
cout << i << " ";

system ("PAUSE");
return 0;
}
Last edited on
closed account (48T7M4Gy)
You're making this far too complicated.

You don't need a second $%#@ loop, especially when it is doing exactly the same as the first one and you go to all the trouble of inputting to a variable n2 and never &*^*#$ using it!!

So, I'll put it another way. Look carefully at the output.

Also if you are going to use a variable (of any) name C++ demands you declare it. So if you have an integer variable called N3, before you can use it in any way you have to write int N3; beforehand. And even better than that, initialize it which means give it an initial value which can be changed later. So you would write int N3 = 0;

Press the gear wheel and see what happens

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

int main () {

for (int i = 123; i <= 456; i++) //<--
cout << i << " ";

system ("PAUSE");
return 0;
}
So global variables are bad... This worked but I did not use n2.



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

int main () {

cout << "Enter a number and press ENTER: ";
cin >> n1;
for (i=0; i <= n1; i++)
cout << i << " ";

system ("PAUSE");
return 0;
}
Last edited on
closed account (48T7M4Gy)
Using global variables isn't the end of the world but as you build up a bit more experience you'll find there isn't much use for them. They should be teaching you about scope etc which is the key aspect to this. It's important but not complicated, something to read up on in the tutorials here when you have time.

As far as your program is concerned I am surprised you haven't got it. But there ya go.

My last try:

There is no reason why you can't have cin >> N2; as another line after you declare int N2;

And there is no reason why you can't at least try for( i = N1; i <= N2; i++) as a substitute.

Cheers
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int i, n1, n2;

int main () {

cout << "Enter a number and press ENTER: ";
cin >> n1;
cout << "Enter a second number and press ENTER: ";
cin >> n2;
for (i = n1; i <= n2; i++)
cout << i << " ";

system ("PAUSE");
return 0;
}
closed account (48T7M4Gy)
I bet you're glad that's over. Cheers :)
Says to use int in the main function.

I've tried for (int i = n1; i <= n2; i++)

But it will not work...
closed account (48T7M4Gy)
just move line 3 down to where line 6 is now
closed account (48T7M4Gy)
if it's not working it's probably because n2 isn't larger than n1

try it with n1 = 7 and n2 = 12
Last edited on
Done? :)

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

int main () {
int i, n1, n2;
cout << "Enter a number and press ENTER: ";
cin >> n1;
cout << "Enter a second number and press ENTER: ";
cin >> n2;
for (int i = n1; i <= n2; i++)
cout << i << " ";

system ("PAUSE");
return 0;
}
Last edited on
Pages: 12