can't get my program to let me enter anything into it

This is a discussion on can't get my program to let me enter anything into it within the C++ Programming forums, part of the General Programming Boards category; I'm brand new at this and i'm trying to write a program that appeared simple at first. I can get ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    Unhappy can't get my program to let me enter anything into it

    I'm brand new at this and i'm trying to write a program that appeared simple at first. I can get the program to let me enter from the keyboard on the first question that is to be answered but when i get to the second one it skips right over it. What in the world am i doing wrong. i'm about to slit my wrists.

    This is what i have:
    cout << "Please indicate the currency to which you need to convert," << "\n";
    cout << "by entering the exact name from the list above: " << "\n";
    cin >> money.response1;
    cout << "Enter the amount of currency that needs to be exchanged: "
    << "\n";
    cin >> money.amount;

    the first one where it asks you to enter a currency i can enter on the screen but when it gets to the one where it is the amount, it skips right over it. to me they both look the same.

    any help would be greatly appreciated.

    thanks in advance.
    k

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    I'll probably get told off for this by someone, but anyways, try using fflush(stdin); after the cin you are having problems with.
    Be a leader and not a follower.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    Thank you so kindly! I hope no one gets onto you. It's darned hard being new at something with no one to ask what to do. Getting stuck is the pits. I appreciate your time.

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Question

    Why are you doing something like this? What is with the . notation here?
    Code:
    cin >> money.response1;
    Where is the rest of your code?

    Something simple:

    cin >> response1;

    As I tell my students: Don't you have a book to help you as well?
    Mr. C: Author and Instructor

  5. #5
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I'll probably get told off for this by someone, but anyways, try using fflush(stdin); after the cin you are having problems with.
    I can't really tell you off, I'm sure I do far worse things by accident than you're doing by design.

    allthekandi, he's not going to get told off for telling you, just for what he told you; fflush(stdin) is non standard. 99 out of 100 times it may do what you want it to, it's that 1 time that may bite you in the arse.

    In C I'd normally use "while (getchar() != '\n');" to clear the input buffer. I don't know of a stadard C++ way of doing it, that's not to say there isn't one, just that right now I have no idea what it is, I'm sure it's been discussed many times before though, do a search on the board for "fflush(stdin)" to see some of the pitfalls and other options.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>I don't know of a stadard C++ way of doing it
    Like this :-)
    Code:
    inline void discard()
    {
      char ch;
    
      while (cin.get(ch) && ch != '\n')
      {} // Throw away extras
    }
    Or if you're using the >> operator of cin you can compactisize things by making a modifier instead of an inline function :-)
    Code:
    istream& discard(istream& in)
    {
      char ch;
    
      while (in.get(ch) && ch != '\n')
      {} // Generic input cleaning
    
      return in;
    }
    Then just call it like this
    Code:
    cin>> var >>discard;
    *Cela*

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    Thank all of you kindly for your time, help and advice. And yes I do have a book and many things to help me but when you get stuck and can't figure it out through those methods a little guidance is a great thing.

  8. #8
    phooey
    Guest
    Not to beat a dead horse, but there is also 'cout.clear()', which, I am fairly sure, IS standard in C++.

    The only drawback is that that instantly flushes any characters to stdout, which may produce some weird things. But, it works. And its cleaner than a while loop I think, just to clean the stdout buffer(s).

  9. #9
    phooey
    Guest
    cin.clear(), rather.

    And all those other 'cout's ...

  10. #10
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Not to beat a dead horse, but there is also 'cout.clear()', which, I am fairly sure, IS standard in C++.
    cin.clear() doesn't flush characters, it clears the error bits of the stream when things go downhill. You might be thinking of cin.ignore() :-)
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Replies: 2
    Last Post: 02-08-2009, 08:26 PM
  3. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 12:21 PM
  4. Time to seconds program
    By Sure in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 08:08 PM
  5. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2003, 11:23 PM

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