| | |
Converting a Python Programme to C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 6
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Aug 2007
Posts: 129
Reputation:
Solved Threads: 15
I know Pyton and I'm bored, so here (done in 5 minutes, so...):
c++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <ctime> using namespace std; int main(int argc, char *argv[]) { srand(time(NULL)); int arand[3]; for(int i=0;i<4;++i) arand[i] = rand()%30; cout << "Enter 4 numbers between 0 and 30." << endl; for(int i=0;i<4;++i) { int x; cin >> x;cin.ignore(); if(x > 30 || x < 0) { cout << x << " is above 30 / below 0!"; return 1; } bool won = false; for(int w=0;w<4;++w) { if(arand[w] == x) { cout << "WINNER" << endl; won = true; break; } else { cout << "LOSER" << endl; //break; (do we need this? I dunno, didn't test it) } } if(won == true) break; } cout << "the computers numbers were:" << endl; cout << "---" << arand[0] << "---" << arand[1] << "---" << arand[2] << "---" << arand[3] << "---" << endl; system("PAUSE"); return EXIT_SUCCESS; }
Last edited by hacker9801; Jul 3rd, 2008 at 9:19 pm.
Don't yell at me if I'm wrong - I'm thirteen. :)
![]() |
Other Threads in the C++ Forum
- Previous Thread: Question about static varables.
- Next Thread: Read from multiple file and Write into 1 file using loop
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





