Converting a Python Programme to C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 6
Reputation: LaurenceB is an unknown quantity at this point 
Solved Threads: 0
LaurenceB LaurenceB is offline Offline
Newbie Poster

Converting a Python Programme to C++

 
0
  #1
Jul 3rd, 2008
Hi, I have written a program in python, and am looking to translate it into c++ code, here is the program:

import random

# You've got to guess 4 numbers between 0 and 30, if you get it right the program will output "WINNER" and tell you the computer's numbers


x = random.randint(0,30)
y = random.randint(0,30)
z = random.randint(0,30)
t = random.randint(0,30)

randnum = [x, y, z, t]
print "Enter 4 numbers between 0 and 30:"
for l in range(1, 5):
prompt = str(l) + " -->"
choice = int(raw_input(prompt))
if choice in randnum:
print "WINNER"
break
# -----------
else:
print "LOSER"

print "The Computer's Numbers Were:"
print "---",x,"---", y,"---", z,"---", t,"---"

exit
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 61
Reputation: AceofSpades19 is on a distinguished road 
Solved Threads: 10
AceofSpades19's Avatar
AceofSpades19 AceofSpades19 is offline Offline
Junior Poster in Training

Re: Converting a Python Programme to C++

 
1
  #2
Jul 3rd, 2008
We are not going to write the code for you, but if you are having problems with this, then ask us
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 129
Reputation: hacker9801 is on a distinguished road 
Solved Threads: 15
hacker9801 hacker9801 is offline Offline
Junior Poster

Re: Converting a Python Programme to C++

 
0
  #3
Jul 3rd, 2008
I know Pyton and I'm bored, so here (done in 5 minutes, so...):
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. srand(time(NULL));
  9.  
  10. int arand[3];
  11. for(int i=0;i<4;++i) arand[i] = rand()%30;
  12.  
  13. cout << "Enter 4 numbers between 0 and 30." << endl;
  14. for(int i=0;i<4;++i) {
  15. int x;
  16. cin >> x;cin.ignore();
  17.  
  18. if(x > 30 || x < 0) {
  19. cout << x << " is above 30 / below 0!";
  20. return 1;
  21. }
  22.  
  23. bool won = false;
  24. for(int w=0;w<4;++w) {
  25. if(arand[w] == x) {
  26. cout << "WINNER" << endl;
  27. won = true;
  28. break;
  29. }
  30. else {
  31. cout << "LOSER" << endl;
  32. //break; (do we need this? I dunno, didn't test it)
  33. }
  34. }
  35.  
  36. if(won == true) break;
  37. }
  38.  
  39. cout << "the computers numbers were:" << endl;
  40. cout << "---" << arand[0] << "---" << arand[1] << "---" << arand[2] << "---" << arand[3] << "---" << endl;
  41.  
  42. system("PAUSE");
  43. return EXIT_SUCCESS;
  44. }
Last edited by hacker9801; Jul 3rd, 2008 at 9:19 pm.
Don't yell at me if I'm wrong - I'm thirteen. :)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC