Range of Numbers | C++ Primer Problem

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 9133 Views - Last Post: 12 July 2011 - 09:46 PM Rate Topic: ***-- 2 Votes

#1 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • 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 !

Where am I doing wrong? Please help !

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  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

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

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 12:56 AM

Your while loop is wrong.
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

Was This Post Helpful? 1
  • +
  • -

#3 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • 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?
Was This Post Helpful? 1
  • +
  • -

#5 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • 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?
Was This Post Helpful? 1
  • +
  • -

#7 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 01:26 AM

View PostPlasticineGuy, 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?
Was This Post Helpful? 0
  • +
  • -

#8 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

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

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 01:27 AM

Your operators are mixed up.
>> is the bitwise shift right; it shifts all the bits in a number right by a specified number.
> is the greater-than operator.
Was This Post Helpful? 1
  • +
  • -

#9 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • 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:

Posted Image

How to get past that?
Was This Post Helpful? 0
  • +
  • -

#10 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • 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?
Was This Post Helpful? 1
  • +
  • -

#11 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • 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

Was This Post Helpful? 0
  • +
  • -

#12 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • 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--;.
Was This Post Helpful? 1
  • +
  • -

#13 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • 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 !
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2993
  • View blog
  • Posts: 10,342
  • 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++) {


Was This Post Helpful? 0
  • +
  • -

#15 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Range of Numbers | C++ Primer Problem

Posted 12 July 2011 - 08:11 AM

View PostCTphpnwb, 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 :dontgetit: .
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2