![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| Welcome
to Tech Support Forum home to more then 136,000 problems solved. Issues
have included: Spyware, Malware, Virus Issues, Windows, Microsoft,
Linux, Networking, Security, Hardware, and Gaming Getting your
problem solved is as easy as: 1. Registering for a free account 2. Asking your question 3. Receiving an answer Registered members: * See fewer ads. * And much more..
|
| Want to know how to post a question? click here | Having problems with spyware and pop-ups? First Steps |
|
|||||||
| Programming A discussion forum for programs and programming used in tech-related businesses. |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Registered User
Join Date: Jun 2007
Posts: 21
OS: winxp
|
c++ loop help
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). |
|
|
|
| Important Information |
|
Join the #1 Tech Support Forum Today - It's Totally Free!
TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free. Join TechSupportforum.com Today - Click Here |
|
|
#2 (permalink) |
|
Registered User
Join Date: Jun 2007
Posts: 21
OS: winxp
|
Re: c++ loop help
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; } |
|
|
|
|
|
#3 (permalink) |
|
Mentor, Analyst - Security Team
Join Date: May 2006
Location: Oregon
Posts: 2,503
OS: MacOS X, Debian, OpenBSD, Windows
|
Re: c++ loop help
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:
__________________
The chance to begin again in a golden land of opportunity and adventure. Need HijackThis help? Please read MicroBell's Five Step Process before posting.
Please donate and help keep this site free to all. ![]() UNITE/ASAP: Proud member since 2006 |
|
|
|
|
|
#4 (permalink) |
|
Registered User
Join Date: Jun 2007
Posts: 21
OS: winxp
|
Re: c++ loop help
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 |
|
|
|
|
|
#6 (permalink) |
|
Mentor, Analyst - Security Team
Join Date: May 2006
Location: Oregon
Posts: 2,503
OS: MacOS X, Debian, OpenBSD, Windows
|
Re: c++ loop help
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.
Code:
#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;
}
__________________
The chance to begin again in a golden land of opportunity and adventure. Need HijackThis help? Please read MicroBell's Five Step Process before posting.
Please donate and help keep this site free to all. ![]() UNITE/ASAP: Proud member since 2006 |
|
|
|
|
|
#7 (permalink) | |
|
Registered User
|
Re: c++ loop help
Quote:
mate you got the above "if condition" wrong. It should be if (choice < 1 || choice > 4) { // invalid choice } |
|
|
|
|
|
|
#8 (permalink) |
|
Mentor, Analyst - Security Team
Join Date: May 2006
Location: Oregon
Posts: 2,503
OS: MacOS X, Debian, OpenBSD, Windows
|
Re: c++ loop help
You're right. It was a test. You passed.
__________________
The chance to begin again in a golden land of opportunity and adventure. Need HijackThis help? Please read MicroBell's Five Step Process before posting.
Please donate and help keep this site free to all. ![]() UNITE/ASAP: Proud member since 2006 |
|
|
|
|
|
#9 (permalink) |
|
Registered User
Join Date: Jun 2007
Posts: 21
OS: winxp
|
Re: c++ loop help
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; } |
|
|
|
|
|
#10 (permalink) |
|
Registered User
Join Date: Jun 2007
Posts: 21
OS: winxp
|
Re: c++ loop help
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? |
|
|
|
|
|
#11 (permalink) |
|
Registered User
Join Date: Jun 2007
Posts: 21
OS: winxp
|
Re: c++ loop help
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
|
|
|
|
|
|
#14 (permalink) |
|
Mentor, Analyst - Security Team
Join Date: May 2006
Location: Oregon
Posts: 2,503
OS: MacOS X, Debian, OpenBSD, Windows
|
Re: c++ loop help
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.
__________________
The chance to begin again in a golden land of opportunity and adventure. Need HijackThis help? Please read MicroBell's Five Step Process before posting.
Please donate and help keep this site free to all. ![]() UNITE/ASAP: Proud member since 2006 |
|
|
|
|
|
#15 (permalink) |
|
Registered User
Join Date: Jun 2007
Posts: 21
OS: winxp
|
Re: c++ loop help
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 |
|
|
|
|
|
#16 (permalink) |
|
Registered User
Join Date: Jun 2007
Posts: 21
OS: winxp
|
Re: c++ loop help
//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; } |
|
|
|
|
|
#18 (permalink) |
|
Registered User
|
Re: c++ loop help
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
Last edited by techsoul; 06-13-2007 at 03:32 PM. |
|
|
|
|
|
#19 (permalink) |
|
Mentor, Analyst - Security Team
Join Date: May 2006
Location: Oregon
Posts: 2,503
OS: MacOS X, Debian, OpenBSD, Windows
|
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);Why not do the validation/verification in the functions themselves? That is to say, in the getAge() function, Code:
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
}
Code:
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);
}
Code:
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;
}
__________________
The chance to begin again in a golden land of opportunity and adventure. Need HijackThis help? Please read MicroBell's Five Step Process before posting.
Please donate and help keep this site free to all. ![]() UNITE/ASAP: Proud member since 2006 |
|
|
|
|
|
#20 (permalink) |
|
Registered User
Join Date: Jun 2007
Posts: 21
OS: winxp
|
Re: c++ loop help
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 |
|
|
|
![]() |
| Thread Tools | |
|
|