Entering initials...need help

This is a discussion on Entering initials...need help within the C++ Programming forums, part of the General Programming Boards category; For one of my HW problems it says "Ask user for initials." So I do the whole deal and write ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    33

    Entering initials...need help

    For one of my HW problems it says "Ask user for initials." So I do the whole deal and write the the code. The next question is "Ask user for phone number." This is where the problem arises. Whenever I enter my initials, it will show them then skip all the code I have written under that line!

    So for example

    Please enter your initials
    ABC
    Please enter your phone number
    all my cout lines


    It doesn't even let me enter my phone number! Instead it just shows the code under but doesn't letter the user input anything. Any help? Also for some reason, say I enter a number for my initials, it works fine...lets me enter my phone number and everything else

    I was thinking I have to create a variable for every initial but I'm not 100% sure.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,035
    There's probably something wrong with how you read the phone number. Post your code.

    The fact that you can enter your phone number when prompted for initials and make it work is because of the way standard input and output work. There is no way to avoid this kind of behavior unless you use special libraries.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    If you're mixing cin >> and getline, then that could cause such a problem. Add a cin.ignore() after each call to cin >> (but not after calls to getline).

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Heres my code

    cout << "Please enter your initials"<< endl;
    cin >> ini;

    cout << "Please enter your phone number" << endl;
    cin >> phone;

    What datatype should initials and phone number be? I have them at char and int

    ( I don't have to code in front of me so if some of the << and ;s are misplaced sorry.)
    Last edited by Sembhi; 10-11-2007 at 06:18 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    A char only holds a single character, so your code is only reading in the first character in the initials. Having a separate variable for each initial is one solution, although it means that the user has to always put three initials, not 2 and not 4. You could also read the initials into a string so that it doesn't matter how many initials they use.

    For the phone number, I would store it as a string. For an int to work, the number has to contain only numeric digits. No dashes or parentheses are allowed.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Ok, gotcha.

    One more thing, I have to ask the user for two numbers, then tell it do some basic math operations. For some reason it won't do the division one properly. Any help? What should I set the datatype for the numbers, I have it at int currently.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    The type int doesn't allow for decimals, only whole numbers, ie: ..., -3, -2, -1, 0, 1, 2, 3, ...

    If you divide 1/2 then you get 0.5, but if it's an int it will only store 0. If you use a double or a float I believe you will be able to divide.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You have to be careful with integer division, though. If you use
    Code:
    double result = 1/2;
    then "1/2" is still integer division, and result will get 0. It's as if you had gone
    Code:
    double result = (double)( (int)((int)1 / (int)2) );
    You have to make sure that one of the values is a double to being with.
    Code:
    double result = (double)1 / 2;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    I would just use double literals to indicate floating point math:
    Code:
    double result = 1.0/2.0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Entering an email...the correct way!
    By ultrabot90 in forum C++ Programming
    Replies: 20
    Last Post: 10-31-2007, 03:52 AM
  2. Entering in an nth degree polynomial
    By Noah in forum C Programming
    Replies: 1
    Last Post: 03-02-2006, 08:02 PM
  3. Printing my initials and surname
    By Guti14 in forum C++ Programming
    Replies: 2
    Last Post: 08-20-2003, 02:46 AM
  4. function to prompt user to continue entering data or not
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2002, 06:38 PM
  5. Score Entering
    By ryn in forum C++ Programming
    Replies: 4
    Last Post: 03-26-2002, 10:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21