0

I’m a beginning C++ programmer and this is only my second program... It’s a simple money converter between dollars and Euros. The problem I have is that if I put in a NaN value, it’ll say so and ask again but quit. Does anyone know how to make it wait until the user types something else? Also at the second cin >> user_dollar I’m getting an error that says “Reference to overloaded function could not be resolved; did you mean to call it?” Sorry that this is so long and thanks~

Here is my code. I’m using Xcode on my MacBook Pro.

#include <iostream>
using namespace std;


int main() {
    string choice;
    int user_dollar;
    int user_euro;

    cout << "Dollars to Euros (type ‘Dollars’) or Euros to dollars (type ‘Euros’)? ";
    cin >> choice;

    if (choice == "Dollars" || choice == "dollars") {

        cout << "Enter dollar amount: ";
        cin >> user_dollar;
            if (isdigit(user_dollar) != true){
                cout << "That’s not a number... " << endl;
                cout << "Enter dollar amount: " << endl;
                *cin >> user_dollar;*
                }
            else {
                cout << "That is " << user_dollar / 1.13 << " Euros." << endl;
                }

    }

    else if (choice == "Euros" || choice == "euros") {

        cout << "Enter Euro amount: ";
        cin >> user_euro;

        cout << "That is " << user_euro * 0.89 << " dollars." << endl;

    }
}
philipxy
  • 14,867
  • 6
  • 39
  • 83
Minnow
  • 1
  • 1
  • 1. Your program has no loops. You want it to ask `while` something is true. 2. Read the spec for `std::isdigit`. – philipxy Feb 07 '15 at 00:41
  • Hi, thanks for your quick reply! If I put `while` instead of `if ` it just prints “That’s not a number...” and “Enter dollar amount: ” endlessly. Also I am not sure what `std::isdigit` means... – Minnow Feb 07 '15 at 00:56
  • 1. Decide when to stop. Loop while not that. (Inside a loop do what you want to do. Make sure at the bottom that you are ready for the next test at the top.) 2. isnan(x) means x != x means x is NaN. Search re using std::isnan. 3. Search re using std::isdigit. 4. Search re using std::cin. Don't call a function unless you know what it does. Otherwise why are you calling it? (Rhetorical.) – philipxy Feb 07 '15 at 01:28
  • @philipxy I agree that you should read up about every function you ever use. re #2. This program doesn't have any NaNs and doesn't need any, so `std::isnan()` isn't necessary. NaNs can only be stored in `float` and `double` variables, and `cin` can't read an NaN value anyway. – Jordan Miner Feb 07 '15 at 01:54
  • @JordanMiner Of course! (My own medicine.) @ Minnow NaN is a certain IEEE-float value. Be very clear about what input strings you mean and what cin is finding and doing and returning and setting. None of which involves NaN. Though it might get an input string that it doesn't find an int in. – philipxy Feb 07 '15 at 02:19
  • Maybe I didn’t explain myself well... What I’d like the program to do is after it asks for a dollar input, if I were to type in letters instead of numbers it’d say “That’s not a number...” and ask for input again. I changed to a `while` loop and added Igor’s suggestion, and now instead of crashing it just goes to a new line every time you hit enter, no matter what you type. – Minnow Feb 07 '15 at 19:43
  • Have you *read how the calls you are making behave* per my comments? Also [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). We cannot do anything with this latest comment. Now we don't even know what your code is. If an answer helped why not accept and post a new question for a new issue with new (MCVE) code? – philipxy Feb 08 '15 at 04:31

2 Answers2

0

You need to flush the cin buffer before second use of cin. Something like

cin.clear();
cin.ignore(INT_MAX,'\n');
cin >> user_dollar;

Take a look at this thread for more details on flushing the buffer.

Community
  • 1
  • 1
Igor Popov
  • 2,588
  • 17
  • 20
0

Since the type of the user_dollar variable is an int, it can only hold an integer. If someone types something besides a number (like "hi"), it cannot be stored in user_dollar.

When you call cin >> user_dollar, and the user enters a non-number, it fails and does not change user_dollar. Since nothing else has set user_dollar at that point, it could be anything--semi-random garbage. You are currently checking that garbage with isdigit(). And isdigit() is not for checking integers read directly using cin.

You can check if it failed to read a number by calling fail(), use clear() to reset whether there was an error, and ignore() to flush the buffer as Igor shows.

As philipxy said, you can use a while loop to keep asking until a number was successfully read.

Jordan Miner
  • 2,034
  • 17
  • 19