Understand while loop and storing arrays.

Hi! First of all I'm new to programming, yeah thats why it's the beginners forum, and try to sort things out. I have taken a distance course so much of the help comes from youtube and a learning book (that I find hard to understand). So I have watch several videos and read through this forum so many times but I can't figure out to store my arrays while I have a menuselection. I can store the arrays when I'm not doing my while loop, but as soon as I try to enter it in my while loop nothing gets stored. I guess it has something to do with the loop but I can't figure it out. I want to have a menu so the user can decide what they want to do. Any help to just point me in the right direction would be helpful.

- Dont mind the search and delete function.
- Why not use vectors? Because that the task says not to.
- Why have you done several choices in menu instead of taking on one at the time so you learn? You are right. I should, and I have a shorter phoonebook with just the options add contact and see contact. The storing is still the same. I learn as I progress. One thing at time. Therefore I just focus on the storing, then I will continue with the searching.



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  #include <iostream>
#include <istream>

using namespace std;


    struct contact
    {
    string name;         //Uppgifter i telefonboken. Namn, address och telefonnummer.
    string adress;
    string phone;
    };



int main(){

char MenuSelection;

    while(true){
    cout << "-------------------Welcome to your Phonebook!-------------------" << endl;                                         //Välkomstmeny med förljande användarval 1 kontaktlista, 2 lägga till kontakt, 3 avsluta program.
    cout << "--------------------What do you want to do?-------------------" << endl;
    cout << "Press [1] to see all contacts" << endl;
    cout << "Press [2] to add a contact" << endl;
    cout << "Press [3] to search a contact " << endl;
    cout << "Press [4] to delete a contact." << endl;
    cout << "Press [5] to exit your phonebook" << endl;
    cin >> MenuSelection;

contact phonebook[10];
int i;



                        if (MenuSelection == '1'){                      // Alla kontakter visas

                            for(int i = 0 < 10; i++;){

                            phonebook[0].name = "Daniel Pettersson";
                            phonebook[0].adress = "Dreamgate 47";
                            phonebook[0].phone = "123456789";

                            cout << phonebook[0].name << ", " << phonebook[0].adress << "." << endl;
                            cout << phonebook[0].phone << "." << endl;
                            cout << " " << endl;

                            phonebook[1].name = "Didrik Petrovic";
                            phonebook[1].adress = "Kvarter Guldbaggen 8";
                            phonebook[1].phone = "987654321";

                            cout << phonebook[1].name << ", " << phonebook[1].adress << "." << endl;
                            cout << phonebook[1].phone << "." << endl;
                            cout << " " << endl;

                            phonebook[2].name = "Dan Pettson";
                            phonebook[2].adress = "Guldbaggegatan 8";
                            phonebook[2].phone = "246897531";

                            cout << phonebook[2].name << ", " << phonebook[2].adress << "." << endl;
                            cout << phonebook[2].phone << "." << endl;
                            cout << " " << endl;

                            phonebook[3].name = "David Persson";
                            phonebook[3].adress = "Guldbaggevaegen 8";
                            phonebook[3].phone = "135798642";

                            cout << phonebook[3].name << ", " << phonebook[3].adress << "." << endl;
                            cout << phonebook[3].phone << "." << endl;
                            cout << " " << endl;

                            cout << "Contact " << i << endl;
                            cout << "Name: " << phonebook[i].name << endl;
                            cout << "Adress: " << phonebook[i].adress << endl;
                            cout << "Phone: " << phonebook[i].phone << endl;

                            break;
                            }
                        }
                                    else if (MenuSelection == '2'){

                                        cout << "Add a contact" << endl;              //Lägga till en kontakt

                                            for(int i=0; i<10; i++){
                                            cout << "Contact " << i << endl;
                                            cout << "Name: " << endl;
                                            cin >> phonebook[i].name;
                                            cout << "Adress: " << endl;
                                            cin >> phonebook[i].adress;
                                            cout << "Phonenumber: " << endl;
                                            cin >> phonebook[i].phone;


                                          break;
                                            }

                                        }


                                                        else if (MenuSelection == '3'){

                                                        cout << "Search a contact" << endl;                 //Söka efter en kontakt


                                                        }


                                        else if (MenuSelection == '4'){

                                        cout << "Delete a contact" << endl;                 //Söka efter en kontakt
                                        }


                            else if (MenuSelection == '5'){              //Avsluta program
                            cout << "Good bye" << endl;
                            break;
                            }


            else
            cout << "Invalid number. Please try again" << endl;  //Om man anger fel siffra
            cout << " " << endl;
            }

}
Without checking the rest of the code, move L30 to L19. I don't think L31 is needed.

Every-time around the while loop, all variables defined within the loop are re-defined. So the values from the previous iteration are 'lost'.
So move line 30 before line 20.

Add Before line 20:

int contact_count = 0;

for if (MenuSelection == '1') your code should look like this:
1
2
3
4
5
6
7
8
for(int i = 0 < contact_count; i++;){ // Note: contact_count instead of 10!

                            cout << phonebook[i].name << ", " << phonebook[i].adress << "." << endl;
                            cout << phonebook[i].phone << "." << endl;
                            cout << " " << endl;

                            }
                            break; // Note: break needs to be outside the loop! 


for if (MenuSelection == '2'):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

if(contact_count < 10) // Note: requires 'int contact_count = 0;' before line 20!
{
                                        cout << "Add a contact" << endl;              //Lägga till en kontakt

                                            cout << "Contact " << contact_count << endl; // Note: contact_count
                                            cout << "Name: " << endl;
                                            cin >> phonebook[contact_count].name; // Note: [contact_count]
                                            cout << "Adress: " << endl;
                                            cin >> phonebook[contact_count].adress; // Note: [contact_count]
                                            cout << "Phonenumber: " << endl;
                                            cin >> phonebook[contact_count].phone; // Note: [contact_count]
++contact_count;

}
else
  cout << "No more free entries in phonebook" << endl;

                                          break;
As a first refactor, consider:

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
#include <iostream>
#include <string>

using namespace std;

constexpr size_t MaxEntry {10};

struct Contact {
    string name;
    string address;
    string phone;

    Contact() {}
    Contact(const string& nam, const string& addr, const string& pho) : name(nam), address(addr), phone(pho) {}
};

int main() {
    Contact phonebook[MaxEntry] {};
    size_t entries {4};

    phonebook[0] = Contact("Daniel Pettersson", "Dreamgate 47", "123456789");
    phonebook[1] = Contact("Didrik Petrovic", "Kvarter Guldbaggen 8", "987654321");
    phonebook[2] = Contact("Dan Pettson", "Guldbaggegatan 8", "246897531");
    phonebook[3] = Contact("David Persson", "Guldbaggevaegen 8", "135798642");

    for (char menuSelection {}; menuSelection != '5'; ) {
        cout << "\n-------------------Welcome to your Phone book!-------------------\n";
        cout << "--------------------What do you want to do?-------------------\n";
        cout << "\nPress [1] to see all contacts\n";
        cout << "Press [2] to add a contact\n";
        cout << "Press [3] to search a contact\n";
        cout << "Press [4] to delete a contact\n";
        cout << "Press [5] to exit your phone book\n";
        cout << "Enter option: ";
        cin >> menuSelection;
        cin.ignore();

        switch (menuSelection) {
            case '1':
                for (size_t i {}; i < entries; ++i) {
                    cout << "\nContact " << i + 1 << '\n';
                    cout << "Name: " << phonebook[i].name << '\n';
                    cout << "Address: " << phonebook[i].address << '\n';
                    cout << "Phone: " << phonebook[i].phone << '\n';
                }
                break;

            case '2':
                if (entries < MaxEntry) {
                    cout << "Add a contact\n";
                    cout << "Contact " << entries << '\n';
                    cout << "Name: ";
                    getline(cin, phonebook[entries].name);
                    cout << "Address: ";
                    getline(cin, phonebook[entries].address);
                    cout << "Phone number: ";
                    getline(cin, phonebook[entries].phone);
                    ++entries;
                } else
                    cout << "Phone book full\n";

                break;

            case '3':
                cout << "Search a contact\n";
                break;

            case '4':
                cout << "Delete a contact\n";
                break;

            case '5':
                cout << "Good bye\n";
                break;

            default:
                cout << "Invalid option. Please try again\n";
                break;
        }
    }
}

Many thanks man. I you helped understand a lot. I still use a big while loop cuz I haven't learn about switch yet. But that code you wrote is way "sexier" than mine. Many thanks again.
Instead of having a bunch of if/else statements a switch reduces the number of comparisons needed, and tends to be more compact in layout.

https://www.learncpp.com/cpp-tutorial/switch-statement-basics/
https://www.learncpp.com/cpp-tutorial/switch-fallthrough-and-scoping/

If one does "old school" Desktop WinAPI programming a switch is used A LOT for delegating the various tasks the app does.

Dantrew wrote:
Why not use vectors? Because that the task says not to.

If only more people asking for help and assistance would be more forthcoming about what parts of C++ can and can't be used. Right off the bat any help I'd offer I know what type of container to use. :)

@Dantrew, I suggest being consistent with your formatting and indentations would be helpful. The current layout makes it very hard to read with all the "excessive" whitespace.

This is just a friendly suggestion, not a command "you MUST DO!" :D
Last edited on
Topic archived. No new replies allowed.