Question

In this lab, you will write a program that reads a series name/value pairs, and stores...

In this lab, you will write a program that reads a series name/value pairs, and stores them in a pair of vectors. After the name/value pairs are read, it will then read names (until the input is exhausted) and print out the corresponding value for that name. If the name is not found in the list, "name not found" should be printed.

The names should be read as strings and stored as a vector<string>; the values should be read as integers and stored as a vector<int>. The name/value pairs are terminated with a pair whose value is 0.

That is, your program has two phases:

  1. Read name/value pairs until you read one with a value of 0.
  2. Read names and print out the corresponding value.

For example, if the input is

Knuth 1938
Dijkstra 1930
Ritchie 1941
Stroustrup 1950
Liskov 1939
Torwalds 1969
Wirth 1934
Hamilton 1936
Kernighan 1942
Turing 1912
Hopper 1906
Lovelace 1815
Noname 0

Stroustrup
Wirth
Turing
Wells
Hopper
Noname
Knuth

The output should be

1950
1934
1912
name not found
1906
name not found
1938

Not that the final name with the value 0 is NOT included in the list.

When reading the name/value pairs, if a name is read that is already in the list, the name is not added to the list a second time. Instead, the value is updated with the new value.

For example, if the input is

xyzzy 1
frobozz 2
xyzzy 3
frobozz 4
noname 0

xyzzy
frobozz

the output should be

3
4

Since we haven't learned much about error handling yet, you may assume that all input is valid; there is no need for error checking.

Hints:

  1. With every name/value pair read, use push_back() to add the name and value to the corresponding vector. That way, the value for names[i] will be in values[i].
  2. For the first loop (the one reading the name/value pairs), use a while (true) loop with a break statement.
  3. You may find it useful to write a function that searches the names list for a given name, and returns the corresponding index.

My professor want me to use this code

int findname(

vector<string> names;

string name)

{

for (....)

if(...)

return i;

return -1;

}

while (cin>> name)

{

for (int i=0, i<name.size();++i)

if names[i]==name)

cout<<dates[i] <<'\n'

cout<<dates[i] <<'\n;

}

Please help me as soon as possible!!!!

Thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <vector>
using namespace std;

/*
function to find the index of a name in vector names and return index
returns -1 if not found
*/
int find_index(vector<string> names,string name)
{
for(int i=0;i<names.size();i++) //iterating through the valid indexes of the vector
{
if(names[i]==name) //if name is found in vector
return i; //returning matching index
}
return -1; //if program reaches this point, name was not found in vector
}

/*
main function to accept inputs and print outputs
*/
int main()
{
vector<string> names; //creating vector to store names
vector<int> values; //creating vector to store values
string name; //to store name input from user
int value; //to store value input from user
while(true) //infinite while loop to accept inputs from user till 0 is entered
{
cin >> name >> value; //reading input
if(value==0) //if value is 0
break; //stopping loop
int index=find_index(names,name); //checking if name is already in vector
if(index==-1)
{
//pushing the values into vectors if name is new
names.push_back(name);
values.push_back(value);
}
else
values[index]=value; //else we update the value
}
cout << endl;
while(cin >> name) //taking input till input runs out
{
int index=find_index(names,name); //checking if name is in vector
if(index==-1)
cout << "name not found" << endl; //output
else
cout << values[index] << endl; //output
}
return 0;
}

Output:

Add a comment
Know the answer?
Add Answer to:
In this lab, you will write a program that reads a series name/value pairs, and stores...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 10.3 Name_year in C - phase 3 This lab is part 2 of a series of...

    10.3 Name_year in C - phase 3 This lab is part 2 of a series of 3 labs that will walk you through the process of creating a C struct and related functions that are the equivalent of the Name_year class you created in chapter 9. For this phase, we will Create an init_name_year() function that initializes a Name_year object. Move the object initialization logic from create_name_year() to init_name_year(). Create a function called compare_name_year() to compare two Name_year objects. The...

  • Write a java project that reads a sequence of up to 25 pairs of names and...

    Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes, street address, city, state, and 10-digit phone number for individuals. Store the data in an object designed to store a first name (string), last name (string), and postal code (integer), street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' . Assume each...

  • User Profiles Write a program that reads in a series of customer information -- including name,...

    User Profiles Write a program that reads in a series of customer information -- including name, gender, phone, email and password -- from a file and stores the information in an ArrayList of User objects. Once the information has been stored, the program should welcome a user and prompt him or her for an email and password The program should then use a linearSearch method to determine whether the user whose name and password entered match those of any of...

  • Write a program named text_indexing.c that does the following: Reads text and stores it as one...

    Write a program named text_indexing.c that does the following: Reads text and stores it as one string called text. You can read from a file or from the user. (In my implementation, I read only one paragraph (up to new line) from the user. With this same code, I am able to read data from a file by using input redirection (executable < filename) when I run the program. See sample runs below). You can assume that the text will...

  • Task 1: Write a Python program that takes as input from the user the name of a file containing po...

    python code: Task 1: Write a Python program that takes as input from the user the name of a file containing postcode/location information. Each line in the file consists of the postcode followed by a tab followed by a comma-separated list of locations that have that postcode. For example, the file Small.txt contains: 3015 Newport,South Kingsville,Spotswood 3016 Williamstown 3018 Altona,Seaholme 3019 3021 Albanvale,Kealba,Kings Park,St Albans Braybrook, Robinson Your program should create a list of postcode/location pairs with each pair stored...

  • Name_year_test.c #include "Name_year.h" #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> void print_name_year(const Name_year * ny) {   ...

    Name_year_test.c #include "Name_year.h" #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> void print_name_year(const Name_year * ny) {    printf("%s,%d\n", ny->name, ny->year); } int main() {    char * ny_format = "%s,%d\n"; #if PHASE >= 1    puts("Phase 1 tests");    // Make sure we can create an object and get the data back.    // We're creating the name as a local array so that we can    // make sure that create_name_year() does not merely make    // a copy of...

  • Any little bit of information will be helpful. Thank you. Problem Statement You are to develop a program to read Mo...

    Any little bit of information will be helpful. Thank you. Problem Statement You are to develop a program to read MotoGP rider information from an input file. You will need a MotoGpRider class whose instances will be used to store the statistics for a MotoGP motorcycle racer. The Rider class will have attributes for last name, first name, racing number, nation, motorcycle make, world championship points, world championship position, and an array that stores the number of top finishes (number...

  • Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names,...

    Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names, and read in course names from an input file. Then do the output to show each course name that was read in. See Chapter 8 section on "C-Strings", and the section "Arrays of Strings and C-strings", which gives an example related to this lab. Declare the array for course names as a 2-D char array: char courseNames[10] [ 50]; -each row will be a...

  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT