Loop doesn't seem to function properly

This is a discussion on Loop doesn't seem to function properly within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; const int MAX_P = 4; const int MAX_C = 10; int inArray(int a[], int ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    67

    Loop doesn't seem to function properly

    Code:
    #include <iostream>
    
    using namespace std;
    
    const int MAX_P = 4;
    const int MAX_C = 10;
    int inArray(int a[], int sc)
    {
        for(int i = 0; i < MAX_P; i++)
        {
            if(sc == a[i]) {
                return 1;
            } else {
                return 0;
            }
        }
    } 
    
    int main() {
        int computer[MAX_P], user[MAX_P];
        int i;
        
        for (i = 0; i < MAX_P; i++) { 
            computer[i] = (rand() % MAX_C);
            cout << computer[i];
        } 
        
        cout << endl << endl;
        
        for (i = 0; i < MAX_P; i++) {
            cout << "Geef een nummer voor " << i << ": ";
            cin >> user[i];
        }
        
        for (i = 0; i < MAX_P; i++) {
            if (inArray(computer, user[i])) {
                if (user[i] == computer[i]) {
                    cout << "getal op de goede plek" << endl;
                } else {
                    cout << "getal wel in de reeks" << endl;
                }
            } else {
                cout << "getal niet in reeks" << endl;
            }
        }
        
        cin.get();
        return 0;
    }
    The purpose of this program is for the PC to 'generate' 4 numbers (and put them in computer[]).
    Our user has to fill in 4 numbers and the computer matches them and tells the user which ones are on the right position or not even part of the array.

    I really can't grasp why, but this code doesn't seem to work out..
    If i lower MAX_P to 1 it works, but with any higher number it justs checks the first number for validity.

    Maybe someone can help me with this?

    thanx in advance
    Last edited by TeQno; 01-31-2005 at 05:21 PM.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    If i lower MAX_P to 1 it works, but with any higher number it justs checks the first number for validity.
    Yes, because your code says
    Code:
      if(sc == a[i]) {
                return 1;
            } else {
                return 0;
            }
    The function will return 0 if the first element of a[] doesn't match sc. Remove the return 0 from the loop and put it after the loop, so if you don't get a match from any of the numbers, the function returns zero.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-14-2008, 09:08 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21