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: Anonymous 2010-03-18 20:22

>>29
Is there a reason why you used int mine[] = { 0, 0, 0, 0, 0, 0 }; as opposed to int mine[6];  in this case?
Just a personal design choice. +safety -speed probably.  You can safely disregard it here.

i = -1; is the same as i--; right?
No.  You might be thinking of i -= 1 which performs the same task as i-- and i = i - 1.  Based on your code, I was assuming you wanted the user to re-enter ALL their numbers if even one of them is duplicated. i-- would decrement the variable by one and have you re-enter the same number again on the loop. i = -1 would cause the loop to start inserting all the numbers in the array again. Depending on what your instructions were, or what your preference is, you could do either.

Also, I just thought of something that even I didn't see the first time: negative numbers.
if(mine[i] <= 0 || mine[i] > 49)

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