Connor Hutch
Connor Hutch

Reputation: 11

C++ Pulling information and looping to display

Not under standing looping for arrays. Looping through all of grab some or search. Can someone explain the process? Thanks in advance. Sorry if duplicate. I looked around and couldnt find a solid explaination that I could understand.

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

Upvotes: 0

Views: 70

Answers (1)

Thomas Matthews
Thomas Matthews

Reputation: 57753

In C++ arrays lose attributes when passed to functions. Those attributes are capacity and size (number of filled slots). You will need to pass this additional information for each array:

void addName(string names[],  unsigned int names_capacity, unsigned int names_size,
 string phones[], unsigned int phones_capacity, unsigned int phones_size)

To get around this, you can use std::vector. The std::vector knows its capacity and size, so you don't have to pass additional attributes to your function.

Also, if you use tolower or toupper before you compare, you only need to make one comparison:

char input; 
cout << "Do you have another contact to add? y or n" << endl;
cin >> input;
input = toupper(input);
if(input == 'Y')

When using strings, you can convert them to all uppercase or all lowercase by using std::transform, such as:

  std::transform(input.begin(),
                 input.begin(), input.end(),
                 tolower);

Upvotes: 1

Related Questions