4 Replies - 4380 Views - Last Post: 25 November 2007 - 07:45 PM Rate Topic: -----

#1 lockdown  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 394
  • Joined: 29-September 07

Subtraction Loop Issue

Post icon  Posted 24 November 2007 - 07:42 PM

I am creating a program that uses functions and loops to give the user the ability to add or subtract numbers. The other part of my code are working properly but I have found what I believe is referred to as a logical error. In the subtraction function the error is caused when the user enter the first number. The do statement will take that first number as make it = to the total. So with total declared as 0 when it was declared the equation would be 0 - 3 = -3 which would not be correct.

Also just to explain the do statement it allows a user to enter numbers until 0 is entered. Once 0 is entered it exits the do loop and will return the result back to the main function in my code.

Here is the code with the issue
double subNumbers()
{

	double number=1;
	double total=0;

	cout << "\n***************************************************" <<endl;
	cout << "Please enter the numbers you would like to subtract" <<endl;
	cout << "Enter 0 to display the result" <<endl;
	cout << "***************************************************" <<endl;

	do
	{
		cout << "Please enter a number: ";
		cin >> number;

		total = total - number;

	}while (number !=0);

	return total; 

}



I tried to uses a if statement to correct the issue but this then caused all calculations to be off for subtraction which I could not determine the reason for it.

double subNumbers()
{

	double number=1;
	double total=0;
		int count=0;

	cout << "\n***************************************************" <<endl;
	cout << "Please enter the numbers you would like to subtract" <<endl;
	cout << "Enter 0 to display the result" <<endl;
	cout << "***************************************************" <<endl;

	do
	{
		cout << "Please enter a number: ";
		cin >> number;
			   
				if (count == 0)
				   {
					 total = number;
					 count++;
				   }

		total = total - number;

	}while (number !=0);

	return total; 

}



I could also put it outside the do loop to take the first number entered and set it equal to the total but I am trying to avoid that. It will take extra code to validate the number enter to make sure it is not 0.

This all might seem a little confusing since I am not that great a explaining a issue. Can someone please recommend a way to correcting my error so the subtraction will be done properly inside my do loop.

Thanks

This post has been edited by lockdown: 24 November 2007 - 07:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Subtraction Loop Issue

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Subtraction Loop Issue

Posted 24 November 2007 - 09:13 PM

I am not sure what it is you are doing... but this is what I gather:
if I enter 100, 10, 10, 0 in the subtraction is should give me (100 - 10) - 10 = 80

so:
double subNumbers()
{
	double number=1;
	double total=0;
	int count=false;
	
	cout << "\n***************************************************" <<endl;
	cout << "Please enter the numbers you would like to subtract" <<endl;
	cout << "Enter 0 to display the result" <<endl;
	cout << "***************************************************" <<endl;
	
	do
	{
		cout << "Please enter a number: ";
		cin >> number;
		if (!count)
		{
			total = number;
			count = true;
		} else {
			total = total - number;
		}		
	}while (number !=0);
	return total;
}

Was This Post Helpful? 0
  • +
  • -

#3 lockdown  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 394
  • Joined: 29-September 07

Re: Subtraction Loop Issue

Posted 25 November 2007 - 06:23 PM

The else corrected the issue.

Also I looked at the suggestion you gave NickDMax and their was a error in it. When you declared the count variable you set it equal to false. This would cause the if statement you added not to run since if count = false it will not run. So the if statement wont ever run in the program. Count needs to be declared as true so after the first number is entered it will run the if statement and then set count to false so it wont run after the second number is entered.

I dont mean to rip threw your suggestion, I just wanted to point of the their was a issue with it.

Here is the working code.
double subNumbers() // Subtraction function - Ask user to enter in numbers and returns the result to the main function
{

	double number=1; // Declares number vairable as double and sets it eqaul to 1
	double total=0; // Declares total variable as double and sets it equal to 0
	int count=true;

	cout << "\n***************************************************" <<endl;
	cout << "Please enter the numbers you would like to subtract" <<endl;
	cout << "Enter 0 to display the result" <<endl;
	cout << "***************************************************" <<endl;

	do // Do statement to ask user to enter in numbers and preform the subtraction equation
	{
		cout << "Please enter a number: ";
		cin >> number;

		if(count)
			{
				total = number;
				count = false;
			}

		else 
			{
				total = total - number;
			}
		
	}while(number !=0);

	return total; // Returns total variable to the main function



Also could someone explain why the else statement corrected the issue?

Also just for anyone else reference here is the code for the program I was creating. I have not finished all the comments and I am still making it look better when run.

//
//
//

#include <iostream>
using namespace std;

void menu();		  // Declares menu function  
int choiceVal();	  // Declares choice validaion function
double addNumbers();  // Declares add numbers function
double subNumbers();  // Declares subtraction number function

void main()
{
	int choice=0;		  // Declares choice vairable as local int and sets it equal to 0
	double total=0;		// Declares total variable as local double and sets it equal to 0
	static int addCount=0; // Declares add count vairbale as a static local int and sets it equal to 0
	static int subCount=0; // Declares sub count vairable as a static local int and sets it equal to 0

	cout << "This application will allow you to add or subtract numbers you enter" <<endl;
	
	while (choice !=3) // While statment to call the functions until 3 is enter to end the application
	{

	menu(); // Calls menu function 

	choice = choiceVal(); // Calls choice validation function 
		
	if (choice == 1) // Addition if statement and keeps count of times function is called
		{
			total = addNumbers();
			addCount++;
		}

	if (choice == 2) // Subtraction if statement and keeps count of times function is called.
		{
			total = subNumbers();
			subCount++;
		}
	
	if (choice !=3) // Total if statement that when the total value is returned it will be displayed if choice 1 or 2 was selected from the menu
		{		  
			cout << "The total is: " << total <<endl;
		}
	}

	// Displays times each function was called
	cout << "\nThe total amount of times you selected addition from the menu was " << addCount <<endl;
	cout << "The total amount of times you selected subtraction from the menu was " << subCount <<endl;

	return;

}


void menu() // Void Menu function - Displays menu for user to select from
{
	
	cout << "\n********************************************" <<endl;
	cout << "* Please select from the fallowing choices *" <<endl;
	cout << "********************************************" <<endl;
	cout << "(1): For addition please enter 1 " <<endl;
	cout << "(2): For subtraction please enter 2 " <<endl;
	cout << "(3): To exit the program please enter 3 " <<endl;
	cout << "********************************************" <<endl; 
	cout << "Choice: ";

}

int choiceVal() // Int Choice Validation Function - User enter in choice, it is validated to make sure it is a valid selection
{

	int choice=0;

	cin >> choice; // User enter in choice

	if (choice !=1 || choice !=2 || choice !=3) // If statement to validate the choice entered
		{
			while (choice !=1 && choice !=2 && choice !=3) // While statement to prompt user that the choice was invalid
			{
				cout << "\nError - Invalid choice" <<endl;
				cout << "Please select from the fallowing menu\n" <<endl;

				menu(); // Calls menu function to redisplay it to the user

				cin >> choice; // User re-enters choice
			}
		}

	return choice; 
}


double addNumbers() // Addition function - Ask user to enter in numbers and returns the result to the main function
{
	
	double number=1; // Declares number vairbale as double and sets it equal to 1
	double total=0; // Declares total variable as double and sets it equal to 0

	cout << "\n***********************************************" << endl;
	cout << "Please enter the numbers you would like to add" <<endl;
	cout << "Enter 0 to display the result" <<endl;
	cout << "************************************************" <<endl;

	do // Do statement that ask user to enter in numbers and preform the equation
	{
		cout << "Please enter a number: ";
		cin >> number;

		total = total + number; // Addition equation 

	}while (number !=0);

	return total; // Returns total variable to the main function

}

double subNumbers() // Subtraction function - Ask user to enter in numbers and returns the result to the main function
{

	double number=1; // Declares number vairable as double and sets it eqaul to 1
	double total=0; // Declares total variable as double and sets it equal to 0
	int count=true;

	cout << "\n***************************************************" <<endl;
	cout << "Please enter the numbers you would like to subtract" <<endl;
	cout << "Enter 0 to display the result" <<endl;
	cout << "***************************************************" <<endl;

	do // Do statement to ask user to enter in numbers and preform the subtraction equation
	{
		cout << "Please enter a number: ";
		cin >> number;

		if(count)
			{
				total = number;
				count = false;
			}

		else 
			{
				total = total - number;
			}
		
	}while(number !=0);

	return total; // Returns total variable to the main function

}



Thanks everyone for the help.

This post has been edited by lockdown: 25 November 2007 - 07:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Subtraction Loop Issue

Posted 25 November 2007 - 06:49 PM

I htink in my code the if statement was suppose to be:

if (!count)

the reason for this is that count starts out as false. Sorry about that.
Was This Post Helpful? 0
  • +
  • -

#5 lockdown  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 394
  • Joined: 29-September 07

Re: Subtraction Loop Issue

Posted 25 November 2007 - 07:45 PM

Yeah I made that same thing in my program with a while loop. Could not figure out why it was messing up and then went back 3 times and finally found it. I hate the little things that mess it all up :P

Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1