Ok, so I'm working on my school project and I have a class that stores the Customers information, in the main file it then asks for the customers ID number and then checks to see if the value is true or false from the criteria entered. However, it is just returning false even though the value is true.

Here is part of the main program (where it checks):

switch (option)
    {
       case '1': char critera;
                 system("cls");
                 cout << "Search for customer: " << endl;
                 cout << ">> ";
                 cin >> critera;
                 if(customers(critera) == true)
                   cout << "Username Found!";
                 else
                   cout << ">> There is no user with this ID\n\n\n\n\n\n";
                 
       break;

And this is the function that checks it against the values stored in the class:

char customers(char critera)
{ 
// run an if statement to check the users
   Customer s[255];
   s[0].setID(1);
   s[1].setID(2);
   for(int i=0; (i<255); i++)
   {
       if(s[i].getID() == critera)
       return true;
       return false;
   }
               
}

Anyone got any suggestions? I really need this to work :( thanks guys!

Recommended Answers

All 6 Replies

move line 11 down outside that loop because it is causing the loop to execute only once.

Is there more to that function then what you posted? Because it is pretty much a worthless function the way it is posted. Why declare an array of 255 Customer classes when you only need two?

Hey thanks, I'll try that! That's all the function does at the minute, all I want it to do is return "true" or "false" and then in the "main" file it will open a display_customer() function that will display the customers information from a txt file

Why is your customers() returning char?

Your customers function is a total mess.

Apart from the errors mentioned by Ancient Dragon and jonsca above I felt these were wrong.

1) The criteria should be matched with a previously available list of customers but here you are creating a new customer list which totally makes no sense.
2)setID() class function is setting an integer itself because char of value 1 and 2 make no sense in this context.So obviously getID() would return an integer itself and you are matching it with a character as

if(s[i].getID() == critera)

So in theory, what I'm trying to achieve is:

1. Set the customers_id (stored in class)

s.setID(1) // set customer_id as 1

2. Get the search criteria (main.cpp)

cout << "Please enter customer ID";
cin >> customer_id;

3. Search the customers from a list

if(s.getID() == critera)
  cout << "Correct";
else 
  cout << "Wrong";

How else could I search to see if the criteria matches with the one stored in the class? Thanks x

So in theory, what I'm trying to achieve is:

1. Set the customers_id (stored in class)

s.setID(1) // set customer_id as 1

2. Get the search criteria (main.cpp)

cout << "Please enter customer ID";
cin >> customer_id;

3. Search the customers from a list

if(s.getID() == critera)
  cout << "Correct";
else 
  cout << "Wrong";

How else could I search to see if the criteria matches with the one stored in the class? Thanks x

Read csurfer's last post again regarding char vs. int. The snippets you're posting aren't making it clear what the types are. There's a decent chance that you're mixing ints and char. For example, '1' as a char is 49 in the ascii table. There are two completely different values.

char a = '1';
char b = 1;

if (a == b)
{
  // this code won't execute
}

So you may have some type issues. It's not totally clear. This is on top of the problem mentioned earlier (i.e. you're declaring a NEW customer list inside of the function, as mentioned by csurfer). You should probably be PASSING the customer list to the function or something like that.

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.