Looping instead of exiting program?

1)When I use do/while, (QUARTERS >= 1000 ), how do I also make it do while the user input is characters instead of numbers. So if I type "blah" in the input, the program will come out unsuccessful.

2) How do I use bool die to loop the program instead of end it?

// function definition
bool die(const string & msg){
cout << " " << msg << endl;
exit(EXIT_FAILURE); //I want it to loop instead of exit here
}

MY FULL PROGRAM:

<
// This program prompts the number of coins, and outputs how many cents you have

#include <iostream>
#include <string>
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!");
if (QUARTERS >= 1000)
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);
}
>
Last edited on
Pls use code tags, much easier to read and comment upon.

http://www.cplusplus.com/articles/jEywvCM9/

1)When I use do/while, (QUARTERS >= 1000 ), how do I also make it do while the user input is characters instead of numbers. So if I type "blah" in the input, the program will come out unsuccessful.


1
2
3
4
5
do {

// Do something here
       
     } while (QUARTERS < 1) && (QUARTERS > 2147483647);
Last edited on
Replace these statements :

1
2
3
4
5
6
7
[code]if (QUARTERS < 1000)
cout << " --> Input Successful!" << endl;
}
else die(" --> :( Input Unsuccessful!");
if (QUARTERS >= 1000)
cout << " You must put in less than 1000 quarters! Please try again." << endl << endl << endl;
} while (QUARTERS >= 1000 );
[/code]

by something like :

1
2
3
4
5
6
7
8
9
[code]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;
}
[/code]

note also that cout is redundant , so you can put it out the statement scopes :

1
2
3
4
5
6
7
8
9
10
11
12
const char* msg;
[code]if(QUARTERS < 1000)
{

msg = " --> Input Successful!" << endl;
}
else
{
die(" --> :( Input Unsuccessful!");
msg =  " You must put in less than 1000 quarters! Please try again." << endl << endl << endl;
}
cout << msg


This is a pseudocode .
Topic archived. No new replies allowed.