### C++ Primer Exercise Problem

Hello Guys,

So I'm already stuck lol. Question is:

Write a program that prompts the user for 2 integers. Print each number in the range specified by these 2 integers.

I used the numbers 1 and 10 and I got the following output:
1, 2, 3, 4, 5, 6, 7, 8, 9,

The 1, and the , after 9 should not be here :S

 ``123456789101112131415161718`` ``````//This is what I wrote but it has some problem #include using namespace std; int main() { cout << "Enter 2 numbers to find all numbers in b/w them: " << endl; int n1=0, n2=0; cin >> n1 >> n2; while(n2>n1) { cout << n1 << ", "; ++n1; } return 0; } ``````
Something like this?

 ``` Enter 2 numbers to find all numbers in b/w them: 1 10 The numbers between 1 and 10 are: 2 3 4 5 6 7 8 9 ```

So.. this is the code
i dunno if it is the best solution but works
 ``1234567891011121314151617181920212223242526272829`` ``````#include using std::cout; using std::cin; using std::endl; int main(){ int number1, number2; cout<<"Enter 2 numbers to find all numbers in b/w them: "<>number1>>number2; cout<<"The numbers between "<
 ``123`` ``````std::cin >> n1 >> n2; while(++n1 < n2) std::cout << n1 << ((n1 + 1 < n2)?", ":"");``````
Last edited on
What would be a possible way of doing it with While Loop? I haven't studied forloop yet, the Example is before for loop.

Edit: And I don't know about counter either. :S
Last edited on
 And I don't know about counter either.
It is just the name of the variable. Rename it to n3 if you prefer cryptic names without any meaning.
I had posted solution using while loop earlier.
Last edited on

Here is with loop while
 ``123456789101112131415161718192021222324252627282930313233`` ``````#include using std::cout; using std::cin; using std::endl; int main(){ int number1, number2; cout<<"Enter 2 numbers to find all numbers in b/w them: "<>number1>>number2; cout<<"The numbers between "<

 ``` Enter 2 numbers to find all numbers in b/w them: 1 7 The numbers between 1 and 7 are: 2 3 4 5 6 ```
Last edited on
Edit: Okay, got it all thanks :D
Last edited on
there is no counter in my code.
In eyenrique code it holds current number (we use it instead of incrementing the lower bound)
counter is just an identifier for the variable, you can choose the name -identifier-
that you want for example i,j,k or counter my advice is that you pick up some good C++ book and learn the basics again, like VARIABLES give me your e-mail and i'll send you a link to download a good book.
The way you used the counter variable in the for parameter confused me, I didn't realize the variable. :)

eyen, check your PM for email :)

Cheers.
closed account (3qX21hU5)
 ``12`` ``````for (int index = ++lowNumber; index != highNumber; ++index) cout << index << " ";``````

Index is the variable that will print each time through the loop.

We initialize index with whatever the user enters for the lowNumber variable and add 1 to it.

Each time through the loop we increment index by 1.

We stop the loop when index is equal to highNumber. highNumber is the number the user enters for the second number.
Last edited on
 ``12345678910111213141516171819202122232425`` ``````#include using namespace std; int main() { cout << "Enter 2 numbers to find all numbers in b/w them: " << endl; int n1=0, n2=0; cin >> n1 >> n2; if ( n2 < n1 ) { int t = n1; n1 = n2; n2 = t; } while ( n2 > n1 ) { cout << ++n1; if ( n1 != n2 ) << ", "; } return 0; }``````
Last edited on
Great examples guys thanks a lot :)

I got one question which I forgot to ask though.

`int n1 = 0, n2 = 0;`

Why do we put n1 =0 instead of just int n1; ?

Why is the =0 needed?
There is no need for that in your code because they are overwritten a moment later. But it is a good idea to initialize your variables:
* If your code ever changes you won't bump into undefined behavour because you used it without initialization.
* Excessive assigment will be optimized away anyway.
Last edited on
Thanks MiiNiPaa :)
Okay, I tried to separate the numbers with a "," but after the last number the "," remains here. I tried at random(without actually knowing it lol) to use an if statement but it doesn't seem to work lol.

Output: 2, 3, 4, 5, 6, 7, 8, 9,
See the "," after 9?

 ``1234567891011121314151617`` ``````#include using namespace std; int main() { cout<<"Enter 2 numbers to find all numbers between them:"<>number1>>number2; cout<<"The numbers between "<
Check my code above or vlad from moscow one.
EDIT: vlad from moscow one does output upper bound :(
And it syntaxically wrong.
I realized, that you might doesn't know how to use ternary operator, so I replaced it with if:
 ``123456`` ``````std::cin >> n1 >> n2; while(++n1 < n2) { std::cout << n1; if (n1 + 1 < n2) std::cout << ", "; }``````
Last edited on
Great explanation MiiNiPaa. Thanks :)
I tried it and it worked perfect. You just missed ++n1 after std::cout<<", "; but it's just fine :D Thanks buddy :)

More question:
Why std::cout and not using namespace std;

How does using namespace std; affect the whole program in case it's more complex? This is a simple program, so obviously it's not a big deal, but what happens in case of bigger, more complex programs, where different namespaces are used? Would it be as follows:
using namespace std;
using namespace x;
using namespace y;
using namespace z;
...etc
?

Thanks :)
 You just missed ++n1
`while(++n1 < n2) {`
It is here. I didn't miss anything.
 How does using namespace std; affect the whole program in case it's more complex?
http://stackoverflow.com/a/1453605
Make sure that you read links to the problems some people encountered.
Ohh, so putting ++n1<n2 will keep looping it. I got it. There's so much to learn lol. Thanks :)
Topic archived. No new replies allowed.