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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//This is what I wrote but it has some problem
#include <iostream>
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
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
26
27
28
29
#include<iostream>
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: "<<endl;
cin>>number1>>number2;

cout<<"The numbers between "<<number1<<" and "<<number2<<" are: "<<endl;

number1+=1;
number2-=1;

for(int counter=number1;counter<=number2;counter++){
        cout<<counter<<' ';
}//end loop for

cout<<endl;


return 0; //indicates successful termination
}//end main 
1
2
3
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
read the @MiiNiPaa's answer

Here is with loop while
ask for doubt
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
26
27
28
29
30
31
32
33
#include<iostream>
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: "<<endl;
cin>>number1>>number2;

cout<<"The numbers between "<<number1<<" and "<<number2<<" are: "<<endl;

number1+=1; //now you increment number1 by 1 e.g you entered 1 and 7
                       //so now number1 is equal to 2

while(number1<number2){ //loop from number1 -now is 2- through 1 before 
                                           //number2

        cout<<number1<<" "; //print the numbers b/w number1 and number2
number1++;
}//end loop while

cout<<endl;


return 0; //indicates successful termination
}//end main



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)
1
2
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
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()
{
    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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main()
{
    cout<<"Enter 2 numbers to find all numbers between them:"<<endl;
    int number1, number2;
    cin>>number1>>number2;
    cout<<"The numbers between "<<number1<<" and "<<number2<<" are:"<<endl;

    number1+=1;
    while(number1<number2)
    {
        cout<<number1<<", ";
        ++number1;
    }
    return 0;
}
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:
1
2
3
4
5
6
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.