# c++ loop help



## miken5678 (Jun 9, 2007)

my project is.. listed below.. keep in mind this is the beginning of my c++ class and we do have some knowledge of loops but as far as nesting the loops ending them within nestings and counters/accumulators.. i am stuck. any help would be appreciated


Include a welcome screen for the program below. 

Write a menu driven program to calculate monthly member rates for a health spa with the following options:

1. AARP Rate //this is for person's 50 and older. $29.95 per month.
2. Student Rate // this is for persons less than 18 years old. $19.95 per month.
3. Regular Rate //for everyone else is $45.95 per month.
4. Exit //this end the program.

Ask the user to enter the number of months they want to join. (Don't allow negative numbers. Keep prompting the user to enter a positive value. Pre-test loop.)

Then ask the user for their age (Don't allow negative numbers and keep prompting until the user enters a positive number. Pre-test loop.)

Provide the user with the opportunity to run the program till they want to stop. (Do you want to run the program again?)

If the user says Y or y, run the program again. Otherwise, end the program. (Post test loop).

Before the program ends, tell the user how many times the program was run (add a count). Also tell the user how much money in fees what calculated (accumulator).


----------



## miken5678 (Jun 9, 2007)

this is my code so far using bloodshed

#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, char *argv[])
{
int choice, months, age, count=0, sum=0;
double charges;

cout << fixed << showpoint << setprecision(2);

do
{
cout << "\n\t\tHealth Spa Rate Calculator Menu\n\n";
cout << " 1. AARP Rate\n";
cout << " 2. Student Rate\n";
cout << " 3. Regular Rate\n";
cout << " 4. Exit\n\n";
cout << "Enter Your Choice: ";
cin >> choice;
while (choice >= 1 && choice <= 3)
{
cout << "Enter Your Age: ";
cin >> age;
while (age <= 0)
{
cout << "\nInvalid\n";
cout << "Enter Age "; 
cin >> age;
}
cout << "For how many months?: ";
cin >> months;
while (months <= 0)
{
cout << "\nInvalid\n";
cout << "Enter Months "; 
cin >> months;
} 
if (choice = 1)
{ 
charges = months * 29.95;
cout << "The total charge is $";
cout << charges << endl;
}
else if (choice = 2)
{ 
charges = months * 19.95;
cout << "The total charge is $";
cout << charges << endl;
}
else if (choice = 3) 
{
charges = months * 19.95; 
cout << "The total charge is $";
cout << charges << endl;
}
else
{ 
cout << "The valide choices are 1 through 4.\n";
 cout << "Try again. \n";
}

} while (choice !=4);
return EXIT_SUCCESS;
}


----------



## Deckard (May 8, 2006)

Your else "valid choices are 1-4" will never be reached because you are only entering in that block of code if choice is between 1 and 3 (which are all valid).

Here is an alternate take on the logic:


get choice from user.
if choice <1 or >4 then print error and go to step 1.
else if choice <4 (i.e., 1, 2, or 3)
increment # of times run
get number of months (can someone join for less than a month?)
get age (is zero positive or negative? :grin
calculate and output cost. add cost to total session amount.
ask user if they want to continue [Y/N]. Loop until Y/y or N/n.

if choice = 4 OR user chose to end (Y/y), print out # of times run, total session amount, and quit.
otherwise, loop back to step 1.


----------



## miken5678 (Jun 9, 2007)

since im still new to loops bear with me

i understand what your saying and can do most of the items on there own but integrating them together and getting them to function together is another issue..

i take it the items you suggested could all be done within the one do while menu group.. ? i tried doing another while loop after the pretest do while and the compiler would never go through them when i tried to run it


----------



## techsoul (Jun 7, 2007)

when is your project due?


----------



## Deckard (May 8, 2006)

Sure, here's less of an outline and more code to give you an idea. There are a few ways to do this, but this should get you started.


```
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, char *argv[])
{
	int choice, months, age, count=0, sum=0;
	double charges;

	cout << fixed << showpoint << setprecision(2);

	do
	{
		cout << "\n\t\tHealth Spa Rate Calculator Menu\n\n";
		cout << " 1. AARP Rate\n";
		cout << " 2. Student Rate\n";
		cout << " 3. Regular Rate\n";
		cout << " 4. Exit\n\n";
		cout << "Enter Your Choice: ";

		cin >> choice;
		
		if (choice > 1 || choice < 4)
		{
			// invalid choice
		}
		else // choice is 1..4
		{
			if (choice < 4) // choice is 1..3
			{
				// get age
			
				// get number of months

				// calculate charges
				if (choice == 1)	// note that it is == and not =
				{
					// calculate 29.95 rate
				}
				 
				else if (choice == 2)
				{
					// 19.95
				}
				
				else // can only be 3 at this point; but you can test "== 3" if you want
				{
					// 45.95
				}

				// show charges to user

				// ask if user wants to run again Y/N
				// if N, set choice = 4 so the loop will exit.
				// if Y, do nothing (we will loop again).

			} // choice is 1..3
			
		} // choice is 1..4
		
	} while (choice != 4);  // a 4 will exit.

	// print out number of times run and what the total fees are.
	
	return EXIT_SUCCESS;
}
```


----------



## techsoul (Jun 7, 2007)

Deckard said:


> int main(int argc, char *argv[])
> {
> int choice, months, age, count=0, sum=0;
> double charges;
> ...




mate you got the above "if condition" wrong. It should be

if (choice < 1 || choice > 4)
{
// invalid choice
}


----------



## Deckard (May 8, 2006)

You're right. It was a test. You passed. :grin:


----------



## miken5678 (Jun 9, 2007)

my mind was stuck in trying to validate the users selection in the do while list with the age input to make sure they were eligible for the selection they chose.. instead it worked out to this


#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

const double AARP_RATE = 29.95;
const double STUDENT_RATE = 19.95;
const double REGULAR_RATE = 49.95;

int main(int argc, char *argv[])
{
char option; 
int age, months, count = 0;
double rate, totalBill, totalSum = 0;
cout << fixed << setprecision(2); 
//welcome
system("CLS"); 
cout << "\n\n\n\n\n\n"
<< "\n\t\t\t**************************************"
<< "\n\t\t\t* *"
<< "\n\t\t\t* Welcome to the C++ Health Club! *"
<< "\n\t\t\t* Our job is to help you work both *" 
<< "\n\t\t\t* body and mind.  *"
<< "\n\t\t\t* *"
<< "\n\t\t\t**************************************"
<< "\n\n\n\n\n\n\n\n\n\n\n";
system("PAUSE");

do 
{
//display the menu
system("CLS");
cout << "\n\n\n\n\t\t\tAARP Rate (50 or more years)"
<< "\n\n\t\t\tStudent Rate (18 or less years)"
<< "\n\n\t\t\tRegular Rate (everyone else)"
<< "\n\n\n\n\t\t\tHow old are you? ";

//get the age
cin >> age;
//validate the input
while (age < 0)
{
cout << "\n\n\t\tInvalid! Enter a positive number.";
cout << "\n\n\t\tHow old are you? ";
cin >> age;
}
//process the age
if (age <= 18)
{
cout << "\n\n\t\tYou qualify for the student rate which is $19.95.";
rate = STUDENT_RATE;
}
else if (age < 50)
{
cout << "\n\n\t\tYou qualify for the regular rate at $45.95.";
rate = REGULAR_RATE;
}
else 
{
cout << "\n\n\t\tYou qualify for the AARP rate at $29.95";
rate = AARP_RATE;
} 
//get the months
cout << "\n\n\t\tHow many months do you want to join? ";
cin >> months;
//validate the input
while (months < 0)
{
cout << "\n\n\t\tInvalid! Enter a positive number.";
cout << "\n\n\t\tHow many months do you want to join? ";
cin >> months;
}
//calculate the bill
totalBill = months * rate;
//display the bill
system("CLS");
cout << "\n\n\n\n\n\n\t\tYour total bill is: $" << totalBill << endl << endl 
<< endl << endl << endl << endl << endl;
//add to counter & accumulator
count += 1; 
totalSum += totalBill;

cout << "\n\n\t\tRun the program again? (Y/N) ";
cin >> option; 
} while (option == 'y' || option == 'Y');
//display farewell and summary 
system("CLS"); 
cout << "\n\n\n\n\n\n"
<< "\n\t\t*********************************************"
<< "\n\t\t* "
<< "\n\t\t* Thank you for visitng C++ Health Club! "
<< "\n\t\t* We hope you enjoyed your stay. " 
<< "\n\t\t* "
<< "\n\t\t* " << count << " people(s) used this program"
<< "\n\t\t* $" << totalSum << " was spent. "
<< "\n\t\t* "
<< "\n\t\t*********************************************"
<< "\n\n\n\n\n\n\n\n\n\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}


----------



## miken5678 (Jun 9, 2007)

this class is moving so fast its almost hard to keep up

now im having to go in and change this program to modular

welcome function 
error function 
function to calculate the total (use one and pass it values) 
functions to collect input (age and numbers of months...getAge - don't allow negative ages. getMos - don't allow negative months).

this should be interesting.. not horribly hard.. but any issues i should note before jumping into functions?


----------



## miken5678 (Jun 9, 2007)

the only issue i ended up with on the above completed program was entering letters and decimal numbers.. it caused unexpected results that i didnt know how to account for.. but it wasnt required so im on to functions


----------



## miken5678 (Jun 9, 2007)

i appreciate the help so far.. very much so.. thanks


----------



## techsoul (Jun 7, 2007)

good for you, happy coding


----------



## Deckard (May 8, 2006)

Validating input is not too complicated, I'm sure you'll be introduced to it in class. I can't think of anything that will surprise you in a function -- just remember to keep it simple and try not to repeat yourself in code. :wink:


----------



## miken5678 (Jun 9, 2007)

ok this has changed to make the program modular.

i got bits and pieces to work but an unclear about how to get a function to work inside the do stream.. also noted that i cannot get the final screen to pop up after n is entered 

it is stated

functions to collect input (age and numbers of months...getAge - don't allow negative ages. getMos - don't allow negative months).
function to calculate the total (use one and pass it values) 

maybe that is where i am going wrong.. advice is appreciated


----------



## miken5678 (Jun 9, 2007)

//Michael J. Nelson
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

const double AARP_RATE = 29.95;
const double STUDENT_RATE = 19.95;
const double REGULAR_RATE = 49.95;
void welcome();
void exit();
void month(int);
void ages(int);
//void months(int)
//void age(int)

int main(int argc, char *argv[])
{
char option; 
int age, months, count = 0;
double rate, totalBill, totalSum = 0;
cout << fixed << setprecision(2); 
//welcome
system("CLS"); 

welcome();


do 
{
//display the menu
system("CLS");
ages;
//validate the input
while (age < 0)
{
cout << "\n\n\t\tInvalid! Enter a positive number.";
cout << "\n\n\t\tHow old are you? ";
cin >> age;
}
//process the age
if (age <= 18)
{
cout << "\n\n\t\tYou qualify for the student rate which is $19.95.";
rate = STUDENT_RATE;
}
else if (age < 50)
{
cout << "\n\n\t\tYou qualify for the regular rate at $45.95.";
rate = REGULAR_RATE;
}
else 
{
cout << "\n\n\t\tYou qualify for the AARP rate at $29.95";
rate = AARP_RATE;
} 
//get the months
cout << "\n\n\t\tHow many months do you want to join? ";
cin >> months;
//validate the input
while (months < 0)
{
void month(int);
}
//calculate the bill
totalBill = months * rate;
//display the bill
system("CLS");
cout << "\n\n\n\n\n\n\t\tYour total bill is: $" << totalBill << endl << endl 
<< endl << endl << endl << endl << endl;
//add to counter & accumulator
count += 1; 
totalSum += totalBill;

cout << "\n\n\t\tRun the program again? (Y/N) ";
cin >> option; 
} while (option == 'y' || option == 'Y');
//display farewell and summary 
system("CLS"); 
void exit();
return EXIT_SUCCESS;
}
void welcome()
{
cout << "\n\n\n\n\n\n"
<< "\n\t\t\t**************************************"
<< "\n\t\t\t* *"
<< "\n\t\t\t* Welcome to the C++ Health Club! *"
<< "\n\t\t\t* Our job is to help you work both *" 
<< "\n\t\t\t* body and mind. *"
<< "\n\t\t\t* *"
<< "\n\t\t\t**************************************"
<< "\n\n\n\n\n\n\n\n\n\n\n";
system("PAUSE"); 
}
void exit(int count, double totalSum)
{
cout << "\n\n\n\n\n\n"
<< "\n\t\t*********************************************"
<< "\n\t\t* "
<< "\n\t\t* Thank you for visitng C++ Health Club! "
<< "\n\t\t* We hope you enjoyed your stay. " 
<< "\n\t\t* "
<< "\n\t\t* " << count << " people(s) used this program"
<< "\n\t\t* $" << totalSum << " was spent. "
<< "\n\t\t* "
<< "\n\t\t*********************************************"
<< "\n\n\n\n\n\n\n\n\n\n\n";
system("PAUSE");
}
void month(int months)
{
cout << "\n\n\t\tInvalid! Enter a positive number.";
cout << "\n\n\t\tHow many months do you want to join? ";
cin >> months;
}
void ages(int age)
{
cout << "\n\n\n\n\t\t\tAARP Rate (50 or more years)"
<< "\n\n\t\t\tStudent Rate (18 or less years)"
<< "\n\n\t\t\tRegular Rate (everyone else)"
<< "\n\n\n\n\t\t\tHow old are you? ";

//get the age
cin >> age; 
}


----------



## miken5678 (Jun 9, 2007)

is it indeed possible to replace the whole if else if statements with one function?


----------



## techsoul (Jun 7, 2007)

yes it is possible. You need to pass "age" as a const parameter and "rate" as a reference parameter to the function. And cut paste all the if>else if>else statements in that function. OR you can also make "rate" as a global parameter (declare it outside main) and pass only "age" to the function. The function will not return anything. Therefore, it will be a VOID function.

void findRate (const int age1, int& rate1) 
{
// if>else if>else statements
// don't forget to rename rate to rate1 in the function defination.
}

OR
void findRate(const int age)
{
// if>else if>else statements
}


calling the function will look like this


findRate(age, rate);

OR

findRate(age);



good luck :grin:


----------



## Deckard (May 8, 2006)

Ugh. I would *avoid* changing global variables in a function. In fact, you don't even need global variables to do this.

A function takes input and produces output. Reading your summary, you could have something like this:

void welcome(void);
void printError(char *error);
unsigned int getAge(void);
unsigned int getMonths(void);
unsigned double calculateTotal(unsigned int age, unsigned int months);​
Why not do the validation/verification in the functions themselves? That is to say, in the getAge() function,


```
unsigned int getAge(void)
{
  int age; // notice that this is not an unsigned int

  do {
    cout << "How old are you? ";

    // get the age
    cin >> age;

    // validate that age is not negative.
    if (age < 1)
    {
      printError("Your age must be a positive number.\n\n");
    }
  } while(age < 1);

  return age;  // 1..MAX_INT
}
```
calculateTotal() would be something like:

```
unsigned double calculateTotal(unsigned int age, unsigned int months)
{
  unsigned double rate;

  if (age < 18)
  {
    rate = 19.95;
  }
  else if (age < 50)
  {
   // .. and so on
  }

  return(rate * months);
}
```
Then your main() loop would be something like:


```
void main(void) {
  unsigned int age, months, count = 0;
  unsigned double rate = 0.0, total = 0.0;

  do {
    welcome();

    age = getAge();
    months = getMonths();
    rate = calculateTotal(age, months);
    total += rate;
    count++;

    // show results to user
    // and ask user if they want to go around again
  } while (choice = true); // however you want to do it.

  exit;
}
```
See how clear and concise it is?


----------



## miken5678 (Jun 9, 2007)

on to next question.. this is my final program for the class.. i have most of it down but.. there is a question

for options b&c call a function that displays " you selected option 'B'. or ......

this function should accept a parameter indication which character was selected


----------



## miken5678 (Jun 9, 2007)

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;
void welcome();
void error(char);
void summary(int, float, float);
void hastalavista();
void the_end();
char choicemenu();
float getPrice();
int getQuantity();
float operation();

int main(int argc, char *argv[])
{
int quantity;
float price, total;
char option, choose;

welcome();

do
{ 
option = choicemenu();

if (option == 'D')
break;
else
{
if (option == 'A' || option == 'a')
{
quantity = getQuantity();
price = getPrice();
} 
else if (option == 'B' || option == 'b');
{ 

}
else if (option == 'C' || option == 'c');
{
}
else
error(option);
total = operation(price, quantity);
summary(quantity, price, total);
}
}
while (choose == 'Y');
hastalavista();
return EXIT_SUCCESS;
}
void welcome()
{
system("CLS");
cout <<"\n\n\n";
cout <<"*********************************************\n";
cout <<"* Welcome To the ACME Merchandise Program *\n";
cout <<"*********************************************\n\n\n\n";
system("PAUSE");
}
char choicemenu()
{
char choice;
system("CLS");
cout << "\t\tACME Merchandise Menu\n"; 
cout << "\t\t A. Calculate Price\n";
cout << "\t\t B. 2nd Option\n";
cout << "\t\t C. 3rd Option\n";
cout << "\t\t D. Exit\n";
cout << "\t\t\a Please Make Your Selection: ";
cin >> choice;
return choice;
}
void error(char value)
{
system("CLS");
cout << "/n/n/n";
cout << "** * * * * * * * * * * * * * * * * * *\n";
cout << "* * * * * * * * * * * * * * * * * * **\n";
cout << "*\t\t Wrong Choice *\n";
cout << "*\t\t\t" << value << "is not a valid selection *\n";
cout << "* * * * * * * * * * * * * * * * * * * *\n";
cout << "** * * * * * * * * * * * * * * * * * **";
system("PAUSE");
}
void summary(int quantity, float price, float total)
{
system("CLS");
cout << fixed << setprecision(2);
cout << "\n\n\n";
cout << "\n\t Total Price Calculation\n";
cout << "\t Quantity entered: " << setw(10) << quantity; 
cout << "\t\t Price entered: " << setw(10) << price;
cout << "\t\t\t Price is: " << setw(10) << quantity*price;

}
void hastalavista
{
system("CLS");
cout << "\n\n\n\";
cout << " Thanks for using the C++ ACME Calculator";
cout << " \t\t Total Running Price is " << sum << "\n";
system("PAUSE");
}
int getQuantity()
{
int quantity;
cout <<" Please enter the Quantity\n";
cin >> quantity;
while (quantity <0)
{
cout << "You cannot have negative quantity";
cout << "Please re-enter\n";
cin >> quantity;
}
return quantity;
}
float getPrice()
{
float price;
cout <<" Please enter the Price/n";
cin >> price;
while (price <0);
{
cout << "Hmm negative Price..../n";
cout << "Please Double Check Values and re-enter\n";
cin >> price;
}
return price;
}
float operation(float price, int quantity)
{
return price * quantity;
}

void b&c


----------



## miken5678 (Jun 9, 2007)

is it possible to just do a function within the else if nested statement then adding a break at the end so that it skips the calculations as they arent needed in choice b&c? ?


----------



## miken5678 (Jun 9, 2007)

these are what im trying to fix so far.. the only issue being line 46/14 

the others i realize are because of no else if statements
aved files/final.cpp:41: error: expected primary-expression before "else"
Saved files/final.cpp:41: error: expected `;' before "else"
Saved files/final.cpp:44: error: expected primary-expression before "else"
Saved files/final.cpp:44: error: expected `;' before "else"
Saved files/final.cpp:14: error: too many arguments to function `float operation()'
Saved files/final.cpp:46: error: at this point in file

Saved files/final.cpp: At global scope:
Saved files/final.cpp:138: error: expected init-declarator before '&' token
Saved files/final.cpp:138: error: expected `,' or `;' before '&' token

make.exe: *** ["Saved files/final.o"] Error 1


----------



## ricer333 (Sep 17, 2004)

I believe why you are getting the error at that line is due to your forward declaration of the fuction 'operation'.


```
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;
void welcome();
void error(char);
void summary(int, float, float); // PARAMETER LIST - int, float, float
void hastalavista();
void the_end();
char choicemenu();
float getPrice();
int getQuantity();
float operation(); // NO PARAMETER LIST HERE - ()
//FIX FOR COMPILATION:: float operation(float, int);

int main(int argc, char *argv[])
{
int quantity;
float price, total;
char option, choose;

welcome();

do
{ 
option = choicemenu();

if (option == 'D')
break;
else
{
if (option == 'A' || option == 'a')
{
quantity = getQuantity();
price = getPrice();
} 
else if (option == 'B' || option == 'b');
{ 

}
else if (option == 'C' || option == 'c');
{
}
else
error(option);
total = operation(price, quantity); // COMPILER is expecting () 
summary(quantity, price, total);
}
}
while (choose == 'Y');
hastalavista();
return EXIT_SUCCESS;
}
void welcome()
{
system("CLS");
cout <<"\n\n\n";
cout <<"*********************************************\n";
cout <<"* Welcome To the ACME Merchandise Program *\n";
cout <<"*********************************************\n\n\n\n";
system("PAUSE");
}
char choicemenu()
{
char choice;
system("CLS");
cout << "\t\tACME Merchandise Menu\n"; 
cout << "\t\t A. Calculate Price\n";
cout << "\t\t B. 2nd Option\n";
cout << "\t\t C. 3rd Option\n";
cout << "\t\t D. Exit\n";
cout << "\t\t\a Please Make Your Selection: ";
cin >> choice;
return choice;
}
void error(char value)
{
system("CLS");
cout << "/n/n/n";
cout << "** * * * * * * * * * * * * * * * * * *\n";
cout << "* * * * * * * * * * * * * * * * * * **\n";
cout << "*\t\t Wrong Choice *\n";
cout << "*\t\t\t" << value << "is not a valid selection *\n";
cout << "* * * * * * * * * * * * * * * * * * * *\n";
cout << "** * * * * * * * * * * * * * * * * * **";
system("PAUSE");
}
void summary(int quantity, float price, float total)
{
system("CLS");
cout << fixed << setprecision(2);
cout << "\n\n\n";
cout << "\n\t Total Price Calculation\n";
cout << "\t Quantity entered: " << setw(10) << quantity; 
cout << "\t\t Price entered: " << setw(10) << price;
cout << "\t\t\t Price is: " << setw(10) << quantity*price;

}
void hastalavista
{
system("CLS");
cout << "\n\n\n\";
cout << " Thanks for using the C++ ACME Calculator";
cout << " \t\t Total Running Price is " << sum << "\n";
system("PAUSE");
}
int getQuantity()
{
int quantity;
cout <<" Please enter the Quantity\n";
cin >> quantity;
while (quantity <0)
{
cout << "You cannot have negative quantity";
cout << "Please re-enter\n";
cin >> quantity;
}
return quantity;
}
float getPrice()
{
float price;
cout <<" Please enter the Price/n";
cin >> price;
while (price <0);
{
cout << "Hmm negative Price..../n";
cout << "Please Double Check Values and re-enter\n";
cin >> price;
}
return price;
}
float operation(float price, int quantity)
{
return price * quantity;
}

void b&c
```


----------



## miken5678 (Jun 9, 2007)

thanks it fixed the error

now im just trying to do this part of it"

"for options b&c call a function that displays " you selected option 'B'. or ......

this function should accept a parameter indication which character was selected"

im trying to figure out a way to rewrite things so that if my else states are used and i use the break feature to get out of the loop..


----------



## miken5678 (Jun 9, 2007)

this is my code so far.. im not sure if i can just call the function inside the else ifs like that or not

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;
void welcome();
void error(char);
void bc(char);
void summary(int, float, float);
void hastalavista();
void the_end();
char choicemenu();
float getPrice();
int getQuantity();
float operation(float, int);

int main(int argc, char *argv[])
{
int quantity;
float price, total;
char option, choose;

welcome();

do
{ 
option = choicemenu();

if (option == 'D')
break;
else
{
if (option == 'A' || option == 'a')
{
quantity = getQuantity();
price = getPrice();
} 
else if (option == 'B' || option == 'b');
{ 
bc(option);
}
else if (option == 'C' || option == 'c');
{
bc(option);
}
else
error(option);
total = operation(price, quantity);
summary(quantity, price, total);
}
}
while (choose == 'Y');
hastalavista();
return EXIT_SUCCESS;
}
void welcome()
{
system("CLS");
cout <<"\n\n\n**";
cout <<"*********************************************\n";
cout <<"* Welcome To the ACME Merchandise Program *\n";
cout <<"*********************************************\n\n\n\n";
system("PAUSE");
}
char choicemenu()
{
char choice;
system("CLS");
cout << "\t\tACME Merchandise Menu\n"; 
cout << "\t\t A. Calculate Price\n";
cout << "\t\t B. 2nd Option\n";
cout << "\t\t C. 3rd Option\n";
cout << "\t\t D. Exit\n";
cout << "\t\t\a Please Make Your Selection: ";
cin >> choice;
return choice;
}
void error(char value)
{
system("CLS");
cout << "/n/n/n";
cout << "** * * * * * * * * * * * * * * * * * *\n";
cout << "* * * * * * * * * * * * * * * * * * **\n";
cout << "*\t\t Wrong Choice *\n";
cout << "*\t\t\t" << value << "is not a valid selection *\n";
cout << "* * * * * * * * * * * * * * * * * * * *\n";
cout << "** * * * * * * * * * * * * * * * * * **";
system("PAUSE");
}
void summary(int quantity, float price, float total)
{
system("CLS");
cout << fixed << setprecision(2);
cout << "\n\n\n";
cout << "\n\t Total Price Calculation\n";
cout << "\t Quantity entered: " << setw(10) << quantity; 
cout << "\t\t Price entered: " << setw(10) << price;
cout << "\t\t\t Price is: " << setw(10) << quantity*price;

}
void hastalavista(float sum)
{
system("CLS");
cout << "\n\n\n***********************************";
cout << " Thanks for using the C++ ACME Calculator";
cout << " \t\t Total Running Price is " << sum << "\n";
system("PAUSE");
}
int getQuantity()
{
int quantity;
cout <<" Please enter the Quantity\n";
cin >> quantity;
while (quantity <0)
{
cout << "You cannot have negative quantity";
cout << "Please re-enter\n";
cin >> quantity;
}
return quantity;
}
float getPrice()
{
float price;
cout <<" Please enter the Price/n";
cin >> price;
while (price <0);
{
cout << "Hmm negative Price..../n";
cout << "Please Double Check Values and re-enter\n";
cin >> price;
}
return price;
}
float operation(float price, int quantity)
{
return price * quantity;
}

void bc(char value)
{
system("CLS");
cout << "/n/n/n";
cout << "** * * * * * * * * * * * * * * * * * *\n";
cout << " You Selected Option: " << value;
system("PAUSE");
}


----------



## miken5678 (Jun 9, 2007)

these are the errors im getting.. what is the issue with my else statements?

E:\Dev-Cpp\Saved files\final.cpp: In function `int main(int, char**)':
E:\Dev-Cpp\Saved files\final.cpp:42: error: expected primary-expression before "else"
E:\Dev-Cpp\Saved files\final.cpp:42: error: expected `;' before "else"
E:\Dev-Cpp\Saved files\final.cpp:46: error: expected primary-expression before "else"
E:\Dev-Cpp\Saved files\final.cpp:46: error: expected `;' before "else"


----------



## miken5678 (Jun 9, 2007)

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;
void welcome();
void error(char);
void bc(char);
void summary(int, float, float);
void hastalavista(float);
void the_end();
char choicemenu();
float getPrice();
int getQuantity();
float operation(float, int);

int main(int argc, char *argv[])
{
int quantity;
float price, total, sum = 0;
char option, choice;

welcome();

do
{ 
option = choicemenu();

if (option == 'D')
break;
else
{
if (option == 'A' || option == 'a')
{
quantity = getQuantity();
price = getPrice();
} 
else if (option == 'B' || option == 'b');
{ 
bc(char)
}
else if (option == 'C' || option == 'c');
{
bc(char)
}
else
error(option);
total = operation(price, quantity);
summary(quantity, price, total);
hastalavista(sum);
}
}
while (choice != 'Y');
return 0;
}
void welcome()
{
system("CLS");
cout <<"\n\n\n**";
cout <<"*********************************************\n";
cout <<"* Welcome To the ACME Merchandise Program *\n";
cout <<"*********************************************\n\n\n\n";
system("PAUSE");
}
char choicemenu()
{
char choice;
system("CLS");
cout << "\t\tACME Merchandise Menu\n"; 
cout << "\t\t A. Calculate Price\n";
cout << "\t\t B. 2nd Option\n";
cout << "\t\t C. 3rd Option\n";
cout << "\t\t D. Exit\n";
cout << "\t\t\a Please Make Your Selection: \n";
cin >> choice;
cout << "\n\n";
return choice;
}
void error(char value)
{
system("CLS");
cout << "\n\n\n";
cout << "** * * * * * * * * * * * * * * * * * **\n";
cout << "* * * * * * * * * * * * * * * * * * * *\n";
cout << "*\t\tWrong Choice *\n";
cout << "*\t\ " << value << " is not a valid selection *\n";
cout << "* * * * * * * * * * * * * * * * * * * *\n";
cout << "** * * * * * * * * * * * * * * * * * **\n";
system("PAUSE");
}
void summary(int quantity, float price, float total)
{
system("CLS");
cout << fixed << setprecision(2);
cout << "\n\n\n";
cout << "\n\t Total Price Calculation\n";
cout << "\t Quantity entered: " << setw(10) << quantity <<"\n"; 
cout << "\t Price entered: " << setw(10) << price << "\n";
cout << "\t\t Price is: " << setw(10) << quantity*price << "\n";
system("PAUSE"); 
}
void hastalavista(float sum)
{
system("CLS");
cout << "\n\n\n******************************************\n";
cout << " Thanks for using the C++ ACME Calculator\n";
cout << " Total Running Price is " << sum << "\n\n";
system("PAUSE");
}
int getQuantity()
{
int quantity;
cout <<" Please enter the Quantity\n";
cin >> quantity;
while (quantity < 0)
{
cout << "You cannot have negative quantity";
cout << "Please re-enter\n";
cin >> quantity;
}
return quantity;
}
float getPrice()
{
float price;
cout <<" Please enter the Price\n";
cin >> price;
while (price < 0);
{
cout << "Hmm negative Price....\n";
cout << "Please Double Check Values and re-enter\n";
cin >> price;
}
return price;
}
float operation(float price, int quantity)
{
return price * quantity;
}

void bc(char value)
{
system("CLS");
cout << "/n/n/n";
cout << "** * * * * * * * * * * * * * * * * * *\n";
cout << " You Selected Option: " << value;
system("PAUSE");
}


----------



## miken5678 (Jun 9, 2007)

for some reason i cannot get the bc function to work within the else if statements.. is that even possible? also if i omit the else if statements for b and c they show up as invalid inputs.. its ok as it is expected.. but then it shows a quantity of 2 for the summary.. also the biggy is why the hell does the get quantity also assume it is a negative number..????


----------



## miken5678 (Jun 9, 2007)

i have most of it figured out except the b and c option and why quantity is listed as two


#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;
void welcome();
void error(char);
void bc(char);
void summary(int, float, float);
void hastalavista(float);
void the_end();
char choicemenu();
float getPrice();
int getQuantity();
float operation(float, int);

int main(int argc, char *argv[])
{
int quantity;
float price, total, sum = 0;
char option, choice;

welcome();

do
{ 
option = choicemenu();

if (option == 'D')
break;
else
{
if (option == 'A' || option == 'a')
{
quantity = getQuantity();
price = getPrice();
} 
//else if (option == 'B' || option == 'b');
//{ 
// //bc();
// }
// else if (option == 'C' || option == 'c');
//{
//bc();
//}
else
error(option);
total = operation(price, quantity);
summary(quantity, price, total);
sum += total;
cout << "\n\n\t Would you like to run this again? ";
cin >> choice;
choice = toupper(choice);

}
}while (choice == 'Y'); 
hastalavista(sum);
return EXIT_SUCCESS;;
}
void welcome()
{
system("CLS");
cout <<"\n\n\n**";
cout <<"*********************************************\n";
cout <<"* Welcome To the ACME Merchandise Program *\n";
cout <<"*********************************************\n\n\n\n";
system("PAUSE");
}
char choicemenu()
{
char choice;
system("CLS");
cout << "\t\tACME Merchandise Menu\n"; 
cout << "\t\t A. Calculate Price\n";
cout << "\t\t B. 2nd Option\n";
cout << "\t\t C. 3rd Option\n";
cout << "\t\t D. Exit\n";
cout << "\t\t\a Please Make Your Selection: \n";
cin >> choice;
cout << "\n\n";
return choice;
}
void error(char value)
{
system("CLS");
cout << "\n\n\n";
cout << "** * * * * * * * * * * * * * * * * * **\n";
cout << "* * * * * * * * * * * * * * * * * * * *\n";
cout << "*\t\tWrong Choice *\n";
cout << "*\t\ " << value << " is not a valid selection *\n";
cout << "* * * * * * * * * * * * * * * * * * * *\n";
cout << "** * * * * * * * * * * * * * * * * * **\n";
system("PAUSE");
}
void summary(int quantity, float price, float total)
{
system("CLS");
cout << fixed << setprecision(2);
cout << "\n\n\n";
cout << "\n\t Total Price Calculation\n";
cout << "\t Quantity entered: " << setw(10) << quantity <<"\n"; 
cout << "\t Price entered: " << setw(10) << price << "\n";
cout << "\t\t Price is: " << setw(10) << quantity*price << "\n";
system("PAUSE"); 
}
void hastalavista(float sum)
{
system("CLS");
cout << "\n\n\n******************************************\n";
cout << " Thanks for using the C++ ACME Calculator\n";
cout << " Total Running Price is " << sum << "\n\n";
system("PAUSE");
}
int getQuantity()
{
int quantity;
cout <<" Please enter the Quantity\n";
cin >> quantity;
while (quantity < 0)
{
cout << "You cannot have negative quantity";
cout << "Please re-enter\n";
cin >> quantity;
}
return quantity;
}
float getPrice()
{
float price;
cout <<" Please enter the Price\n";
cin >> price;
while (price < 0);
{
cout << "Hmm negative Price....\n";
cout << "Please Double Check Values and re-enter\n";
cin >> price;
}
return price;
}
float operation(float price, int quantity)
{
return price * quantity;
}

void bc()
{
system("CLS");
char option; 
if (option == 'B')
{
cout << "You Selected Option 'B'.\n";
}
else 
{
cout << "You Selected Option 'B'.\n";
}
system("PAUSE");
}


----------



## ricer333 (Sep 17, 2004)

I would do something like this:

create a function called printChoice(char c)

Within that function just do a printf


```
printChoice(char c)
{
   printf("You have chosen option %c\n", c);
}
```
Then in your if/else statements above call that function with the cin character.

There may be some format issues (I can't remember if CIN handles input as characters or something else) but try it first.


----------

