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 19:44

>>28

It's meant to be a lottery program and the balls thing was a joke by the tutor, he gave us a few lines of code to start off and I never got round to changing that (although I did have an if statement to display either ball or balls; whichever applied) or the "Enter Number 1" even though people don't like arrays starting at anything other than 0 but I didn't even think of doing i/count+1 there.

A couple of questions if you don't mind.
Is there a reason why you used int mine[] = { 0, 0, 0, 0, 0, 0 }; as opposed to int mine[6]; in this case? As far as I know since you aren't using the numbers before they are entered there does it really matter if you zero them?
i = -1; is the same as i--; right?

And thank you for showing me a better way of entering numbers, much less typing required for the user. I guess I will use your code for entering numbers and have all my arrays start at 0 then use a shuffled pool of numbers for the machine's numbers :)

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