0

I need help with a program

I Need to do a program in C++ with function where the user try too guess a 4 digit random number using 2 function, the first one is generateTheNumber it just generate the random number, and the second one called guessTheNumber is going to do the following things: make the user give on input a number, compare the inserted number with the random number and if the inserted number is greater than the random one the program will print: the inserted number is too big, otherwise if the inserted number is lower than the random number the program will say: the inserted number is too small. I tried to do in this way but It doesn't work, i think there is a problem with the random number because is value isn't "carried" in the main function. Can someone help me with this? Thanks in advance and sorry if i wrote bad in English https://code.sololearn.com/c3z77uD6Mj17/?ref=app

9th Mar 2020, 5:29 PM
Massimiliano Messina
Massimiliano Messina - avatar
10 ответов
+ 1
void guessTheNumber (int RandomNumber, int a){ int c=0, Number; do{ cout<<"\n insert a four digit number"; cin>>Number; c++; if (Number<RandomNumber) { cout<<"the inserted number is too small"; } else if (Number>RandomNumber) { cout<<"the inserted number is too big"; }else if (Number==RandomNumber) { break; } }while(c!=a); cout<<endl; } part:2
9th Mar 2020, 6:17 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 2
I think this will work, #include <ctime> #include <iostream> #include <cstdlib> using namespace std; int generateTheNumber(); void guessTheNumber(int RandomNumber, int a); int n; int main(){ const int ATTEMPS=5; char Choice; do { generateTheNumber (); guessTheNumber (n, ATTEMPS); cout<<"do you want to play again Y/N:?"<<endl; cin>>Choice; }while(Choice=='y'||Choice=='Y'); system("PAUSE"); return 0; } int generateTheNumber (){ srand (time(NULL)); n=rand()%10+1; return n; } part:1
9th Mar 2020, 6:16 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 1
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ but it generates numbers between 0and 10?
9th Mar 2020, 6:35 PM
Massimiliano Messina
Massimiliano Messina - avatar
+ 1
Massimiliano Messina you can handle it,you can make it any number you want.
9th Mar 2020, 6:38 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 1
Massimiliano Messina yes but rand()%9999+1000 is also fine.
9th Mar 2020, 7:45 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
0
Yes it works
9th Mar 2020, 6:34 PM
Massimiliano Messina
Massimiliano Messina - avatar
0
How can i generare 4 digit numbers only?
9th Mar 2020, 6:59 PM
Massimiliano Messina
Massimiliano Messina - avatar
0
I did n=rand()%9000+1000; is it correct?
9th Mar 2020, 7:04 PM
Massimiliano Messina
Massimiliano Messina - avatar
0
I need a help this prgrm
11th Mar 2020, 4:18 PM
sowmya shree
0
class restuarent: def __init__(self): print("*************************************************************************") print("\t\t\t hotel xyz mall") print("*************************************************************************") print("itemno\t\t\t items \t\t\t amount") print("-------------------------------------------------------------------------") self.foodserial=[1,2,3,45,6,7,8,9,10,11,12] self.fooditems=['idly\t\t\t','masaladosa\t\t\t','poori\t\t\t','vada\t\t\t'] self.foodprice=[20,40,30,40,50,55] self.order=[] self.quantity=[] L=len(self.fooditems) for i in range (0,L): print(self.foodserial[i],self.fooditems[i],self.foodprice[i]) print("enter the placeorder:") def takeorder(self): choice=1 while(1): choice=int(input("enter the item order:")) if(choice == 0): break self.orde
11th Mar 2020, 4:18 PM
sowmya shree