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.
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.
CODE
//
//
//
#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 Nov, 2007 - 06:16 PM