Ok I have gotten myself started and seem to be COMPLETELY lost from here. I just have no clue as to how to finish this program I've tried but don't know where to go from here. Any suggestions would be greatly appreciated. I am supposed to write a program that
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.

And here is the code I have so far:

#include <cstdlib>
#include <iostream>

using namespace std;
int main()
{
        cout << "This program written for CS102 Online.\n";
        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};
        int searchLIst(int [], int, int);
}	
 
        {
        int results;

    //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;
}
 

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

		



{
    system("PAUSE");
    return 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.

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

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;
}

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;
}

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.

You created a new thread with the same question. Look there.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.