0

The program runs but has a logic error how can I fix it?
and I need to finish the header block on the function, but I don't know how to, can anyone help

#include <iostream>
using namespace std;

// Function prototype
bool searchList(long [], int, long);

// Constant for the array size
const int SIZE = 18;

int main()
{
   // Array of account numbers
   long accounts[SIZE] = 
      { 5658845,  4520125,  7895122,
        8777541,  8451277,  1302850,
        8080152,  4562555,  5552012,
        5050552,  7825877,  1250255,
        1005231,  6545231,  3852085,
        7576651,  7881200,  4581002 };
        
   long accountNumber;  // To hold an account number

   // Get an account number from the user.
   cout << "\nPlease enter a 7-digit account number:  ";
   cin  >> accountNumber;

   // Search the array for the number and indicate
   // whether it is valid or not.
   if (searchList(accounts, SIZE, accountNumber))   
      cout << "The number you entered is valid.\n";
   else
      cout << "The number you entered is invalid." << endl;

   return 0;
}

bool searchList(long list[], int size, long value)
{
   bool found = true; // Flag
   int count = 0;      // Loop counter

   // Step through the array as long as the value
   // is not found.
   while (!found && count < size)
   {
      if (list[count] == value)
         found = true;  // The value is found.
      count++;
   }
   
   return found;
}
2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Fbody
0

Try initializing found to false instead of true.

Also, since SIZE is a global constant, you shouldn't need to pass it to your function. It's automatically available, you can just use it.

0

Try initializing found to false instead of true.

Also, since SIZE is a global constant, you shouldn't need to pass it to your function. It's automatically available, you can just use it.

ok, I did the change true to false, but I know how to get the if else statement to work (the number you entered is invalid or valid) and I still get a warning and I need to finish the header block on the function, but I don't know what that is or where it is.

#include <iostream>
using namespace std; // Function prototypebool searchList(long [], int, long); // Constant for the array sizeconst int SIZE = 18; int main(){   // Array of account numbers   long accounts[SIZE] =       { 5658845,  4520125,  7895122,        8777541,  8451277,  1302850,        8080152,  4562555,  5552012,        5050552,  7825877,  1250255,        1005231,  6545231,  3852085,        7576651,  7881200,  4581002 };    long accountNumber;  // To hold an account number    // Get an account number from the user.   cout << "\nPlease enter a 7-digit account number:  ";   cin  >> accountNumber;    // Search the array for the number and indicate   // whether it is valid or not.   if (searchList(accounts, SIZE, accountNumber))         cout << "The number you entered is valid.\n";   else      cout << "The number you entered is invalid." << endl;    return 0;} bool searchList(long list[], int size, long value){   bool found = true; // Flag   int count = 0;      // Loop counter    // Step through the array as long as the value   // is not found.   while (!found && count < size)   {      if (list[count] == value)         found = true;  // The value is found.      count++;   }    return found;}#include <iostream>
using namespace std;

// Function prototype
bool searchList(long [], int, long);

// Constant for the array size
const int SIZE = 18;

int main()
{
   // Array of account numbers
   long accounts[SIZE] = 
      { 5658845,  4520125,  7895122,
        8777541,  8451277,  1302850,
        8080152,  4562555,  5552012,
        5050552,  7825877,  1250255,
        1005231,  6545231,  3852085,
        7576651,  7881200,  4581002 };
        
   long accountNumber;  // To hold an account number

   // Get an account number from the user.
   cout << "\nPlease enter a 7-digit account number:  ";
   cin  >> accountNumber;

   // Search the array for the number and indicate
   // whether it is valid or not.
   if (searchList(accounts, SIZE, accountNumber))   
      cout << "The number you entered is valid.\n";
   else
      cout << "The number you entered is invalid." << endl;

   return 0;
}

bool searchList(long list[], int size, long value)
{
   bool found = false; // Flag
   int count = 0;      // Loop counter

   // Step through the array as long as the value
   // is not found.
   while (!found && count < size)
   {
      if (list[count] == value)
         found = false;  // The value is found.
      count++;
   }
   
   return found;
}

Edited by rookanga: n/a

0

ok, I did the change true to false, but I know how to get the if else statement to work (the number you entered is invalid or valid) and I still get a warning and I need to finish the header block on the function, but I don't know what that is or where it is.

#include <iostream>
using namespace std; // Function prototypebool searchList(long [], int, long); // Constant for the array sizeconst int SIZE = 18; int main(){   // Array of account numbers   long accounts[SIZE] =       { 5658845,  4520125,  7895122,        8777541,  8451277,  1302850,        8080152,  4562555,  5552012,        5050552,  7825877,  1250255,        1005231,  6545231,  3852085,        7576651,  7881200,  4581002 };    long accountNumber;  // To hold an account number    // Get an account number from the user.   cout << "\nPlease enter a 7-digit account number:  ";   cin  >> accountNumber;    // Search the array for the number and indicate   // whether it is valid or not.   if (searchList(accounts, SIZE, accountNumber))         cout << "The number you entered is valid.\n";   else      cout << "The number you entered is invalid." << endl;    return 0;} bool searchList(long list[], int size, long value){   bool found = true; // Flag   int count = 0;      // Loop counter    // Step through the array as long as the value   // is not found.   while (!found && count < size)   {      if (list[count] == value)         found = true;  // The value is found.      count++;   }    return found;}#include <iostream>
using namespace std;

// Function prototype
bool searchList(long [], int, long);

// Constant for the array size
const int SIZE = 18;

int main()
{
   // Array of account numbers
   long accounts[SIZE] = 
      { 5658845,  4520125,  7895122,
        8777541,  8451277,  1302850,
        8080152,  4562555,  5552012,
        5050552,  7825877,  1250255,
        1005231,  6545231,  3852085,
        7576651,  7881200,  4581002 };
        
   long accountNumber;  // To hold an account number

   // Get an account number from the user.
   cout << "\nPlease enter a 7-digit account number:  ";
   cin  >> accountNumber;

   // Search the array for the number and indicate
   // whether it is valid or not.
   if (searchList(accounts, SIZE, accountNumber))   
      cout << "The number you entered is valid.\n";
   else
      cout << "The number you entered is invalid." << endl;

   return 0;
}

bool searchList(long list[], int size, long value)
{
   bool found = false; // Flag
   int count = 0;      // Loop counter

   // Step through the array as long as the value
   // is not found.
   while (!found && count < size)
   {
      if (list[count] == value)
         found = false;  // The value is found.
      count++;
   }
   
   return found;
}

Can you elaborate on the warning(s) you get?

I was able to compile and run without any issue, but all input is considered false even if valid. You need to change Line 48 from found = false; to found = true; . I'm not sure why you changed this line, it is an assignment statement and it was fine. I suggested you change the initialization statement which is now found on line 40. Do you understand the difference?

The function header in on Line 38, you've already completed it. A function header is nothing more than Line 1 of a function's definition/implementation.

Edited by Fbody: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.