beginner question

Discussion in 'C++' started by dougmmika@gmail.com, Mar 21, 2013.

  1. Guest

    why does the following code not let me enter the phone number nor the address. You would think that if I would have an unwanted \n hanging around my input the first cin.get() would absorb it and at least it would ask me for the address to be input. Instead after entering the first and last name the program quits. Why?
    Many thanks to all.

    cout<<"Please Enter your first name: ";
    cin>>firstStudent.firstName;
    cout<<"Please Enter your last name: ";
    cin>>firstStudent.lastName;
    cout<<"Please enter your Phone Number: (XXX) XXX-XXXX: ";
    cin.get(phoneNumber, 15);
    cin.get(dummy); //eat the end of line marker
    cout<<"Please enter your address: ";
    cin.get(address,51);
    cin.get(dummy); //eat the end of line marker
     
    , Mar 21, 2013
    #1
    1. Advertising

  2. On 3/21/2013 3:49 PM, wrote:
    > why does the following code not let me enter the phone number nor the address. You would think that if I would have an unwanted \n hanging around my input the first cin.get() would absorb it and at least it would ask me for the address to be input. Instead after entering the first and last name the program quits. Why?
    > Many thanks to all.
    >
    > cout<<"Please Enter your first name: ";
    > cin>>firstStudent.firstName;
    > cout<<"Please Enter your last name: ";
    > cin>>firstStudent.lastName;
    > cout<<"Please enter your Phone Number: (XXX) XXX-XXXX: ";
    > cin.get(phoneNumber, 15);
    > cin.get(dummy); //eat the end of line marker
    > cout<<"Please enter your address: ";
    > cin.get(address,51);
    > cin.get(dummy); //eat the end of line marker


    It might be useful to get into the habit of posting *complete* code.

    Also, to read out a line, consider using std::getline instead of >>.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 21, 2013
    #2
    1. Advertising

  3. Guest

    Here's the complete code:


    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    #include "bool.h"



    typedef char String10[11];
    struct StudentRec{
    String10 firstName;
    String10 lastName;
    };


    int main()
    {
    //using namespace std;
    char address[51];
    char pause, newLine;
    char fileName[30];
    char phoneNumber[15];
    char dummy;
    int number=6;
    StudentRec firstStudent;

    cout<<"Please Enter your first name: ";
    cin>>firstStudent.firstName;
    cout<<"Please Enter your last name: ";
    cin>>firstStudent.lastName;
    cout<<"Please enter your Phone Number: (XXX) XXX-XXXX: ";
    cin.get(phoneNumber, 15);
    cin.get(dummy); //eat the end of line marker
    cout<<"Please enter your address: ";
    cin.get(address,51);
    cin.get(dummy); //eat the end of line marker

    cout<<"The information entered is:"<<endl;
    cout<<" First Name: "<<firstStudent.firstName<<endl;
    cout<<" Last Name: "<<firstStudent.lastName<<endl;
    cout<<" Phone #: "<<phoneNumber<<endl;
    cout<<" Address: "<<address<<endl;


    cout<<"End of Program"<<endl;
    cin.get(pause);


    return 0;

    }
     
    , Mar 21, 2013
    #3
  4. On 3/21/2013 4:41 PM, wrote:
    > Here's the complete code:
    >
    >
    > #include <iostream.h>
    > #include <fstream.h>
    > #include <string.h>
    > #include "bool.h"
    >
    >
    >
    > typedef char String10[11];
    > struct StudentRec{
    > String10 firstName;
    > String10 lastName;
    > };
    >
    >
    > int main()
    > {
    > //using namespace std;
    > char address[51];
    > char pause, newLine;
    > char fileName[30];
    > char phoneNumber[15];
    > char dummy;
    > int number=6;
    > StudentRec firstStudent;
    >
    > cout<<"Please Enter your first name: ";
    > cin>>firstStudent.firstName;
    > cout<<"Please Enter your last name: ";
    > cin>>firstStudent.lastName;


    So, what happens if you [accidentally, wink-wink, nudge-nudge] enter
    more than 10 characters?

    Drop your char arrays and start using 'std::string', you're using C++ not C.

    > cout<<"Please enter your Phone Number: (XXX) XXX-XXXX: ";
    > cin.get(phoneNumber, 15);


    Use std::getline here.

    > cin.get(dummy); //eat the end of line marker


    Use std::cin.ignore for that. RTFM

    > cout<<"Please enter your address: ";
    > cin.get(address,51);
    > cin.get(dummy); //eat the end of line marker
    >
    > cout<<"The information entered is:"<<endl;
    > cout<<" First Name: "<<firstStudent.firstName<<endl;
    > cout<<" Last Name: "<<firstStudent.lastName<<endl;
    > cout<<" Phone #: "<<phoneNumber<<endl;
    > cout<<" Address: "<<address<<endl;
    >
    >
    > cout<<"End of Program"<<endl;
    > cin.get(pause);
    >
    >
    > return 0;
    >
    > }


    What book on C++ are you reading that teaches you to use character
    arrays like that?

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 21, 2013
    #4
  5. Guest

    what should I use instead of char arrays? Is there an online book to introduce me to C++. I'm reading Programming and Problem Solving with C++ by Dale, Weems, and Headington.

    Any more info on std::getLine and the other recommended functions to input data?

    Thanks
    Mika
     
    , Mar 21, 2013
    #5
  6. Jorgen Grahn Guest

    On Fri, 2013-03-22, Andy Champ wrote:
    > On 21/03/2013 20:41, wrote:

    ....
    > My two checks on names are mine (I have two middle names) and this girl
    > I knew at Uni - she was Spanish. I won't post up her actual name, but it
    > had a format like this:
    >
    > xxxxxxxxxx xxxxxxxxx-xxxxxxxxx
    >
    > You might like to use Prince Charles as test data - Charles Philip
    > Arthur George Mountbatten-Windsor.


    That's probably not even close to the most complex name ... add a few
    "von", "de la" and ", Jr.". And that's just european names.

    Or he might ask himself: what do I /really/ need to do with this name?
    Probably nothing, in which case it's just one string.
    Possibly, collating it according to some national conventions.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Mar 22, 2013
    #6
  7. Dombo Guest

    Op 22-Mar-13 0:56, schreef:
    > what should I use instead of char arrays? Is there an online book to
    > introduce me to C++.


    "Thinking in C++" springs to mind (just Google for it), I don't know if
    it is good but it is freely downloadable.
     
    Dombo, Mar 24, 2013
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerker Hammarberg

    Beginner question: What trigs processes

    Jerker Hammarberg, Jul 17, 2003, in forum: VHDL
    Replies:
    16
    Views:
    1,633
    Mike Treseler
    Jul 22, 2003
  2. smu
    Replies:
    3
    Views:
    2,644
    shobhit24
    Jun 28, 2006
  3. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    604
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  4. Rensjuh
    Replies:
    7
    Views:
    998
    Mabden
    Sep 2, 2004
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    229
    7stud --
    Apr 12, 2011
Loading...

Share This Page