Question

Exercise Five: (Parallel Array) In this exercise create two string arrays. One array will hold your name, another will hold c

Text in red, entered by the user. Sample Output-1: Enter your name: name: name 2 Enter your password: password2 Its matching
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<string>
using namespace std;
//function to search for the name in the array
int search(string name[], string uname, int n)
{
   //iterate over the array
   for(int i = 0;i < n;i++)
   {
       //if the name in array if equal to the given name, return the index
       if(name[i] == uname)
       {
           return i;
       }
   }
   //if the name not found in the array return -1
   return -1;
}

int main()
{
   //create two parallel arrays
   string name[] = {"name1", "name2", "name3"};
   string password[] = {"password1", "password2", "password3"};
  
   //prompt the user to enter name
   cout<<"Enter your name: name: ";
   string uname;
   cin>>uname;
   //prompt the user to enter password
   cout<<"Enter your password: ";
   string pass;
   cin>>pass;
   //calculate the size of the array
   int n = sizeof(name)/sizeof(name[0]);
   //search the name in the names array
   int index = search(name, uname, n);
   if(index != -1)
   {
       //if the current name and password are at the same index in parallel arrays
       if(password[index] == pass)
       {
           //match them
           cout<<"It's matching!"<<endl;
       }
       else
       {
           //otherwise they are not matching
           cout<<"It's NOT matching!"<<endl;
       }
   }
   //if the name not found in the array, they are not matching
   else
   {
       cout<<"It's NOT matching!"<<endl;
   }
}

EN Activities Terminal - Thu 06:28 deepika@deepika-TravelMate-P243-M: -/Desktop File Edit View Search Terminal Help deepika@d

If you have any doubts please comment and please don't dislike.

Add a comment
Know the answer?
Add Answer to:
Exercise Five: (Parallel Array) In this exercise create two string arrays. One array will hold your...
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
  • Design a program using a RAPTOR flowchart that has two parallel arrays: a String array named...

    Design a program using a RAPTOR flowchart that has two parallel arrays: a String array named people that is initialized with the names of twenty of your friends, and a String array named phoneNumbers that is initialized with your friends’ phone numbers. The program should allow the user to enter a person’s name. It should then search for that person in the people array. If the person is found, it should get that person’s phone number from the phoneNumbers array...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

  • Array Processing - Dictionary Program NOTE: Review the parallel array example from class Another method of...

    Array Processing - Dictionary Program NOTE: Review the parallel array example from class Another method of "Sequentially Searching an Array" is covered in the book in Chapter 8. Using Notepad, write psuedocode ONLY for the following situation. Create and load an array with the following 7 values. Add one more word (of your own choosing) for a total of 8 words. biff comely fez mottle peruke bedraggled quisling Create a second array (parallel array). To hold the defintions to these...

  • Write a program that has an array of at most 50 strings that hold people’s names...

    Write a program that has an array of at most 50 strings that hold people’s names and phone numbers. You can assume each string’s length is no more than 40. You may make up your own strings, or use the following: "Becky Warren, 555-1223" "Joe Looney, 555-0097" "Geri Palmer, 555-8787" "Lynn Presnell, 555-1212" "Holly Gaddis, 555-8878" "Sam Wiggins, 555-0998" "Bob Kain, 555-8712" "Tim Haynes, 555-7676" "Warren Gaddis, 555-9037" "Jean James, 555-4939" "Ron Palmer, 555-2783" The program should ask the user...

  • In Java, Please do not forget the ToString method in the Student class! In this exercise,...

    In Java, Please do not forget the ToString method in the Student class! In this exercise, you will first create a Student class. A Student has two attributes: name (String) and gradePointAverage (double). The class has a constructor that sets the name and grade point average of the Student. It has appropriate get and set methods. As well, there is a toString method that prints the student's name and their grade point average (highest is 4.0) You will then write...

  • Array Class Assignment

    This is a two-part assignment. It is better to submit the files for both parts when you are done than to submit part 1 first and part 2 later.In the Array.h file includes the class definition and member functions implementations.PART 1Write And Test An Array Class [Array.TestDriver.cpp] Write a data structures class. The resulting class can be used in any program in place of a C++ array, in case you want the advantages of range safety and built-in size tracking.Requirements....

  • In this lab, you will create a program to help travelers book flights and hotel stays that will b...

    In this lab, you will create a program to help travelers book flights and hotel stays that will behave like an online booking website. The description of the flights and hotel stays will be stored in two separate String arrays. In addition, two separate arrays of type double will be used to store the prices corresponding to each description. You will create the arrays and populate them with data at the beginning of your program. This is how the arrays...

  • 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...

  • qbasic is the programming language Chapter 8 Do Programming Exercise 8-3 om page 414. Create a...

    qbasic is the programming language Chapter 8 Do Programming Exercise 8-3 om page 414. Create a string array to hold the months of the year. Call it anything you want to just make sure it's a string array is at the end of the name, e.g. months). Create a DATA statement that holds the month names: DATA "jan","Feb", "Mar" etc. Next you'll need a FOR loop to READ the DATA into the array, Call a SUB to get the rainfall...

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