7 Replies - 393 Views - Last Post: 12 April 2011 - 03:37 PM Rate Topic: -----

#1 redrose92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 11-April 11

C++ programming...

Posted 12 April 2011 - 02:31 PM

#include<iostream>  

02 #include<cstdlib>       

03 using namespace std;  

04 void main ()  

05 {  

06     int x,y;  

07     cout<<"insert 2 numbers please"<<endl;  

08     cin>>x>>y;  

09    

10 do 

11 {  

12    

13 cout<<x++<<") "<<rand()%25<<endl;  

14 }  

15    

16     while (y!=x);  

17        

18         if(y<x)  

19             cout<<"LOW"<<endl;  

20         if(y>x)  

21            

22             cout<<"HIGH"<<endl;  

23         else 

24         cout<<"Congratulations"<<endl;  

25        

26        

27 } 



Write full documented C++ program that prompt the user to enter a number of three digits, then print “right” if the first digit is the largest number or print “middle” if the second digit is the largest number or print “left” if the third digit is the largest number or print “balanced” if there are two or three digits are equal. For example if the user enter 625 the output is “left”

the output is infinitve loop but i don't know where cxactly i was wronge.i couldn't notice where is the error exactly...sorry but i can't speak english very well so hope u understod what i mean :)

Is This A Good Question/Topic? 0
  • +

Replies To: C++ programming...

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: C++ programming...

Posted 12 April 2011 - 02:37 PM

Your do-while loop says "do while y is not equal to x". If the starting value of x is greater than y, y will never be equal to x because x keeps getting bigger and y never changes, so there is an infinite loop.
Was This Post Helpful? 2
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1745
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: C++ programming...

Posted 12 April 2011 - 02:55 PM

The code which you submitted has nothing to do with your question!!!!
You have used this code before in a thread which has been locked few minutes ago by moderators asking a different question!!!


Your question needs to check the three digits of the entered number. Then you may say:
num1 = x/100;
num2 = (x%100)/10;
num3 = (x%100)%10;

Then check which of these three numbers is greater!
Was This Post Helpful? 0
  • +
  • -

#4 redrose92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 11-April 11

Re: C++ programming...

Posted 12 April 2011 - 03:03 PM

View Postsmohd, on 12 April 2011 - 02:55 PM, said:

The code which you submitted has nothing to do with your question!!!!
You have used this code before in a thread which has been locked few minutes ago by moderators asking a different question!!!


Your question needs to check the three digits of the entered number. Then you may say:
num1 = x/100;
num2 = (x%100)/10;
num3 = (x%100)%10;

Then check which of these three numbers is greater!


Ooooopps ... this code is for another Qustion this qustion i was writing in it and i switched them :S

Write a program that generate a random number between 5 and 25 then prompt the user to guess this number, if the user guess the number print “Congratulations”, if the user’s number is less than the correct number then prints “LOW” else prints “HIGH”
Note: use #include<cstdlib>
use break with -1
this the qustion
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1745
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: C++ programming...

Posted 12 April 2011 - 03:07 PM

Then which is your question in this thread?
Was This Post Helpful? 0
  • +
  • -

#6 redrose92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 11-April 11

Re: C++ programming...

Posted 12 April 2011 - 03:13 PM

View Postsmohd, on 12 April 2011 - 03:07 PM, said:

Then which is your question in this thread?

my qustion is : is this how to use random number?
and the output is infnite loop but i don't know why exactly??
thank you for helping :)
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1745
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: C++ programming...

Posted 12 April 2011 - 03:25 PM

Question two has been answered by r.stiltskin and is clear. The question answer is available in this tutorial.
Now when you look at your code:
 do 

 {  
// in the loop you are just incrementing x
//and creating a random number to display 
// but you dont use it any where!!
cout<<x++<<") "<<rand()%25<<endl;  
 }  
    while (y!=x); 
// This is the end of the loop after this loop,
// if not infinite then x ==y. Then the output will always
// be the last one!


For more info about guessing look at this snippet
Was This Post Helpful? 1
  • +
  • -

#8 redrose92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 11-April 11

Re: C++ programming...

Posted 12 April 2011 - 03:37 PM

View Postsmohd, on 12 April 2011 - 03:25 PM, said:

Question two has been answered by r.stiltskin and is clear. The question answer is available in this tutorial.
Now when you look at your code:
 do 

 {  
// in the loop you are just incrementing x
//and creating a random number to display 
// but you dont use it any where!!
cout<<x++<<") "<<rand()%25<<endl;  
 }  
    while (y!=x); 
// This is the end of the loop after this loop,
// if not infinite then x ==y. Then the output will always
// be the last one!


For more info about guessing look at this snippet


mmm..OK got it :) thank you very much for helping :D
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1