In the input loop if you enter 2.5 or anything with a decimal the cin check reads it as a fail because of the decimal, but for some reason it also acepts the first number then the next input takes the 5 so you get an input of 2 then an error and then an input of 5 instead of just giving me an error.
When you enter a letter it runs the error code I have and just tells them to reenter a number, but the decimal confuses the program for some reason.
#include <iostream>
using namespace std;
//prototypes
void input(int choice,int &pine, int &oak, int &otherWood, int &drawers);
void calc(int &pine, int &oak, int &otherWood, int &drawers, double &subtotal, double &tax, double &totalSale);
void errorCheck(char &errorChar);
void output(double subtotal, double tax, double totalSale);
int main()
{
char runAgain = ' ';
do
{
//local variables to main
int choice = 0;
int pine = 0;
int oak = 0;
int otherWood = 0;
int drawers = 0;
double tax = 0;
double subtotal = 0;
double totalSale = 0;
input(choice, pine,oak, otherWood, drawers);
calc(pine, oak, otherWood, drawers, subtotal, tax, totalSale);
output(subtotal, tax, totalSale);
cout << "Do you want to run the program again?: ";
cin >> runAgain;
while((runAgain != 'y') && (runAgain != 'n'))
{
cout << "Invalid entry -- enter y or n" << endl;
cin >> runAgain;
}
system("pause");
system("cls");
}while(runAgain == 'y');
return 0;
}//end of main
void input(int choice,int &pine, int &oak, int &otherWood, int &drawers)////parameter list
{
char errorChar = ' ';
cout << "\t\t\tPLEASE PLACE YOUR ORDER" << endl << endl;
cout << endl;
while((choice <=0) || (choice >=4))
{
cout << "Enter 1 for PINE Desk" << endl;
cout << "Enter 2 for OAK Desk" << endl;
cout << "Enter 3 for OTHER Desk" << endl;
cout << "ENTER NOW ----> ";
cin >> choice;
cout << endl;
if(cin.fail()) //if it fails (a char is entered), go into if to fix
{
errorCheck(errorChar);
}
}
cout << endl;
cout << "****** THERE IS A $30 SURCHARGE FOR EACH DRAWER!!!" << endl;
cout << endl;
cout << "How many drawers do you want: ";
cin >> drawers;
cout << endl;
if(cin.fail()) //if it fails (a char is entered), go into if to fix
{
errorCheck(errorChar);
}
switch(choice)
{
case 1: //PINE
pine = pine + 100;
break;
case 2: //OAK
oak = oak + 140;
break;
case 3: //OTHER
otherWood = otherWood + 180;
break;
}//end of switch
system("cls");
}//end of input
void calc(int &pine, int &oak, int &otherWood, int &drawers, double &subtotal, double &tax, double &totalSale)//parameter list
{
int choice = 0;
drawers = drawers * 30;
subtotal = pine + oak + otherWood + drawers;
tax = subtotal * .06;
totalSale = subtotal + tax; //Total of all items purchased
system("pause");
system("cls");
}//end of calculations
void errorCheck(char &errorChar)
{
system("cls");
cin.clear(); //clear the failed bit
cin >> errorChar;
//ch1 allows a place for the error bit to be stored - - you must declare a char variable. Earlier, I have char ch1 defined
cout << endl << "The following value is not recognized: " << errorChar;
cout << endl;
cout << "Repeat entry" << endl;
}
void output(double subtotal, double tax, double totalSale)
{
cout << endl;
cout << "\t\t\t RECEIPT" << endl << endl;
cout << "\t\t\t---------" << endl << endl;
cout << endl;
cout << "Subtotal: $" << subtotal << endl;
cout << "Tax: $" << tax << endl;
cout << endl;
cout << "TOTAL: $" << totalSale << endl;
cout << endl;
system("pause");
}//end of output

New Topic/Question
Reply



MultiQuote


|