2 Replies - 879 Views - Last Post: 18 March 2009 - 05:37 PM Rate Topic: -----

#1 KWags   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 27
  • Joined: 11-February 09

Error Code

Posted 18 March 2009 - 04:13 PM

I was wondering if there was a way to do this...which I'm sure there is, but hopefully all you bright minds could give me an idea!
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



Is This A Good Question/Topic? 0
  • +

Replies To: Error Code

#2 bsaunders   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Error Code

Posted 18 March 2009 - 05:29 PM

One thing you can do is, instead of accepting an integer, accepting a string from the user. That way, the decimal point, as well as any other non-digit character will be read from the stream.

You can define a function that determines if the input string is an integer. Here is an example function isInteger:

#include <cstring>
#include <cctype>

/*
 * bool isInteger(const char *string)
 *
 * Return Value: Returns the specified string as an integer if it represents
 *				 a positive integer or zero. Otherwise, the return value is
 *				 -1.
 *
 * Description: Determines if the specified string contains an integer.
 */
int isInteger(const char *string)
{
	int i;
	int len;

	if(!string)
		return -1;

	len = strlen(string);

	if(len < 1)
		return -1;

	for(i = 0; i < len; i ++) {
		/* If the string contains a non-digit character, it is not an integer */
		if(!isdigit(string[i]))
			return -1;
	}

	return atoi(string);
}


Here is an example of an input string being converted to an integer:

char choice[10];

do {
	cout << "Enter 0 to quit.";
	cin >> choice;
}while(isInteger(choice) != 0);

This post has been edited by bsaunders: 18 March 2009 - 05:32 PM

Was This Post Helpful? 1
  • +
  • -

#3 KWags   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 27
  • Joined: 11-February 09

Re: Error Code

Posted 18 March 2009 - 05:37 PM

Oh, that's such a good idea!!! Thank you! I'll give it a try :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1