943,948 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 713
  • C++ RSS
Jul 3rd, 2008
0

Converting a Python Programme to C++

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LaurenceB is offline Offline
6 posts
since Jun 2008
Jul 3rd, 2008
1

Re: Converting a Python Programme to C++

We are not going to write the code for you, but if you are having problems with this, then ask us
Reputation Points: 61
Solved Threads: 10
Junior Poster in Training
AceofSpades19 is offline Offline
61 posts
since Jun 2008
Jul 3rd, 2008
0

Re: Converting a Python Programme to C++

I know Pyton and I'm bored, so here (done in 5 minutes, so...):
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 59
Solved Threads: 16
Junior Poster
hacker9801 is offline Offline
129 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Question about static varables.
Next Thread in C++ Forum Timeline: Read from multiple file and Write into 1 file using loop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC