C++

I decided to learn c++ and purchased several books, I currently am focusing on C++ without fear since I find it most helpful. I'm like up to page 53/400+ and I'm stuck on this one task. This is the task "Exercise 2.2.1. Write a program to print 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 two values n1 and n2; then initialize i to n1 and use n2 in the loop condition.)"

And the original source code is
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main() {
int i = 1, n;
// Get number from the keyboard and initialize i.
cout << "Enter a number and press ENTER: ";
cin >> n;
while (i <= n) { // While i less than or equal n,
cout << i << " "; // Print i,
i = i + 1; // Add 1 to i.
}
return 0;
}


And MY source code is..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
int i = 1, n, b;
// Get number from the keyboard and initialize i.
cout << "Enter a number and press ENTER: ";
cin >> n;
cout << "Enter another number and press ENTER: ";
cin >> b;
while (b <= n) { // While i less than or equal n,
cout << n << " "; // Print i,
n = b + 1; // Add 1 to i.
}
return 0;
system("PAUSE");
}
(IGnore the // comments


I've tried several ways but with no luck. The closest I ever got was probably writing one that just kept somehow generation random numbers :\
Haha. I remember this one :)

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main() {
int n;
int b;
// Get number from the keyboard and initialize i.
cout << "Enter a number and press ENTER: ";
cin >> n;
cout << "Enter another number and press ENTER: ";
cin >> b;
while (b >= n) { // While i less than or equal n,
cout << n << " "; // Print i,
n++; // Add 1 to i.
}
while (b <= n)
{
cout << n << " ";
n--;

}
return 0;

}


I added something =]
(I left your comments in, so they don't make much sense :P)
EDIT: Forgot to make variables
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdafx.h>
#include <iostream>
using namespace std;
int main() {
int i = 1, n, b;
//Assuming n is less than b
cout << "Enter a number and press ENTER: ";
cin >> n;
cout << "Enter another number and press ENTER: ";
cin >> b;
//I entered 1 for n and 9 for b
//so loop condition b<=n i.e. (9<=1) is false. So nothing happens
//so you might want to change it to n<=b (1<=9 is true)
while (b <= n) {
cout << n << " ";   //So you print n here, i.e. 1 in first case
n = b + 1;          //now n=b+1=10. which makes n<=b false. so n is printed only for the first time
                    //why not make it n=n+1 or n++ instead?
}
return 0;

}
system("PAUSE");


And yes, I haven't changed your code. Only removed your comments
Last edited on
you shouldnt use system pause how about cin.get()
I tried CodeAssassin's source but it didn't work, I also tried using the system pause command with it. Pravesh, can you show me your version of it, I can't comprehend what your doing
Okay, let's first take the original source code and analyze it.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main() {
int i = 1, n;
// Get number from the keyboard and initialize i.
cout << "Enter a number and press ENTER: ";
cin >> n;
while (i <= n) { // While i less than or equal n,
cout << i << " "; // Print i,
i = i + 1; // Add 1 to i.
}
return 0;
}

First of all, what does this code do?

Enter a number and press ENTER: 4
1 2 3 4


Enter a number and press ENTER: 7
1 2 3 4 5 6 7


It allows the user to input a number, then counts to that number, starting with 1. Pretty simple.

And how does it do this? It initializes the variable i to 1, and then uses the following loop:

1
2
3
    while (i <= n) { // While i less than or equal n,
        cout << i << " "; // Print i,
        i = i + 1; // Add 1 to i. 


to count from i (1) to n. Since i starts out at 1, we print out every number from 1 to n, stopping when i is greater than n.

So the program counts from 1 to n. We want it to count from n1 to n2.

In the original program, we set i to 1 and continued through the loop until it was bigger than n. But what if we set i equal to n1, and continued through the loop until it's bigger than n2? In that case, our loop would start at n1, and then continue through n2. Perfect!

I'd imagine it'd look like this (comments mine):

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() {
    int i;        //variable to count through loop
    int n1, n2;   //two numbers the user will enter

    //Get the first (assumed to be smaller) number
    cout << "Enter a number: ";
    cin >> n1;

    //Get the second (assumed to be bigger) number
    cout << "Enter a BIGGER number: ";
    cin >> n2;

    //Here's the magic - set i equal to n1, so we
    //  start counting at n1!
    i = n1;

    while (i <= n2) { // While i less than or equal n2
        cout << i << " "; // Print i,
        i = i + 1; // Add 1 to i.
    }
    return 0;
    system("PAUSE");
}


And the output:

Enter a number: 3
Enter a BIGGER number: 8
3 4 5 6 7 8


I hope this was clear. Please let me know if you don't understand in any way.
*EDIT* I tried to use a different compiler, I was originally using Visual C++ 2010. Anyway CodeAssassin's source code worked as did ReedTompkin's (thanks for the detailed post dude, it helped a lot). However one problem I encountered during both is that when I input the commands they both do as they are told to, but the console just shuts down immediately after. Even with the pause command. Any idea's why :o?
Try putting the system("PAUSE"); BEFORE the return 0;
Someone propose cin.get() which will wait for user input. Looks good to replace system("PAUSE') isn't it?
If you have learned if you can check which number is bigger and if they are as supposed to be continue as usual and in the other case swap them (give the value of b to n and vice versa - you will need a third temporary variable for this)
system("pause") is completely fine, trust me. There are compatibility issues with it, among other things, but honestly if you're just using it while doing learning exercises than it's not even a problem. Also here is my take on your problem, just a little cleaner. :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void main()
{
	int n1, n2;

	std::cout << "Enter a number: ";
	std::cin >> n1;
	std::cout << "Enter a BIGGER number: ";
	std::cin >> n2;

	if ( n2 <= n1 )
		return;
	
	for ( int i = n1; i <= n2; i ++ )
		std::cout << i << std::endl;
}
@TheNoobie: It is fine in practice, but it takes up a lot of memory. There are shorter ways (or just slightly longer ways) to do it. As I read once on this, why use the bulldozer to open the door when the key works just as fine.
I would say that it's not completely fine. That's like trying to teach your kids to clean the dishes, and when they do, they use a toothbrush. It takes up entirely too much processor time, and it's a bad habit to put them into in the first place.
o.O

How in heck, did mine fail?
It works on my compiler.
Last edited on
Hmm. Maybe because of this:

int i = 1, n, b;

Yeah, sorry about that. You could've fixed that, by making n and b ints.
The weird thing is: It works on my compiler! O_O

EDIT: Nvm. Just read the OP's Post :P
Last edited on
the reason why i say its fine in practice is because its showing what not to do
Nvm tried a different compiler and it worked. Thanks guys.

/solved
Topic archived. No new replies allowed.