Question

C++ language I need to update this code with the following: ask the user for how...

C++ language

I need to update this code with the following:

ask the user for how many structures they would like. Once you get the number, allocate an array
of pointers. Once they have been created, proceed to loop through and get the data as usual, and display
it back to the screen.

struct record
{
   int age;
   string name;
};

int check(record r[], int n, string nm)
{
   for (int i = 0; i < n; i++)
   {
       if (r[i].name == nm)
           return 0;
   }
   return 1;
}

int main()
{
   std::cout << "Program 3-4:Repeating Names Check: " << endl;
   std::cout << "________________________________________________\n" << endl;

   cout << "How many records would you like?: ";
   int n;
   while (!(cin >> n))
   {
       cin.clear();
       while (cin.get() != '\n') continue;
       std::cout << "Invald entry, please try again: ";
   }
   record *rec = new record[n];
   for (int i = 0; i < n; i++)
   {
       cin.ignore();
       cout << "What is the name for record " << (i + 1) << "?: ";
       string nm;
       getline(cin, nm);
       while (check(rec, i, nm) == 0)
       {
           cout << "THERE CAN BE ONLY ONE. What is the name for record " << (i + 1) << "?: ";
           getline(cin, nm);
       }
       rec[i].name = nm;
       int ag;
       cout << "What is the age for record " << (i + 1) << "?: ";
       while (!(cin >> ag))
       {
           cin.clear();
           while (cin.get() != '\n') continue;
           std::cout << "Invald entry, please try again: ";
       }
       rec[i].age = ag;
   }
   cout << "The data is: " << endl;
   for (int i = 0; i < n; i++)
   {
       cout << (i + 1) << " Name: " << rec[i].name << ", Age: " << rec[i].age << endl;
   }
}

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

If you have any doubts, please give me comment...

If you want to declare memory as dynamically with malloc, replace with following statments:

#include <iostream>

#include <cstdlib>

#include <string>

using namespace std;

struct record

{

int age;

string name;

};

int check(record r[], int n, string nm)

{

for (int i = 0; i < n; i++)

{

if (r[i].name == nm)

return 0;

}

return 1;

}

int main()

{

std::cout << "Program 3-4:Repeating Names Check: " << endl;

std::cout << "________________________________________________ "

<< endl;

cout << "How many records would you like?: ";

int n;

while (!(cin >> n))

{

cin.clear();

while (cin.get() != ' ')

continue;

std::cout << "Invald entry, please try again: ";

}

// record *rec = new record[n];

record *rec = (record *)malloc(sizeof(record)*n);

for (int i = 0; i < n; i++)

{

cin.ignore();

cout << "What is the name for record " << (i + 1) << "?: ";

string nm;

getline(cin, nm);

while (check(rec, i, nm) == 0)

{

cout << "THERE CAN BE ONLY ONE. What is the name for record " << (i + 1) << "?: ";

getline(cin, nm);

}

rec[i].name = nm;

int ag;

cout << "What is the age for record " << (i + 1) << "?: ";

while (!(cin >> ag))

{

cin.clear();

while (cin.get() != ' ')

continue;

std::cout << "Invald entry, please try again: ";

}

rec[i].age = ag;

}

cout << "The data is: " << endl;

for (int i = 0; i < n; i++)

{

cout << (i + 1) << " Name: " << rec[i].name << ", Age: " << rec[i].age << endl;

}

}

Add a comment
Know the answer?
Add Answer to:
C++ language I need to update this code with the following: ask the user for how...
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
  • C++ language I am having some trouble with user validation, can someone look at my code...

    C++ language I am having some trouble with user validation, can someone look at my code and tell me what I am doing wrong: char firstInital, lastInitial;    int userAge;    std::cout << "Program 1-2: Get user initials and age in days\n ";    std::cout << "-------------------------------------------------------------------------\n";    std::cout << "Please enter the first letter of your first name: \n " << flush;    std::cin >> firstInital;    std::cout << "Please enter the first letter of your last name: \n...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the...

    (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the date of birth.            **fall.sort(compare_DOB); //Here lies your error** #include <fstream> #include <iostream> #include <string> #include <list> #include <cctype> using namespace std; const char THIS_TERM[] = "fall.txt"; const string HEADER = "\nWhat do you want to do ?\n\n" "\t. Add a new friend name and DOB ? \n" "\t. Edit/Erase a friend record ? \n" "\t. Sort a Friend's record ? \n"...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • ​c++ program that takes user input from the console and store the data into a linked list.

    c++ program that takes user input from the console and store the data into a linked list. The input made of 4 objects associated to a car: model, make, mileage and year. My program should take the how many inputs the user want to make, followed by the inputs themselves. After the input, my program should print the data inside the linked list. So far, the issue is that my program print some of the input, but not all. Input sample:3HondaSentra89002017ToyotaCamri1098271999NissanSentra87261987current...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

  • Code is in C++: Im wondering how i can make the program continue to ask the...

    Code is in C++: Im wondering how i can make the program continue to ask the user to enter Y/N with the if statment. Is it possible with an If statment? #include <iostream> using namespace std; int main() { float usDollars,cYuan; float *Dollars; char choice; Dollars = &usDollars; while(usDollars >= 0){ cout <<"Enter the amount in U.S Dollars: "; cin >> usDollars; cout << usDollars<< " U.S Dollar in Chinese Yuan is :"<<*Dollars*7.09<<endl; if (choice == 'y' || choice ==...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • Coding in c++

    I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() {    char option;    vector<movie> movies;     while (true)     {         printMenu();         cin >> option;         cin.ignore();         switch (option)         {            case 'A':            {                string nm;                int year;                string genre;                cout << "Movie Name: ";                getline(cin, nm);                cout << "Year: ";                cin >> year;                cout << "Genre: ";                cin >> genre;                               //call you addMovie() here                addMovie(nm, year, genre, &movies);                cout << "Added " << nm << " to the catalog" << endl;                break;            }            case 'R':            {                   string mn;                cout << "Movie Name:";                getline(cin, mn);                bool found;                //call you removeMovie() here                found = removeMovie(mn, &movies);                if (found == false)                    cout << "Cannot find " << mn << endl;                else                    cout << "Removed " << mn << " from catalog" << endl;                break;            }            case 'O':            {                string mn;                cout << "Movie Name: ";                getline(cin, mn);                cout << endl;                //call you movieInfo function here                movieInfo(mn, movies);                break;            }            case 'C':            {                cout << "There are " << movies.size() << " movies in the catalog" << endl;                 // Call the printCatalog function here                 printCatalog(movies);                break;            }            case 'F':            {                string inputFile;                bool isOpen;                cin >> inputFile;                cout << "Reading catalog info from " << inputFile << endl;                //call you readFromFile() in here                isOpen = readFile(inputFile, &movies);                if (isOpen == false)                    cout << "File not found" << endl;...

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
Active Questions
ADVERTISEMENT