C++

Dec 19, 2011 at 9:46am
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 :\
Dec 19, 2011 at 10:08am
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 Dec 21, 2011 at 4:07am
Dec 19, 2011 at 12:46pm
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 Dec 19, 2011 at 12:47pm
Dec 19, 2011 at 3:58pm
you shouldnt use system pause how about cin.get()
Dec 20, 2011 at 1:08am
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
Dec 20, 2011 at 2:02am
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.
Dec 20, 2011 at 5:24am
*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?
Dec 20, 2011 at 6:02am
Try putting the system("PAUSE"); BEFORE the return 0;
Dec 20, 2011 at 6:51am
Someone propose cin.get() which will wait for user input. Looks good to replace system("PAUSE') isn't it?
Dec 20, 2011 at 8:53am
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)
Dec 20, 2011 at 6:18pm
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;
}
Dec 21, 2011 at 2:41am
@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.
Dec 21, 2011 at 2:51am
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.
Dec 21, 2011 at 3:52am
o.O

How in heck, did mine fail?
It works on my compiler.
Last edited on Dec 21, 2011 at 3:55am
Dec 21, 2011 at 3:57am
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 Dec 21, 2011 at 4:10am
Dec 21, 2011 at 7:44am
the reason why i say its fine in practice is because its showing what not to do
Dec 22, 2011 at 1:55am
Nvm tried a different compiler and it worked. Thanks guys.

/solved
Topic archived. No new replies allowed.