-1
  1. How do I make a function that will terminate the program if the user says e, and loop if the user presses l at any time?

  2. How do I make the program reask the user for number input if the user inputs letters instead of numbers? Currently, the program terminates when I input blah, for instance. My obstacle is the bool die definition: I'm not sure how to use bool die to loop instead of die (my teacher required bool die usage.)

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

bool die(const string & msg);

int main() {

// declaring variables:
unsigned QUARTERS;
unsigned DIMES;
unsigned NICKELS;
unsigned PENNIES;
double total;

cout << "********************************************************" << endl;
cout << "             Welcome to Crazy Coin Counter!             " << endl;
cout << "********************************************************" << endl << endl;

// user input:

//QUARTERS
do {
    cout << "   # QUARTERS: ";
    cin >> QUARTERS;
    if (cin){
        if (QUARTERS < 1000)
            cout << "               --> Input Successful!" << endl;
    }
    else die("             --> :( Input Unsuccessful!");

        cout << "               You must put in less than 1000 quarters! Please try again." << endl << endl << endl;
} while (QUARTERS >= 1000 );

//DIMES
do{
    cout << endl << "   # DIMES: ";
    cin >> DIMES;
    if (cin){
        if (DIMES < 1000)
            cout << "               --> Input Successful!" << endl;
    }
    else die("             --> :( Input Unsuccessful!");
    if (DIMES >= 1000)
        cout << "                You must put in less than 1000 dimes! Please try again." << endl << endl << endl;
} while (DIMES >= 1000);

//NICKELS
do {
    cout << endl << "   # NICKLES: ";
    cin >> NICKELS;
    if (cin){
        if (NICKELS < 1000)
            cout << "               --> Input Successful!" << endl;
    }
    else die("             --> :( Input Unsuccessful!");
    if (NICKELS >= 1000)
        cout << "                You must put in less than 1000 nickels! Please try again." << endl << endl << endl;
} while (NICKELS >= 1000);

//PENNIES
do {
        cout << endl << "   # PENNIES: ";
        cin >> PENNIES;
        if (cin){
            if (PENNIES < 1000)
                cout << "               --> Input Successful!" << endl;
        }
        else die("             --> :( Input Unsuccessful!");
        if (PENNIES >= 1000)
            cout << "                You must put in less than 1000 pennies! Please try again." << endl;
    } while (PENNIES >= 1000);


// calculations:

total = (QUARTERS * 0.25) + (DIMES * 0.1) + (NICKELS * 0.05) + (PENNIES * 0.01);

// output:

cout << endl <<endl<< "Congrats! You have       $" << total << "      worth of coins! " << endl << endl << endl;

}

// function definition
bool die(const string & msg){
cout << "  " << msg << endl;
exit(EXIT_FAILURE);

}
Biffen
  • 6,249
  • 6
  • 28
  • 36
ravsmy
  • 37
  • 6
  • 1
    Read input as `std::string` in 1st place e.g. with `getline()` parse further using a `std::istringstream `. See here how that works: http://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it – πάντα ῥεῖ Sep 14 '15 at 18:57

1 Answers1

0

Try something like this:

while ( true ) // Loop forever
{
  cout << "Enter 'e' to exit:";
  std::string answer;
  getline(cin, answer);
  if (answer == "e")
  {
    break;  // Break out of the loop
  }
  else
  {
    cout << "\nWrong answer.\n";
    continue; // The continue would start at the top of the loop.
  }
}

There are many other techniques that you can find by searching StackOverflow for "c++ terminate loop".

Edit 1: Checking numerical input
The simplest method for checking numerical input is to test the result of inputting the number:
unsigned int quarters; // Using unsigned because quantities can't be negative.

cout << "Enter number of quarters: ";
if (cin >> quarters) // Input and test in same statement.
{
  cout << "Your total is " << (quarters * 0.25) << "\n";
}
else
{
  // Handle incorrect input
  cout << "Invalid input, try again.\n";
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154