Question

In: Computer Science

C++ Write a program that reads in a list of 10 names as input from a...

C++

Write a program that reads in a list of 10 names as input from a user and places them in an array.

The program will prompt for a name and return the number of times that name was

entered in the list. The program should output total number of instances of that name and then prompt for another name until the word done is typed in.

For this lab, use the string data type as opposed to char to store the names (i.e. don’t use c-strings).

Expected Input/ Output:

Enter name #1: Joe

Enter name #2: Sally

Enter name #3: Joe

Enter name #4: Sue

Enter name #5: Sally

Enter name #6: Adam

Enter name #7: Joe

Enter name #8: Adam

Enter name #9: Adam

Enter name #10: Joe

Who do you want to search for (enter done to exit)? Joe

There are 4 instances of the name Joe.

Who do you want to search for (enter done to exit)?Sally

There are 2 instances of the name Sally.

Who do you want to search for (enter done to exit)? Adam

There are 3 instances of the name Adam.

Who do you want to search for (enter done to exit)? Sue

There is one instance of the name Sue.

Who do you want to search for (enter done to exit)? John

John’s name does not exist in this list.

Who do you want to search for(enter done to exit)?done

Thank you for using my program.

Solutions

Expert Solution

#include <iostream>
#include <string>

using namespace std;

int main() {
    string names[10], name;
    for (int i = 0; i < 10; ++i) {
        cout << "Enter name #" << i + 1 << ": ";
        getline(cin, names[i]);
    }
    while (true) {
        cout << "Who do you want to search for (enter done to exit)? ";
        getline(cin, name);
        if (name == "done")
            break;
        int count = 0;
        for (int i = 0; i < 10; ++i) {
            if (names[i] == name) {
                count++;
            }
        }
        if (count == 0) {
            cout << name << "'s name does not exist in this list." << endl;
        } else if (count == 1) {
            cout << "There is one instance of the name " << name << "." << endl;
        } else {
            cout << "There are " << count << " instances of the name " << name << "." << endl;
        }
    }
    return 0;
}


Related Solutions

Write a C/C++ program which reads in a list of process names and integer times from...
Write a C/C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read the list until end of transmission (^d). You should read the list and represent it in a linked list data structure. You should use the alarm system call to schedule a timer...
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
Please code C# 10. Write a program that allows a user to input names and corresponding...
Please code C# 10. Write a program that allows a user to input names and corresponding heights (assumed to be in inches). The user can enter an indefinite number of names and heights. After each entry, prompt the user whether they want to continue. If the user enters true, ask for the next name and height. If the user enters false, display the name of the tallest individual and their height. Sample run: “Name?” James “Height?” 50 “Continue?” True “Name?”...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. Remember, try not to do the entire job all at once! First try input of a single number and make sure it works....
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Use C language Write a program that reads in a series of lines of input character...
Use C language Write a program that reads in a series of lines of input character by character (using getchar()). The first line of the input contains an integer which specifies the number of remaining lines of input, each of which contains a floating point number. The integer value on the first line can be read with scanf(), but all of the following lines can only be read with getchar(). Each line after the first contains a single floating point...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT