Thread: Practice problems _ Structures

  1. #1
    Registered User
    Join Date
    Jan 2016
    Location
    Germany
    Posts
    3

    Question Practice problems _ Structures

    Hay Guys,
    my question is related to a practice problem in the book "Jumping into C++":

    Create an address book program that builds on problem #1—this time, the user should be able to not just fill out a single structure, but should be able to add new entries, each with a separate name and phone number. Let the user add as many entries as he or she wants—is this easy to do? It is even possible? Add the ability to display all, or some of the entries, letting the user browse the list of entries.
    Oh yeah.. the problem one was:

    Write a program that lets the user fill in a single structure with the name, address, and phone number of a single person
    So the first problem I solved quiet good (I hope)

    Code:
    #include <iostream>
    
    using namespace std;
    
    // define struct
    struct personalInformation
    {
            string firstName; string lastName;
            string address;
            int phoneNumber;
    };
    
    int main()
    {
        // Initialize struct
        personalInformation person;
    
        // Get and Save Information
        // Sorry if you don't understand the strings...
        // Just asking for Informations
        cout << "Eingabe von Informationen" << endl;
        cout << "Vorname der Person: "; cin >> person.firstName;
        cout << "Nachname der Person: "; cin >> person.lastName; cin.ignore();
        cout << "Addresse der Person: "; getline(cin, person.address, '\n');
        cout << "Telefonnummer der Person: "; cin >> person.phoneNumber;
    
        cout << "\n\n" << person.firstName << " " << person.lastName << endl;
        cout << person.address << endl;
        cout << person.phoneNumber << endl;
        return 0;
    }
    My question now is...
    What should I do? (The problem i mentioned earlier)
    "Let the user add as many entries as he or she wants"
    I really have no Idea how I should save all of this data.
    (If it would be a lot)
    An array won't do it, because I have to set the size of it before the program compiles.

    I guess I have to write something similiar to this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    // define struct
    struct personalInformation
    {
            string firstName; string lastName;
            string address;
            int phoneNumber;
    };
    
    personalInformation getPersonInformation()
    {
        // Initialize struct
        personalInformation person;
    
        // Get and Save Information
        // Sorry if you don't understand the strings...
        // Just asking for Informations
        cout << "Eingabe von Informationen" << endl;
        cout << "Vorname der Person: "; cin >> person.firstName;
        cout << "Nachname der Person: "; cin >> person.lastName;
        cout << "Telefonnummer der Person: "; cin >> person.phoneNumber;
    
        return person;
    }
    
    int main()
    {
        int newEntries;
        cout << "How many entries should be add?" << endl;
        cin >> newEntries;
    
        for( int i = 0; i < newEntries; i++ )
        {
            cout << "\n";
            somehowSafeInformation = getPersonInformation();
        }
    
        return 0;
    }
    Do you see the bold underlined text?
    There is the part where I don't know how to progress...

    So I hope u can give me some advices.

    Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > "Let the user add as many entries as he or she wants"

    Do you know about things like std::vector?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2016
    Location
    Germany
    Posts
    3
    Not at the moment... The next Chapter is about Pointers.
    In the problem the author said "Is it even possible"? I would say yes, otherwise he would'nt describe what the program should do / can do.
    I hope you can give me some advice.

    Thank you

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, skip forward and look into vectors. That book is junk anyway since it pushes that actual useful C++ stuff to the last.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2016
    Location
    Germany
    Posts
    3
    Thank you guys..

    I'm now moving on, maybe I can solve it later with std::vector (whatever it is ^^)

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with structures practice
    By treebeard in forum C++ Programming
    Replies: 6
    Last Post: 07-25-2015, 03:39 PM
  2. Practice Problems Help
    By StaticBlue in forum C++ Programming
    Replies: 2
    Last Post: 03-03-2014, 09:37 AM
  3. Chapter 2 practice problems
    By Kyle Spalding in forum C++ Programming
    Replies: 14
    Last Post: 03-19-2013, 06:12 AM
  4. Suggestions to practice with data structures
    By smoking81 in forum C Programming
    Replies: 2
    Last Post: 03-31-2008, 01:57 PM
  5. Practice Problems
    By Eultza in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:13 PM

Tags for this Thread