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

Recommended Answers

All 2 Replies

We are not going to write the code for you, but if you are having problems with this, then ask us

commented: Yes! +17

I know Pyton and I'm bored, so here (done in 5 minutes, so...):

#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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.