Thread: Structures

  1. #1
    SnoopFrogg KingFlippyNips's Avatar
    Join Date
    Sep 2016
    Posts
    33

    Structures

    I'm working on a problem that ask the following:

    Write a program that lets the user fill in a single structure with name, address, and phone number of a single person.

    The problem I keep running into is when I input an address that has whitespace the program doesn't allow the user to input their phone number. I would really appreciate if someone can point me into the right direction to resolve my issue. Thanks!

    Here is what my code looks like:
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    struct UserInfo {
    
        string firstName;
        string lastName;
        string address;
        int phoneNumber;
    };
    
    
    int main(){
    
        UserInfo user[1];
        for (int i = 0; i < 1; i++) {
            cout << "Please enter your first name: ";
            cin >> user[i].firstName;
            cout << "Please enter your last name: ";
            cin >> user[i].lastName;
            cout << "Please enter your address: ";
            cin >> user[i].address;
            cout << "Please enter your phone number: ";
            cin >> user[i].phoneNumber;
            cout << "\n";
    
        }
        system("pause");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The problem I keep running into is when I input an address that has whitespace the program doesn't allow the user to input their phone number.
    You may want to consider getline() instead of the extraction operator>> for all of your strings. Also a "phone number" isn't really a number and should probably be stored as a string.

  3. #3
    SnoopFrogg KingFlippyNips's Avatar
    Join Date
    Sep 2016
    Posts
    33
    Quote Originally Posted by jimblumberg View Post
    You may want to consider getline() instead of the extraction operator>> for all of your strings. Also a "phone number" isn't really a number and should probably be stored as a string.
    I made the edits and my program worked perfectly:

    Code:
     
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct UserInfo {
    
        string firstName;
        string lastName;
        string address;
        string phoneNumber;
    };
    
    
    int main(){
    
        UserInfo user[1];
        for (int i = 0; i < 1; i++) {
            cout << "Please enter your first name: ";
            getline(cin, user[i].firstName);
            cout << "Please enter your last name: ";
            getline(cin, user[i].lastName);
            cout << "Please enter your address: ";
            getline(cin, user[i].address);
            cout << "Please enter your phone number: ";
            getline(cin, user[i].phoneNumber);
            cout << "\n";
    
        }
        system("pause");
        return 0;
    }
    Can to you explain to me why getline() is better to use in this situation instead of cin?

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    std::cin >> str; skips whitespace (space, tab, newline, etc) and then reads characters up to but not including the next whitespace character.

    So when your address had a space in it, only the first part was read, leaving the rest in the input buffer. Then you tried to read that as an integer, which didn't work too well.

    std::getline(std::cin, str); reads an entire line, whitespace and all. It also "consumes" the newline at the end of the line but doesn't store it in the string.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    SnoopFrogg KingFlippyNips's Avatar
    Join Date
    Sep 2016
    Posts
    33
    Thank you for the explination!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring structures inside structures ?
    By jamaican1231 in forum C Programming
    Replies: 6
    Last Post: 04-13-2010, 03:40 PM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Accessing structures contained in structures
    By enigmaatj in forum C Programming
    Replies: 1
    Last Post: 04-18-2002, 08:53 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread