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>
usingnamespace 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.
#include <iostream>
usingnamespace 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.
#include <iostream>
usingnamespace 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?
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).
#include <iostream>
usingnamespace 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;
}
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>
usingnamespace 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;
}
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>
usingnamespace 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>
usingnamespace 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;
}
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.
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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;
}