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

Dani AI

Generated

Quick, practical notes for converting ’s Python game to robust C++ — and a few fixes for ’s helpful but buggy submission.

  • Range and randomness: Python’s random.randint(0,30) returns 0..30 inclusive. Using rand()%30 produces 0..29, so it doesn’t match the original. Prefer C++11’s <random> (mt19937 + uniform_int_distribution) to get a correct, portable inclusive range and better quality RNG.
  • Safety and UB: ’s code declares the array with the wrong size and writes four elements; that causes undefined behavior. Use std::array<int,4> or std::vector<int> so size and indexing are explicit.
  • Logic flow: printing “LOSER” inside the inner comparison loop leads to multiple "LOSER" messages per guess. Scan the computer’s numbers first; print “WINNER” and break if found, otherwise print “LOSER” once per guess.
  • Portability and input: avoid system("PAUSE") and srand(time(NULL)) in modern C++. Validate input (handle non-integer entries) rather than letting the program exit on bad input. Decide whether duplicate computer numbers are allowed — Python’s code permits duplicates; if you want unique numbers, explicitly enforce uniqueness.

Useful snippets (illustrative):

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(0, 30);
int r = dist(gen);           // yields 0..30 inclusive

For membership testing, prefer std::find (keeps duplicates) or std::unordered_set (faster lookups but removes duplicates):

if (std::find(comp.begin(), comp.end(), guess) != comp.end()) { /* winner */ }
else { /* loser */ }

Finally: decide expected behavior (unique vs duplicates, how many LOSER messages), add input re-prompting, and use C++11 idioms for safety and clarity. These small changes make the translation correct, readable, and portable.

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.