0

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;
    }
}
  • What part of the code is it that you don't understand ? – slugo Feb 25 '16 at 16:02
  • 2
    `for (int i = 0; i < sizeof(names); i++)` isn't correct. `sizeof` will return the size of a `string*` (in bytes), not a size of the array. Read up on `sizeof` operator, and passing arrays to functions (and array decaying). – Algirdas Preidžius Feb 25 '16 at 16:02
  • You have a `goto` that goes backwards, and it jumps from inside a loop to a place before it. Please don't do that. `goto` should be used in **very** limited circumstances. This is not one of them. That said... What is your question exactly? – Fabio says Reinstate Monica Feb 25 '16 at 16:02
  • 1
    Read up on `continue` and `break` statements. They would resolve your `goto` issue. – Thomas Matthews Feb 25 '16 at 16:16
  • Why don't you use `std::vector` and just add as many conacts as you like without any size checking while adding data. – Simon Kraemer Feb 25 '16 at 16:27
  • Or at least use `std::array`.... – Simon Kraemer Feb 25 '16 at 16:31
  • Some info on ["What is array decaying"](https://stackoverflow.com/questions/1461432/what-is-array-decaying) – Patryk Feb 25 '16 at 16:33
  • My question is when looping through arrays, what does all the information in the loop mean, so I can use to make search and show all and save etc. – Connor Hutch Feb 25 '16 at 16:50

1 Answers1

1

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);
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154