When i run the program and select premium it runs it put then it also runs the function for regular service. the problem is in int main at the lines. What did i do wrong? IT says i didnt explain the problem clearly enough, not sure why so here is more text to get around that.
premiumserv = premserv(daymin, nightmin);
regularserv = regserv ();
Full code below
/************************************************/
/* Author: Sam LaManna */
/* Course: CSC 135 Lisa Frye */
/* Assignment: Program 3 Cell Phone Bill */
/* Due Date: 10/20/2011 */
/* Filename: program3.cpp */
/* Purpose: This program calculates and */
/* prints the bill for a cellular */
/* telephone company */
/************************************************/
//TODO:
//Fix account number issue: large numbers cause intfinte loop. Maybe use string?
//Complete premium and regular service calculation functions
//Output function - Figure out how to print
#include <iostream> //Basic input/output
#include <iomanip> //Manipulators
using namespace std;
void instruct(); //Function declaration for printing instructions
int accnum (); //Function declaration for account number
char servcode (); //Function declaration for service code
float regserv (); //Function declaration for regular service
float premserv (int& daymin, int& nightmin); //Function declaration for premium service
void output (float premiumserv, float regularserv); //Function declaration for printing results
int main()
{
int accountnum = 0; //Var for account number
char servicecode = 'a'; //Var for service code
float premiumserv = 0; //Var for premium service total
float regularserv = 0; //Var for regular service total
int daymin = 0;
int nightmin = 0;
instruct(); //Function call to print instructions
accountnum = accnum(); //Function call to input account number
servicecode = servcode(); //Function call to get service code
switch(servicecode) //Switch to determine which calc to use
{
case 'r':
break;
case 'R':
regserv ();
break;
case 'p':
break;
case 'P':
premserv (daymin, nightmin);
break;
}
premiumserv = premserv ( daymin, nightmin); //Function call for regular service
regularserv = regserv (); //Function call for premium service
cout <<"total" << premiumserv <<"day" << daymin << "night " << nightmin;
return 0;
}
/************************************************/
/* Name: instruct */
/* Description: Prints instructions to user */
/* Parameters: N/A */
/* Return Value: N/A */
/************************************************/
void instruct()
{
cout << "\n" << "This program will calculate the cellular telephone bill for a given customer when " << "\n"
<< "given the correct inputs. ";
//Prints instructions to user
return;
}
/************************************************/
/* Name: accnum */
/* Description: Get user account number */
/* Parameters: N/A */
/* Return Value: accnum */
/************************************************/
int accnum()
{
int accnum = 0; //Define var for account number
char accyn = 'n'; //Define char for user verify
int i = 1; //Define var for sentinel loop
do {
cout << "\n" << "\n" << "Please enter your account number: ";
cin >> accnum;
while ((accnum < 0) && (i<3))
{
cout << "\n" << "Error - invalid number, please try again.";
cout << "\n" << "Please enter your account number: ";
cin >> accnum;
i++;
if (i == 3)
{
cout << "\n" << "Too many invalid attempts, program terminating." << "\n"
<< "Have a nice day. " << "\n" << "\n";
exit (0);
} //end if
} //end while
cout << "\n";
cout << "Your account number is: " << accnum << "\n"
<< "Is this correct? [y/n] ";
cin >> accyn;
} while (accyn != 'y'); //end do/while loop
return accnum;
}
/************************************************/
/* Name: servcode */
/* Description: Get service type */
/* Parameters: N/A */
/* Return Value: servcode */
/************************************************/
char servcode()
{
char servcode = 'a'; //Define var for service code
char serviceyn = 'n'; //Define var for user verify
int i = 1; //Define var for sentinel loop
do {
cout << "\n" << "Please enter your service code, [R]egular or [P]remium:" << "\n";
cin >> servcode;
while ((servcode != 'R' && servcode != 'P' && servcode != 'r' && servcode != 'p' ) && (i < 3))
{
cout << "\n" << "Error - invalid service code, please try again. ";
cout << "\n" << "Please enter your service code: ";
cin >> servcode;
i++;
if (i == 3)
{
cout << "\n" << "Too many invalid attempts, program terminating." << "\n"
<< "Have a nice day. " << "\n" << "\n";
exit (0);
} //end if
} //end while
cout << "\n" << "You entered: " << servcode << "\n"
<< "Is that correct? [y,n]";
cin >> serviceyn;
} while (serviceyn != 'y'); // end do/while loop
return servcode;
}
/************************************************/
/* Name: regserv */
/* Description: Calculations for regular serv */
/* Parameters: N/A */
/* Return Value: regserv */
/************************************************/
float regserv ()
{
int regmin = 0; //Var for number of minutes used
float regserv = 0; //Var for cost for regular service
cout << "\n" "Please enter the minutes used: " << "\n";
cin >> regmin;
regserv = (regmin - 50) * 0.2 + 10;
return regserv;
}
/************************************************/
/* Name: premserv */
/* Description: Calculations for premium serv */
/* Parameters: N/A */
/* Return Value: premserv */
/************************************************/
float premserv (int& daymin, int& nightmin)
{
float daytotal = 0; //Var for day total
float nighttotal = 0; //Var for night total
float premserv = 0; //Var for premium service cost
cout << "\n" << "Please enter the number of minutes used durring the day (6AM - 6PM): " << "\n";
cin >> daymin;
cout << "\n" << "Please enter the number of minutes used durring the night (6PM - 6AM): " << "\n";
cin >> nightmin;
daytotal = (daymin - 75) * 0.1;
nighttotal = (nightmin - 100) * 0.05;
premserv = 25 + daytotal + nighttotal;
return premserv;
}
You call
regserv()orpremserv()inside the switch statement, but then you unconditionally call them both immediately after the switch.The comment on the switch suggests that you’ve misunderstood the semantics of switch. It doesn’t “select” what’s to going to happen next, it actually runs the code inside the switch, so you don’t need to invoke them after the switch. However, don’t forget to assign the
premiumservandregularservvariables and perform output inside the switch statement, e.g.: