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-14 15:17

I wasn't looking for handouts or anything, I tried this before and it didn't work (obviously I typed something wrong) but this does the same as my original code did (plus it compares 5&6 too, if anyone spotted that).


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

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

    do {
        test=0;
        for (count=1;count<7;count++) {
            cout<<"Enter Number "<<count<<endl;
            cin>>mine[count];
        }
        for (count1=1;count1<7;count1++) {
            for (count2=count1+1;count2<7;count2++) {
                if (mine[count1]==mine[count2]) {
                test++;
                }
            }
        }

    } while (test>0);
    return 0;
}


I'm not used to the text boards, I didn't realise it'd keep code::blocks' silly indentation. From what you've guys said it could be even shorter but that far surpasses my days worth of knowledge.

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