Thread: Need help with assignment

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    6

    Need help with assignment

    Hi guys, I need help with my code

    contacts.h
    Code:
    struct Name {    char firstName[31];
        char middleInitial[7];
        char lastName[36];
    };
    
    struct Address
    {
        int streetNumber;
        char street[41];
        int apartmentNumber;
        char postalCode[8];
        char city[41];
    };
    
    struct Numbers
    {
        char cell[11];
        char home[11];
        char business[11];
    };
    
    struct Contact
    {
        struct Name name;
        struct Address address;
        struct Numbers numbers;
    
    
    };
    
    // Function Prototypes
    void getName(struct Name *);
    
    void getAddress(struct Address *);
    
    void getNumbers(struct Numbers *);
    
    void getContact(struct Contact *);

    contactHelpers.h

    Code:
    void clearKeyboard(void);
    
    // pause:
    // Put function prototype below:
    void pause(void);
    
    
    // getInt:
    // Put function prototype below:
    int getInt(void);
    
    
    // getIntInRange:
    // Put function prototype below:
    int getIntInRange(int, int);
    
    
    // yes:
    // Put function prototype below:
    int yes(void);
    
    
    // menu:
    // Put function prototype below:
    int menu(void);
    
    
    // contactManagerSystem:
    // Put function prototype below:
    void contactManagerSystem(void);

    contactHelpers.c

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    
    #include <stdio.h>
    
    void clearKeyboard(void)
    {
        while (getchar() != '\n');
    }
    
    
    // pause: Empty function definition goes here:
    void pause(void)
    {
        printf("(Press Enter to continue)");
        clearKeyboard();
    }
    
    
    
    
    // getInt: Empty function definition goes here:
    int getInt(void)
    {
        int value, flag = 0;
        char check;
        scanf("%d%c", &value, &check);
    
    
        while (check != '\n') {
            clearKeyboard();
    
    
            printf("*** INVALID INTEGER *** <Please enter an integer>:");
            scanf("%d%c", &value, &check);
    
    
    
    
        }
        return value;
    }
    
    
    
    
    
    
    // getIntInRange: Empty function definition goes here:
    int getIntInRange(int low, int high) {
        int valid, flag = 0;
    
    
        do {
            valid = getInt();
            if (valid < low || valid > high)
                printf("*** OUT OF RANGE *** <Enter a number between %d and %d>:", low, high);
            else
                flag = 1;
        } while (flag != 1);
    
    
        return valid;
    }
    
    
    
    
    
    
    // yes: Empty function definition goes here:
    int yes() {
        int send, flag = 0;
        char yes, check;
    
    
        scanf(" %c%c", &yes, &check);
    
    
        while (check != '\n' && (yes != 'y' || yes != 'Y' || yes != 'n' || yes != 'N')) {
            clearKeyboard();
            printf("*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>:");
    
    
            scanf(" %c%c", &yes, &check);
        }
    
    
        if (yes == 'y' || yes == 'Y')
            send = 1;
        else
            send = 0;
        return send;
    }
    
    
    
    
    // menu: Empty function definition goes here:
    int menu(void) {
        int chosen;
    
    
    
    
    
    
        printf("Contact Management System\n");
        printf("-------------------------\n");
        printf("1. Display contacts\n");
        printf("2. Add a contact\n");
        printf("3. Update a contact\n");
        printf("4. Delete a contact\n");
        printf("5. Search contacts by cell phone number\n");
        printf("6. Sort contacts by cell phone number\n");
        printf("0. Exit\n");
        printf("\nSelect an option:>");
    
    
        chosen = getIntInRange(0, 6);
    
    
        return chosen;
    }
    
    
    
    
    
    
    // contactManagerSystem: Empty function definition goes here:
    void contactManagerSystem(void) {
        int option = 0, feature = 0;
    
    
        while (option == 0) {
            feature = menu();
    
    
            while (feature != 0) {
                printf("feature not avaliable:  ");
                pause();
                printf("\n");
                feature = menu();
    
    
            }
    
    
            printf("Exit the program? (Y)es/(N)o:");
            option = yes();
    
    
            if (yes()) {
                printf("contact manegermnet: temnresdadsa\n");
            }
        }
    }

    contacts.c

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    
    #include <stdio.h>
    
    
    #include "contacts.h"
    
    
    #include "contactHelpers.h"
    
    void getName(struct Name *name)
    {
        
        printf("Please enter the contact's first name: ");
        scanf(" %30[^\n]s", name->firstName);
    
    
    
    
        printf("Do you want to enter a middle initial(s)? (y or n): ");
        clearKeyboard();
    
    
        if (yes())
        {
            printf("Please enter the contact's middle initial(s): ");
            scanf(" %6[^\n]s\n", name->middleInitial);
            clearKeyboard();
    
    
        }
    
    
        printf("Please enter the contact's last name: ");
        scanf(" %35[^\n]s\n", name->lastName);
    
    
    
    
    
    
    }
    
    
    
    
    // getAddress:
    void getAddress(struct Address *address)
    {
        
        printf("Please enter the contact's street number: ");
        address->streetNumber = getInt();
        while (address->streetNumber < 0) {
            printf("error: ");
            address->streetNumber = getInt();
    
    
        }
    
    
        printf("Please enter the contact's street name: ");
        scanf(" %36[^\n]", address->street);
    
    
        printf("Do you want to enter an apartment number? (y or n): ");
        clearKeyboard();
    
    
    
    
        if (yes())
        {
            printf("Please enter the contact's apartment number?: ");
            clearKeyboard();
    
    
            address->apartmentNumber = getInt();
        while (address->apartmentNumber < 0) {
            printf("error: ");
            address->apartmentNumber = getInt();
    
    
        }
        }
    
    
        
    
    
    
    
    
    
        printf("Please enter the contact's postal code: ");
        scanf(" %8[^\n]", address->postalCode);
    
    
    
    
        printf("Please enter the contact's city: ");
        scanf(" %41[^\n]", address->city);
    
    
    }
    
    
    
    
    
    
    // getNumbers:
    void getNumbers(struct Numbers *numbers)
    {
        
        printf("please enter contact's cell phone number: ");
        scanf(" %10[^\n]s", numbers->cell);
    
    
        printf("Do you want to enter a home phone number? (y or n): ");
        clearKeyboard();
    
    
        if (yes())
        {
            printf("Please enter the contact's home phone number: ");
            clearKeyboard();
    
    
        }
    
    
        printf("Do you want to enter a business phone number? (y or n): ");
        clearKeyboard();
    
    
        if (yes())
        {
            printf("Please enter the contact's business phone number: ");
            clearKeyboard();
    
    
        }
    
    
    }
    
    
    
    
    
    
    
    
    // getContact:
    // Define Empty function definition below:
    void getContact(struct Contact *contact) {
        getName(&(*contact).name);
        getAddress(&(*contact).address);
        getNumbers(&(*contact).numbers);
    }

    main.c

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    
    
    #include <stdio.h>
    #include "contacts.h"
    #include "contactHelpers.h"
    
    void printSectionHeader(const char*);
    
    
    int main(void)
    {
        struct Contact contact = { { { '\0' } } };
        int tstInteger;
    
    
        //--------------------------------------------------------
        
    
    
        //--------------------------------------------------------
        printSectionHeader("getContact(struct Contact *)");
        getContact(&contact);
        printf("\nValues Entered:\n");
        printf("Name: %s %s %s\n", contact.name.firstName, contact.name.middleInitial, contact.name.lastName);
        printf("Address: %d|%s|%d|%s|%s\n", contact.address.streetNumber, contact.address.street,
            contact.address.apartmentNumber, contact.address.postalCode, contact.address.city);
        printf("Numbers: %s|%s|%s\n", contact.numbers.cell, contact.numbers.home, contact.numbers.business);
    
    
        //--------------------------------------------------------
        printSectionHeader("contactManagerSystem()");
        contactManagerSystem();
    
    
        //--------------------------------------------------------
        printSectionHeader("Assign#2 - MS #2 test completed");
    
    
        return 0;
    }
    
    
    // Display specified test section header
    void printSectionHeader(const char* testName)
    {
        printf("\n");
        printf("------------------------------------------\n");
        printf("Testing: %s\n", testName);
        printf("------------------------------------------\n");
    }

    so everything seems to be working with only one problem, for some reason the apartment number when I enter -120, it should give me an error but now I have to enter twice for it to give the error, the same with the business number, when it asks if i want to enter a business number, when enter n, it displays nothing, I have to enter again. The same problem again with the last one at menu selecting when I want to select 0 to exit, it will ask y or n, when I type y or n, I need to type twice for it to work.

    can someone please help me with this ?

    thanks a lot.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Quote Originally Posted by hzkids
    for some reason the apartment number when I enter -120, it should give me an error but now I have to enter twice for it to give the error
    I did not get that in my test run:
    Code:
    ------------------------------------------
    Testing: getContact(struct Contact *)
    ------------------------------------------
    Please enter the contact's first name: Jane
    Do you want to enter a middle initial(s)? (y or n): n
    Please enter the contact's last name: Doe
    Please enter the contact's street number: -120
    error: 120
    Please enter the contact's street name:
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    6
    Quote Originally Posted by laserlight View Post
    I did not get that in my test run:
    Code:
    ------------------------------------------
    Testing: getContact(struct Contact *)
    ------------------------------------------
    Please enter the contact's first name: Jane
    Do you want to enter a middle initial(s)? (y or n): n
    Please enter the contact's last name: Doe
    Please enter the contact's street number: -120
    error: 120
    Please enter the contact's street name:
    hmm the street number is working for me, the apartment number part is the one giving me trouble. When i enter -120 it should give me error, but i have to enter 2nd time for the error to appear.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    That's because of this:
    Code:
    printf("Please enter the contact's apartment number?: ");
    clearKeyboard();
    Your yes() function already removes the newline from the input buffer, so this call to clearKeyboard() just waits for input until a newline is entered, i.e., it ends up reading and discarding the input that the user entered.

    May I suggest a rework of your input handling? Use the fgets + parse approach, e.g., use fgets to read the entire line, then use sscanf or strtol etc to parse it. This way, you don't have to worry about whether or not you have read and discarded a trailing newline. (You do have to worry about excessively long lines, but there are ways around that if you must deal with it.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    6
    Quote Originally Posted by laserlight View Post
    That's because of this:
    Code:
    printf("Please enter the contact's apartment number?: ");
    clearKeyboard();
    Your yes() function already removes the newline from the input buffer, so this call to clearKeyboard() just waits for input until a newline is entered, i.e., it ends up reading and discarding the input that the user entered.

    May I suggest a rework of your input handling? Use the fgets + parse approach, e.g., use fgets to read the entire line, then use sscanf or strtol etc to parse it. This way, you don't have to worry about whether or not you have read and discarded a trailing newline. (You do have to worry about excessively long lines, but there are ways around that if you must deal with it.)
    thanks for quick reply, I haven't learn that yet =[, I fixed the apartment part but the contactHelpers.c last part when I select 0, it will ask if i want to exit y or n, when I enter y or n, it would not show anything till i enter y or n again, could you help me take a look ? Thanks so muchh.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Quote Originally Posted by hzkids
    I haven't learn that yet
    As in you have not learned how to use fgets? You should.

    Quote Originally Posted by hzkids
    I fixed the apartment part but the contactHelpers.c last part when I select 0, it will ask if i want to exit y or n, when I enter y or n, it would not show anything till i enter y or n again, could you help me take a look ?
    Have you taken a look yourself? The problem should become obvious to you once you carefully trace through what is happening right after you print "Exit the program? (Y)es/(N)o:".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2018
    Posts
    6
    Quote Originally Posted by laserlight View Post
    As in you have not learned how to use fgets? You should.


    Have you taken a look yourself? The problem should become obvious to you once you carefully trace through what is happening right after you print "Exit the program? (Y)es/(N)o:".

    I got it :] thanks so much for the help !!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help On an Assignment
    By Erik Ingvoldsen in forum C++ Programming
    Replies: 191
    Last Post: 04-07-2015, 11:03 AM
  2. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  3. 1st Assignment
    By Zeff in forum C Programming
    Replies: 29
    Last Post: 09-19-2006, 07:21 PM
  4. First assignment help
    By FirstC++ in forum C++ Programming
    Replies: 7
    Last Post: 07-01-2004, 07:57 AM
  5. Help With Assignment Please
    By MegaVortex in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2004, 09:40 PM

Tags for this Thread