Cell Phone Bill.
Oct 31, 2011 at 8:23pm UTC
Hey guys this is my first time posting so please bear with me.
So I have this assignment for my C++ class to write a program that calculates and prints my cell phone bill.
I have most of the code done but my issue is the input of the account number. My code is:
cout << "Please enter your 6-digit account number: ";
cin >> accountNum;
cout << endl;
if (accountNum > 100000)
{
cin >> accountNum;
cout << endl;
}
else
{
cout << "Invalid entry, please re-enter a 6-digit account number: ";
cin >> accountNum;
cout << endl;
}
How can I restrict the input to only 6 digits without any accounts starting with leading 0's such as "000265"?
p.s: how do I get the program to print output to a file? Thanks
Oct 31, 2011 at 9:02pm UTC
Why don't check if accountNum < 1000000
Oct 31, 2011 at 9:26pm UTC
Either "<" or ">" the program gets stuck. According to the instructions, I must keep the 6 digit account without it starting with a 0. The expression for the if statement could be wrong.
Oct 31, 2011 at 10:30pm UTC
you have two conditions and you can combine them with AND operator:
if (accountNum > 99999 && accountNum < 1000000)
All numbers satisfying these condition lie in [100000-999999], they all have 6 digits.
Oct 31, 2011 at 10:40pm UTC
I just changed it and the output gets stuck giving my input, this is my whole program. Where could I've gone wrong?
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const double rService = 10.00;
const double rPerMinute = 0.20;
const double pService = 25.00;
const double pPerMinuteDay = 0.10;
const double pPerMinuteNight = 0.05;
int main()
{
int regMinutes;
int dayMinutes;
int nightMinutes;
int accountNum;
char serviceType;
double amountDue;
cout << "Please enter your 6-digit account number: ";
cin >> accountNum;
cout << endl;
if (accountNum > 99999 && accountNum < 999999)
{
cin >> accountNum;
cout << endl;
}
else
{
cout << "Invalid entry, please re-enter a 6-digit account number: ";
cin >> accountNum;
cout << endl;
}
cout << "Enter a service code: R or r (Regular), P or p (Premium): ";
cin >> serviceType;
cout << endl;
switch (serviceType)
{
case 'r':
case 'R':
cout << "Enter the number of minutes used: ";
cin >> regMinutes;
cout << endl;
if (regMinutes > 50)
{
amountDue = rService + ((regMinutes - 50) * rPerMinute);
}
else
amountDue = rService;
break;
case 'p':
case 'P':
cout << "Enter the minutes used for the day: ";
cin >> dayMinutes;
cout << endl;
if (dayMinutes > 75)
amountDue = pService + ((dayMinutes - 75) * pPerMinuteDay);
else
amountDue = pService;
cout << "Enter the minutes used for the night: ";
cin >> nightMinutes;
cout << endl;
if (nightMinutes > 100)
amountDue = pService + ((nightMinutes - 100) * pPerMinuteNight);
else
amountDue = pService;
break;
}
cout << "Account Number: " << accountNum << endl;
cout << "Type of Service: " << serviceType << endl;
switch (serviceType)
{
case 'R':
case 'r':
cout << "Number of minutes used (if regular): " << regMinutes << endl;
break;
case 'P':
case 'p':
cout << "Number of minutes used during the day (if premium): " << dayMinutes << endl;
cout << "Number of minutes used during the night (if premium): " << nightMinutes << endl;
break;
}
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Amount Due = $ " << amountDue << endl;
return 0;
}
Oct 31, 2011 at 11:19pm UTC
I think that you are stuck here
1 2 3 4 5
if (accountNum > 99999 && accountNum < 999999)
{
cin >> accountNum;
cout << endl;
}
In the line
cin >> accountNum;
the program asks for a new phone number. Maybe you should write something like it:
1 2 3 4 5
if (accountNum > 99999 && accountNum < 999999)
{
cout << "You entered " << accountNum;
cout << endl;
}
Topic archived. No new replies allowed.