How to dispose of unused characters

This is a discussion on How to dispose of unused characters within the C++ Programming forums, part of the General Programming Boards category; Hello I am currently in a 1st year Comp sci class and am having trouble with one aspect of a ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    13

    How to dispose of unused characters

    Hello I am currently in a 1st year Comp sci class and am having trouble with one aspect of a program I am trying to write. Basically the program calculates tax payable based upon whether or not the user has a dependent and their income. a sample run could look like this.............................
    Do you have any dependants (y/n) -> maybe
    You must answer with a 'y' or an 'n'.
    Do you have any dependants (y/n) -> yes 53000
    What is your income -> -3000.00
    Income entered must be non-negative.
    What is your income -> -53000.00
    Income entered must be non-negative.
    What is your income -> 53000.00
    Your tax payable is $5600.00

    The problem is if the first data item on the line is correct then it must be used. All other data on the line must be ignored. so if when asked Do you have any dependents the user could enter: yes 53000 . and the 'y' would be read in and everything else would be discarded so its not stored and used in the tax calculation which would be problematic. So could anyone let me know how I could do this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So use .ignore().

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    13
    I cant use that function unfortunately. The way I am supposed to do it is probably not the way a real programmer would do it. Is their any other ways to do it you know of?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You could use getline, and then read your input from the line o' input instead of from the stream.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Quote Originally Posted by C++Newb View Post
    I cant use that function unfortunately. The way I am supposed to do it is probably not the way a real programmer would do it. Is their any other ways to do it you know of?
    Well instead of talking about what you can't do, why not tell us what you can/must use?

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    13
    Quote Originally Posted by Sebastiani View Post
    Well instead of talking about what you can't do, why not tell us what you can/must use?
    Ok I know I need to use and that is a while loop the loop must read in all of the characters until it reads in a \n(return)

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Well, then just call istream::get() until you hit a newline (or EOF).

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    13
    Quote Originally Posted by Sebastiani View Post
    Well, then just call istream::get() until you hit a newline (or EOF).
    I wont be able to use that either unfortunately...here is a sample of code I am working on basically I need to do something once and then terminate when I read in a \n or return. the code in question is the second while loop. ls their anyway to read in all of the characters into the char variable and then stop when I reach a \n so their wont be any that could be claimed by the float variable later on? Do you know what I mean? I am certain It must be something like this and I know I cant use any functions.

    Code:
    while( dependants != 'Y' && dependants != 'y' && dependants != 'n' && dependants != 'N' ){
           cout << "You must answer with a 'y' or an 'n'.\n";
           cout << "Do you have any dependants (y/n) -> ";
           cin >> dependants;
           while( dependants != '\n' ){
           cin >> dependants;
           } 
           }

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Those requirements don't make any sense. You *are* using functions (just not the right ones). If 'dependants' is, in fact, a char, then cin will just keep reading input while it discards the newline. Which of course means that there is no solution for you (considering your current requirements).

    In that case, it might be a good idea to go ahead and post the full requirements (verbatim) so that we can get a better idea of what is really expected of you here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM

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