Range of Numbers | C++ Primer Problem

• (2 Pages)
• 1
• 2

27 Replies - 23118 Views - Last Post: 12 July 2011 - 09:46 PMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=239095&amp;s=456c17a332d3fe50b03b0f6e9b5caf86&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 12:45 AM

According to the suggestions of fellow members of DIC, I got myself the book C++ Primer. Got stuck in Exercise 1.18

Quote

Write a program that prompts the user for two numbers and writes each number in the range specified by the two numbers to standard output.

Here's the code I wrote:

```#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int a, b;
cout << "Enter a number: ";
cin >> a;
cout << "Enter another number: ";
cin >> b;
if(a>>B)/>{
while(b<=a){
b=b++;
}
cout << b <<endl;
}
if(b>>a){
while(a<=B)/>{
a=a++;
}
cout <<a<<endl;
}
if(a==B)/>{
cout <<"Both Numbers are same"<< endl;
}
_getch();

}

```

But it doesn't work !

Note: In my program all the b are in small. But whenever I try to do the b small in the above code, it gets capitalized. May be a bug in the forum?

This post has been edited by Jeet.in: 12 July 2011 - 12:51 AM

Is This A Good Question/Topic? 0

Replies To: Range of Numbers | C++ Primer Problem

#2 PlasticineGuy

• mov dword[esp+eax],0

Reputation: 281
• Posts: 1,436
• Joined: 03-January 10

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 12:56 AM

```while (b <= a) {
b = b++; // pointless; b++ increments b anyway, so no need to assign
}
std::cout << b; // here you only print b at the end of the loop; is this really what you want?
```

This post has been edited by PlasticineGuy: 12 July 2011 - 12:56 AM

#3 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 01:09 AM

No, I want to show all the increments of b, say if the first digit is 3 and the second digit is 5 I want to print 3, 4, 5.

#4 PlasticineGuy

• mov dword[esp+eax],0

Reputation: 281
• Posts: 1,436
• Joined: 03-January 10

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 01:10 AM

Right, so where do you think the print statement should go?

#5 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 01:16 AM

```while(b<=a){b++;
cout <<b;
}
```

Here? But still it doesn't print any value.

The solution is somewhat achieved if I change the operators little bit:

```if(a>=B)/>{
while(b<=a){
b++;
cout << b << endl;
}

}
if(b>=a){
while(a<=B)/>{
a++;
cout << a << endl;
}

```

But still it has a logic error, it prints one digit more then intended, say if I enter 3 and 5, it prints 3, 4 5, 6.
And if I enter 5, 3, it prints 4, 5, 5, 6, 7

This post has been edited by Jeet.in: 12 July 2011 - 01:24 AM

#6 PlasticineGuy

• mov dword[esp+eax],0

Reputation: 281
• Posts: 1,436
• Joined: 03-January 10

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 01:21 AM

```if (b>>a)
```
What exactly do you think you're doing here?

#7 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 01:26 AM

PlasticineGuy, on 12 July 2011 - 01:21 AM, said:

```if (b>>a)
```
What exactly do you think you're doing here?

I want to tell the compiler if b is greater than a. Is the method wrong?

#8 PlasticineGuy

• mov dword[esp+eax],0

Reputation: 281
• Posts: 1,436
• Joined: 03-January 10

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 01:27 AM

>> is the bitwise shift right; it shifts all the bits in a number right by a specified number.
> is the greater-than operator.

#9 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 01:39 AM

Thanks ! I studied the difference but forgot it. Stupid me !

```#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int a, b;
cout << "Enter a number: ";
cin >> a;
cout << "Enter another number: ";
cin >> b;
if(a>B)/>{
while(b<=a){
b++;
cout << b << endl;
}

}
if(b>a){
while(a<=B)/>{
a++;
cout << a << endl;
}

}
if(a==B)/>{
cout <<"Both Numbers are same"<< endl;
}
_getch();
```

But There's few limitations, viz:

How to get past that?

#10 PlasticineGuy

• mov dword[esp+eax],0

Reputation: 281
• Posts: 1,436
• Joined: 03-January 10

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 01:45 AM

Think about it; are there any parts of your code that are unnecessary?

#11 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 03:09 AM

Thanks mate ! Did it ! Your way of answering is impressive, really puts the learner back into the code and sets him searching for the error instead of waiting for a solution. Here's the final code:

```#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Enter a number: ";
cin >> a;
cout << "Enter another number: ";
cin >> b;
if(a>B)/>{
while(b<a){
b++;
cout << b << endl;
}

}
else if(b>a){
while(b>a){
--b;
cout << b << endl;
}

}
else{
cout <<"Both Numbers are same !"<< endl;
}
cin.clear();
cin.ignore();
cin.get();

}

```

Do you suggest any other improvements?

This post has been edited by Jeet.in: 12 July 2011 - 03:17 AM

#12 PlasticineGuy

• mov dword[esp+eax],0

Reputation: 281
• Posts: 1,436
• Joined: 03-January 10

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 03:28 AM

Looks OK to me, though I would have written a++; instead of b--;.

#13 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 04:00 AM

b-- just to make it according to the question . Thanks a lot for the great help !

#14 CTphpnwb

• D.I.C Lover

Reputation: 3786
• Posts: 13,715
• Joined: 08-August 08

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 05:58 AM

According to the question you want each of the numbers in the range specified by a and b. There are an infinite number of numbers, but if we assume they're talking about integers then you could do something like this:

```if(b > a) { // You could also use swap() here.
temp = b;
b = a;
a = temp;
}
for(int i = a; i <=b; i++) {

```

#15 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 08:11 AM

CTphpnwb, on 12 July 2011 - 05:58 AM, said:

According to the question you want each of the numbers in the range specified by a and b. There are an infinite number of numbers, but if we assume they're talking about integers then you could do something like this:

```if(b > a) { // You could also use swap() here.
temp = b;
b = a;
a = temp;
}
for(int i = a; i <=b; i++) {

```

Will you please explain a bit? Didm't get the code, am just a beginner .