4
Contributors
7
Replies
10
Views
10 Years
Discussion Span
Last Post by Ancient Dragon
0

first thing you need to do is rearrange the program so that it contains valid functions. Pay attention to where the opening and closing braces are located. You can not start a new function before ending the previous one. Try to align the braces as neatly as possible in order to make this easier to see.

0

Hello,

First of all to solve the problem, you need to analyze all the things you must do. You will need to:

  1. Create an array with account numbers in it
  2. Get user's input
  3. Validate the input
  4. Print out the result

Except for the first step, all the others should be in their own seperate functions. Write and compile each step at a time, so it's easier to spot errors in your code. Remember, it's always easier to work on a project when you already have working code (even if it's quite minimal).

For coding your search function, it should be relatively simple. Create a loop, which loops through each element of the account number array. Each time it loops, it compares the number entered with an element in the list. If it finds a match, it returns true immediately, exiting the loop. If finishes the loop without finding anything, it returns -1.

Some comments on your code:

Your prototype for this function exists inside main . Are you sure this is really what you want to do?

int searchLIst(int [], int, int);

Try putting it before main . (And by the way, I think you've got a typo in the function name)

And this should be part of the main() function:

//Search the array for charge account 
    results = searchList(accounts, arrSize, number);
    
    // If searchList returned -1, then charge account was not found.
    if (results == -1)
       cout << "The number you entered is invailid.\n";
    else
    {
        cout << "The charge account number you entered is valid and found in element "
        cout << (results + 1) << " in the array." << endl;
        }
        return 0;

(The way you had it, it looked kind of seperated.)

Hope this helps

0

I revised the code a little bit and got a little farther but now it wont compile?

#include <cstdlib>
#include <iostream>

using namespace std;
int main()
{
        
        const int size = 18;
        long accounts[size] = { 5658845, 8080152,1005231, 4520125, 4562555, 6545231, 7895122, 5552012,
                                   3852085, 8777541, 5050552, 7576651, 8451277, 7825877, 7881200, 1302850,
                                   1250255, 4581002};
        long number;
        
}    
 
        int searchList(int [], int, int);
        int results;

    //Search the array for charge account 
    results = searchList(accounts, arrSize, number);
    
    
    number = get_number();    // subroutine will ask user for a number
    cout << "Please enter your charge account number: \n";
    
    // If searchList returned -1, then charge account was not found.
    if (results == -1)
       cout << "The number you entered is invailid.\n";
    else
    {
        cout << "The charge account number you entered is valid and found in element "
        cout << (results + 1) << " in the array." << endl;
        }
        return 0;
}
int searchList(int list[], int numElems, int value)
{
    int index = 0;
    int position = -1;
    bool found = false;
    
    while (index < numElems && !found)
    {
          if (list[index] == value)
          found = true;
          position = index;
          }
          index++;
}
return position;
}
0

I have rearanged my code but I still cannot compile my program. I am supposed to write a program that lets the user enter a charge account number. The program should determine if the number is valid by checking for it in the following list:

5658845
8080152
1005231
...
etc.

The list of numbers above should be initialized in a single dimensional array. A
simple linear search should be used to locate the number entered by the user. If
the user enters a number that is in the array, the program should display a
message saying the number is valid. If the user enters a number that is not in
the array, the program should display a message indicating the number is invalid.

CAn anyone see what I've done wrong? I've taken the suggestions of others and switched some
things around but it still won't compile, here is my code:

#include <cstdlib>
#include <iostream>

using namespace std;

int searchList(int [], int, int);
const int size = 18;

int main()
{
    long accounts[size] = { 5658845, 8080152,1005231, 4520125, 4562555, 6545231, 7895122, 5552012,
                            3852085, 8777541, 5050552, 7576651, 8451277, 7825877, 7881200, 1302850,
                            1250255, 4581002};
    long number;
    
    number = get_number();    // subroutine will ask user for a number
    cout << "Please enter your charge account number: \n";
    cin >> get_number
    
    int results;
    //Search array for account number
    results = searchList(accounts, size, number);
    // If searchList returned -1, then charge account was not found.
    if (results == -1)
       cout << "The number you entered is invailid.\n";
    else
    {
        cout << "The charge account number you entered is valid and found in element "
        cout << (results + 1) << " in the array." << endl;
        }
        return 0;
    


    
    
    
    
    
    
}
int searchList(int list[], int numElems, int value)
{
    int index = 0;
    int position = -1;
    bool found = false;
    
    while (index < numElems && !found)
    {
          if (list[index] == value)
          found = true;
          position = index;
          }
          index++;
}
return position;
}
0

1)

number = get_number();    // subroutine will ask user for a number

Not if it's not defined it won't ;)
2)

cin >> get_number

a. Where is get_number defined?
b. Missing semi-colon.
3)

cout << "The charge account number you entered is valid and found in element "

Missing semi-colon.
4)

while (index < numElems && !found)
    {
          if (list[index] == value)
          found = true;
          position = index;
          }
          index++;
}
return position;
}

Messed up, you want:

while (index < numElems && !found)
{
          if (list[index] == value)
          {
               found = true;
               position = index;
          }
          index++;
}
return position;
}

5) Indentation could be...better.

0

I have rearanged my code but I still cannot compile my program.

what are the errors? Pay attention to them. Example:

1. line 20: get_number() is undefined. You have to defined variables and functions before they can be used. Did you forget to prototype it?

2. line 24 has a syntax error. But look at the line above it and you will see that you forgot to end the statement with a semicolon.

3. line 26. parameter 1 is incorrect data type. Expects an array of ints and you are trying to pass an array of longs. Either change the array or change the function prototype to accept an array of longs.

4. The next few errors occur because you failed to end statements with a semicolon. Pay attention to details.

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.