Loops and Arrays

OK I just looked at guides on how to do loops with arrays and did a rough sketch, I am getting errors on my if statements and my show all works but doesnt. Can someone explain what is going on?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

void allContacts(string names[], string phones[])
{
    cout << "Showing all contacts... Press Q to go back to main menu" << endl;

    for (int i = 0; i < 100; i++)
    {
        cout << names[i] << ":" << phones[i] << endl;

        if(names[i] || phones[i] == "q" || "Q")
        {
            mainMenu();
        }
    }
}

void addName(string names[], string phones[])
{
    cout << "To quit press Q" << endl;

    for (int i = 0; i < 100; i++)
    {
        cout << "Enter contact name: " << endl;
        cin >> names[i];
        cout << "Enter contact number: " << endl;
        cin >> phones[i];

        if(names[i] || phones[i] == "q" || "Q")
        {
            mainMenu();
        }
    }

}

void searchName(string names[], string phones[])
{
    string name;
    cout << "Enter Name: ";
    cin >> name;

    cout << "Search for a name or Press B to go back to main menu" << endl;

    for (int i = 0; i < 100; i++){
        if (name == names[i])
        {
            cout << names[i] << " 's phone number is: " << phones[i] << endl << endl;
            if(names[i] || phones[i] == "b" || "B")
            {
                mainMenu();
            }
        }
    }
}

int main()
{
    string names[100];
    string phones[100];
    bool keepGoing = true;

    while(keepGoing == true)
    {
        cout << "1- Add a New Contact" << endl;
        cout << "2- Search By Name" << endl;
        cout << "3- Display All" << endl;
        cout << "0- Exit" << endl << endl;

        int choice;

        cout << "What's your choice?: " << endl;
        cin >> choice;

        cout << endl;

        if (choice == 1) // Add Names
        {
            addName(names, phones, count);
        }

        if (choice == 2) //Search Names
        {
            searchName(names, phones);
        }

        if (choice == 3) // Show All Contacts
        {
            allContacts(names, phones);
        }

        if (choice == 0)
        {
            cout << "Exiting PhoneBook...";
            break;
        }
    }
}
my show all works but doesn't

What does that mean? It's an oxymoron.

Line 8, 12, 50: Don't assume you always have exactly 100 entries in the array. What if the user has only entered 20? You're going to be displaying 80 garbage entries. Ditto for your search function. You need to pass the number of valid entries to each function.

Line 36, 56: Where is mainmenu()?

Line 34,54: You can't do an or statement like that. Each condition must be fully qualified.
 
if (names[i] == 'q' || names[i] == 'Q' || phones[i] == "q" || phones[i] == "Q")


Line 27: What if the user calls addNames() more than once? You're going to start replacing entries starting at 0 rather than continuing from where the user left off.

Lines 82-101: These are a perfect candidate for a switch statement.

Line 84: You're trying to pass three arguments to a function that only takes two arguments.

Last edited on
So I want to do all of them on showAll, so Im thinking some kind of size of the array but cant seem to figure it out.
I changed a few things about the code. Also, question. How would you at the end of a function, send a user back to main, Would I have to place main in through as a parameter?

Thanks in advance.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

void allContacts(string names[], string phones[])
{
    cout << "Showing all contacts... Press Q to go back to main menu" << endl;
}

void addName(string names[], string phones[])
{
    bool keepGoing;
    string input;

    beginning:
    for (int i = 0; i < sizeof(names); i++)
    {
        cout << "Enter contact name: ";
        cin >> names[i];
        cout << "Enter contact number: ";
        cin >> phones[i];
        cout << "Do you have another contact to add? y or no" << endl;
        cin >> input;
        if(input == "y" || input == "Y")
        {
            goto beginning;
        }

        if(input == "n" || input == "N")
        {
            cout << "Contacts you have entered: " << endl;
            cout << names[i] << " : " << phones[i] << endl;
        }
    }
}

void searchName(string names[], string phones[])
{
    string name;
    cout << "Enter Name: ";
    cin >> name;

    cout << "Search for a name or Press Q to go back to main menu" << endl;

    for (int i = 0; i < sizeof(names); i++){
        if (name == names[i])
        {
            cout << counter << names[i] << " 's phone number is: " << phones[i] << endl;
        } else {
            cout << "No results found";
        }
    }
}

int main()
{
    string names[100];
    string phones[100];
    int choice;

    cout << "============================" << endl;
    cout << "=== Welcome to PhoneBook ===" << endl;
    cout << "============================" << endl;

    cout << "1- Add a New Contact" << endl;
    cout << "2- Search By Name" << endl;
    cout << "3- Display All" << endl;
    cout << "0- Exit" << endl;

    cout << "Select a number: " << endl;
    cin >> choice;

    switch(choice)
    {
    case 1:
        addName(names, phones);
        break;
    case 2:
        searchName(names, phones);
        break;
    case 3:
        allContacts(names, phones);
        break;
    case 0:
        cout << "Exiting PhoneBook...";
            break;
    }
}
so I'm thinking some kind of size of the array

You need a counter of the number of names that have successfully been added to names and phones.

1
2
// After line 63
  int cnt_names = 0;

You will have to add that ad an argument to each of your functions.
For add_name(), you will have to update the value. You can do that in one of two ways. 1) Pass cnt by reference. 2) Have addName return the updated values.

Line 29: gotos should be avoided.

Line 89: You should have a default case in the event the user doesn't enter a valid value.

line 29: sizeof(name) isn't going to work.

edit:
line 19: You should start your loop at num_names to avoid overwriting existing entries.

lines 68-90: You probably want to put these lines inside a loop so that a user can do multiple functions.

line 22: cin >> stops at whitespace. If you want the user to be able to enter names with embeded space, you want to use getline.

line 29: If the user enters "y", you're going to overwrite the entry the user just entered.

lines 34-35: You probably don't want these lines inside your loop. ALso, why not just use allContacts here?
Last edited on
Topic archived. No new replies allowed.