Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ program

Name: Anonymous 2010-03-10 20:06

Made a short program that takes numbers, puts them into an array and compares them with each other. If any of your numbers are the same it gets you to type the numbers in again. This code seems to work fine but I though it's quite a lot for just comparing numbers, is there an easier/shorter way of comparing multiple numbers like this?

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
int count, test, mine[7], machine[7], count1, count2;
srand(time(NULL));
test=0;
count1=1;
count2=0;

while (test<1) {
     labelA:
    for (count=1;count<7;count++) {
        cout<<"Enter Number "<<count<<endl;
        cin>>mine[count];
        }
        while (count1<6) {
            for (count2=2;count2<7;count2++) {
                if (mine[count1]==mine[count2]) {
                    goto  labelA;
                    }
                    }
                    count1++;
                    for (count2=2;count2<7;count2++) {
                        if (mine[count1]==mine[count2]) {
                            goto  labelA;
                            }
                            }
                            count1++;
                            for (count2=3;count2<7;count2++) {
                                if (mine[count1]==mine[count2]) {
                                    goto  labelA;
                                    }
                                    }
                                    count1++;
                                    for (count2=4;count2<7;count2++) {
                                        if (mine[count1]==mine[count2]) {
                                            goto  labelA;
                                            }
                                            }
                                            count1++;
                                            for (count2=2;count2<7;count2++) {
                                                if (mine[count1]==mine[count2]) {
                                                    goto  labelA;
                                                    }
                                                    }
                                                    count1++;
                                                    test++;
                                                    }
                                                    }
return 0;
}

Name: OP 2010-03-18 17:54

Here's the whole program, almost done I think (we haven't been fully told what it must do). I know that srand can generate duplicate numbers but I'm undecided on how I should go about rectifying this; whether I should do like I have for the user entered numbers and keep trying (not really lottery like :<)or have a set pool of numbers, randomize these then pull out the first 6 and use them as the machine's numbers.


#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    int count, test, mine[7], machine[7], count1, count2, temp;
    srand(time(NULL));
    do {         //fills mine & machine arrays, ensuring no user entered numbers are the sames
        test=0; //resets test variable
        for (count=1;count<7;count++) { // runs 6 times
        cout<<"Enter Number "<<count<<endl; //prompts user to enter numbers
        cin>>mine[count];                  //assigns entered numbers to count array
        machine[count]=(rand()%49)+1;     //generates numbers for machine array
        }
        for (count1=1;count1<7;count1++) { //runs 6 times
            for (count2=count1+1;count2<7;count2++) { //runs 6 times
                if (mine[count1]==mine[count2]||mine[count1]==0||mine[count1]>49) { //runs if a number in mine array matches another, 0 or is higher than 49
                    test++;
                }
            }
        }
    }
    while (test>0); //contiues to run until the if statement isn't triggered in a loop
    for (count1=1;count1<7;count1++) { //runs 6 times
        for (count2=count1+1;count2<7;count2++) { //runs 6 times
            if (mine[count1]>mine[count2]) { //sorts mine array
               temp=mine[count1];
               mine[count1]=mine[count2];
               mine[count2]=temp;
            }
            if (machine[count1]>machine[count2]) { //sorts machine array
               temp=machine[count1];
               machine[count1]=machine[count2];
               machine[count2]=temp;
            }
        }
    }
    for (count=1;count<7;count++) { //runs 6 times
        cout<<mine[count]<<", "; //displays numbers from mine array
    }
    cout<<endl;
    for (count=1;count<7;count++) { //runs 6 times
        cout<<machine[count]<<", "; //displays numbers from machine array
    }
    cout<<endl;
    count=0;
    for (count1=1;count1<7;count1++) { //runs 6 times
        for (count2=1;count2<7;count2++) { //runs 6 times
            if (mine[count1]==machine[count2]) { //checks matches between entered and random generated numbers
                count++;
            }
        }
    }
    cout<<"You matched "<<count<<" Balls";
    return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List