I cannot figure out why my program isnt working

// Names
#include <iostream>
#include <string>
using namespace std;
void getNames (string list[], const int MAX);
void sortNames (string list[], int size);
void displayNames (string list[], const int MAX);
void findName (string list[], const int MAX);
void removeName (string list[], const int MAX);
int main ()
{
const int MAX = 20;
string list[MAX];
string choice;
cout << "Choose from the list: " << endl;
cout << "Enter names: 'e', display names: 'd', sort names: 's', find name: 'f', remove name: 'r', quit program: 'q'." << endl;
cin >> choice;
if (choice == "e" || "E")
{
void getNames (string list[], const int MAX);
}
else if (choice == "d" || "D")
{
void displayNames (string list[], const int MAX);
}
else if (choice == "s" || "S")
{
void sortNames (string list[], int size);
}
else if (choice == "f" || "F")
{
void findName (string list[], const int MAX);
}
else if (choice == "r" || "R")
{
void removeName (string list[], const int MAX);
}
else if (choice == "q" || "Q")
{
return 0;
}
while (choice != "e" && "E" && "d" && "D" && "s" && "S" && "f" && "F" && "r" && "R" && "q" && "Q")
{
cout << "Error, chose an option from the list: ";
cin >> choice;
}
}
void getNames (string list[], const int MAX)
{
for (int i=1; i < MAX; i++)
{
cout << "Enter a name: ";
cin >> list[MAX];
}
return;
}
void sortNames (string list[], int size)
{
bool flag;
do
{
flag = 0;
for (int count = 0; count < size-1; count++)
{
if (list[count] > list[count+1])
swap (list[count],list[count+1]);
flag = 1;
}
}
while (flag == 1);
return;
}
void displayNames (string list[], const int MAX)
{
cout << list[MAX];
return;
}
void findName (string list[], const int MAX, int size)
{
string name;
cout << "Please enter a name to find:";
cin >> name;
int first = 0;
int last = size - 1;
bool found = false;
while (!found && first <= last)
{
int middle = (first + last) / 2;
if (list[middle] == name)
{
found = true;
cout << list[middle] << " is on the list." << endl;
}
else if (list[middle] > name)
last = middle - 1;
else
first = middle + 1;
}
return;
}
void removeName (string list[], const int MAX)
{
string Empty;
string nameRemove;
cout << "Please enter the name to remove from the list: ";
cin >> nameRemove;

for(int i = 0; i < MAX; i++)
{
if(list[i] == nameRemove)
{
list[i] = Empty;
}
else if(i == MAX - 1)
{
cout << "Could not find that name to remove.";
}
return;
}
}
Hi,

First of all please always code tags: http://www.cplusplus.com/articles/z13hAqkS/

Read the tutorial, in particular this section about flow control.
http://www.cplusplus.com/doc/tutorial/control/

Consider using a switch statement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
switch (choice) {
   case "e":
   case "E";
       getNames (string list[], const int MAX); // function call does not have return type
       break;

   case "d":
   case "D":
       displayNames (string list[], const int MAX);
       break;

// ....

   default:
      std::cout << "Invalid Selection" ;
      break;
 }



1
2
3
4
5
6
7
void getNames (string& list[], const int MAX) //pass by ref so value is changed in main
{
for (int i = 0; i < MAX; i++) // arrays start at 0
{
cout << "Enter a name: ";
cin >> list[i]; // store in the i'th position not MAX
}


I have a pathological hatred for constructs which look anything like this:

while (choice != "e" && "E" && "d" && "D" && "s" && "S" && "f" && "F" && "r" && "R" && "q" && "Q")

Even if they are coded correctly, they are an abhorrent disaster.
Last edited on
If you do stay with the if statements instead of using switch, you need to add choice == to the right hand side of the ||. Also remove the types from the arguments in the function calls.

1
2
3
4
if (choice == "e" || choice =="E")
{
    getNames (list, MAX);
}
Line 18,22,26,30,34,38,42: C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y'))

('y') always evaluates to 1 (true), therefore the if statement is always true.

Lines 20,24,28,32,36: These are function declarations, not function calls. Remove the void, string and const int.

Line 53: This will cause an out of bounds reference. You're inputting to list[20] on every iteration. You want:
 
  cin >> list[i];


Line 67: I think you want the flag = 1; within the if statement.

Line 62,67,70: As a matter of style, you should use true and false rather than 0 and 1 when using a bool.

Line 75: This is going to cause a bounds errors. This is going to cout << list[20] which doesn't exist. Did you intend to output each of the names in the arrray?

Line 118: This is going to cause the function to exit on the first iteration.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.



Topic archived. No new replies allowed.