Click here to Skip to main content
15,905,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello and happy holidays to you all.

I am looking for some assistance with my C++ Code for a cellular bill program.

everything I have written so far works as I am wanting it to, except for 1 problem.

the issue that I am running into is that once an invalid entry is entered I receive the error prompt, but when the correct entry is entered it wont exit the loop and continue the program.

as a new student of C++, I am not seeing my error or the correct code. can somebody assist me with this please?

this is what I am getting while trying to run the program:

Welcome to Nates Wireless
Please enter your numeric account number: sdgf
Not an acceptable account number. Please try again.
123456
Not an acceptable account number. Please try again.


What I have tried:

here is my code:

C++
#include<iostream>
#include<iomanip>
#include<limits>

using namespace std;

int main()
{
	int accNum;
	int minutes, minutes2, minutes3;
	char serviceCode;
	double bill;

	const int INITIAL_REG = 10;
	const double REG_RATE = .2;
	const int REG_FREE = 50;
	const int INITIAL_PREM = 25;
	const double PREM_RATE1 = .1;
	const double PREM_RATE2 = .05;
	const int PREM_FREE1 = 75;
	const int PREM_FREE2 = 100;

	cout << "Welcome to Nates Wireless \n";
	cout << "Please enter your numeric account number: ";
	cin >> accNum;
	cin.ignore(numeric_limits<int>::max(), '\n');
	while ((!cin || cin.gcount() != 1))
	{
		cin.clear();
		cin.ignore(100, '\n');
		cout << "Not an acceptable account number. Please try again." << endl;
		cin >> accNum;
	}

	cout << "Please verify the type of service you have with us. \n";
	cout << "Enter \"r\" or \"R\" for Regular service or\n"; 
	cout << "\"p\" or \"P\" for Premium service: ";
	cin >> serviceCode;
	while (serviceCode != 'r' && serviceCode != 'R' && serviceCode != 'p' && serviceCode != 'P')
	{
		cin.clear();
		cin.ignore(100, '/n');
		cout << "Invalid service type. Please enter \"r\" or \"R\" for Regular or \"p\" or \"P\" for Premium." << endl;
		cin >> serviceCode;
	}

	cout << fixed << setprecision(2);

	switch (serviceCode)
	{
	case 'r':
	case 'R':
		cout << "Please enter the number of whole minutes you used: ";
		cin >> minutes;

		if (minutes == 0)
		{
			bill = INITIAL_REG;
			cout << "Regular account number " << accNum << " owes $" << bill << "." << endl;
			cout << "$" << INITIAL_REG << " initial service charge + $0 for 0 minutes used." << endl;
		}
		else if (minutes < REG_FREE && minutes > 0 || minutes == REG_FREE)
		{
			bill = INITIAL_REG;
			cout << "Regular account number " << accNum << " owes $" << bill << "." << endl;
			cout << "$" << INITIAL_REG << " initial service charge + $0 for a total of " << minutes
				<< " minutes of service." << endl;
		}
		else if (minutes > REG_FREE)
		{
			bill = INITIAL_REG + ((minutes - REG_FREE) * REG_RATE);
			cout << "Regular account number " << accNum << " owes $" << bill << "." << endl;
			cout << "$" << INITIAL_REG << " initial service charge + $" << ((minutes - REG_FREE) * REG_RATE)
				<< " for a total of " << minutes << " minutes of service." << endl;
		}
		else
			cout << "Can not use that value for minutes, please enter another value." << endl;
		break;
	case 'p':
	case 'P':
		cout << "Please enter the number of whole minutes you used between 6AM and 6PM: ";
		cin >> minutes2;
		while (minutes2 < 0)
		{
			cin.clear();
			cin.ignore(100, '\n');
			cout << "Invalid value for minutes. Please try again." << endl;
			cin >> minutes2;
		}

		cout << "Please enter the number of whole minutes you used between 6PM and 6AM: ";
		cin >> minutes3;
		while (minutes3 < 0)
		{
			cin.clear();
			cin.ignore(100, '\n');
			cout << "Invalid value for minutes. Please try again." << endl;
			cin >> minutes3;
		}

		if (minutes2 == 0 && minutes3 == 0)
		{
			bill = INITIAL_PREM;
			cout << "Premium account number " << accNum << " owes $" << bill << "." << endl;
			cout << "$" << INITIAL_PREM << " initial service charge + $0 for 0 minutes used." << endl;
		}
		else if (minutes2 == 0 && minutes3 > 0)
		{
			bill = INITIAL_PREM + ((minutes3 - PREM_FREE2) * PREM_RATE2);
			cout << "Premium account number " << accNum << " owes $" << bill << "." "\n";
			cout << "$" << INITIAL_PREM << " initial service charge + $" << ((minutes3 - PREM_FREE2) * PREM_RATE2)
				<< " for " << minutes3 << " minutes used between 6PM and 6AM." << endl;
		}
		else if (minutes3 == 0 && minutes2 > 0)
		{
			bill = INITIAL_PREM + ((minutes2 - PREM_FREE1) * PREM_RATE1);
			cout << "Premium account number " << accNum << " owes $" << bill << "." << endl;
			cout << "$" << INITIAL_PREM << " initial service charge + $" << ((minutes2 - PREM_FREE1) * PREM_RATE1)
				<< " for " << minutes2 << " minutes used between 6AM and 6PM. " << endl;
		}
		else if (minutes2 > 0 && minutes3 > 0)
		{
			bill = INITIAL_PREM + ((minutes2 - PREM_FREE1) * PREM_RATE1) + ((minutes3 - PREM_FREE2) * PREM_RATE2);
			cout << "Premium account number " << accNum << " owes $" << bill << "." << endl;
			cout << "$" << INITIAL_PREM << " initial service charge + $" << ((minutes2 - PREM_FREE1) * PREM_RATE1)
				<< " for " << minutes2 << " minutes used between 6AM and 6PM + $" << ((minutes3 - PREM_FREE2) * PREM_RATE2)
				<< " for " << minutes3 << " minutes used between 6PM and 6AM." << endl;
		}
		break;
	}

	return 0;
}
Posted
Updated 27-Dec-21 5:58am
Comments
jeron1 23-Dec-21 16:01pm    
while ((!cin || cin.gcount() != 1)) //what does this line do?
0x01AA 24-Dec-21 9:10am    
Lazy and far away from perfect, still somethin like '33aa' will be accpeted:


#include <string>
#include <sstream>
//....
string cinstr;
int accNum= 0;

cout << "Welcome to Nates Wireless \n";
cout << "Please enter your numeric account number: ";
getline (cin,cinstr);
stringstream(cinstr) >> accNum;
while (accNum == 0)
{
cout << "Not an acceptable account number. Please try again." << endl;
getline (cin,cinstr);
stringstream(cinstr) >> accNum;
}
cout << accNum;
//
nmeyer1 27-Dec-21 9:31am    
not really sure what that comment has to do with the assistance I was asking for, but thanks any way. I got it figured out.
0x01AA 27-Dec-21 10:12am    
A more or less proper accNum input method, I think/thought :-)

Instead of:
C++
cin >> accNum;
cin.ignore(numeric_limits<int>::max(), '\n');
while ((!cin || cin.gcount() != 1)) {
   cin.clear();
   cin.ignore(100, '\n');
   cout << "Not an acceptable account number. Please try again." << endl;
   cin >> accNum;
}

could you just write:
C++
while (!(cin >> accNum)) {
  cin.clear();
  cin.ignore(numeric_limits<streamsize>::max(), '\n');
  cout << "Not an acceptable account number. Please try again." << endl;
}

Not only would that be much shorter, it would also work as desired.
 
Share this answer
 
Comments
nmeyer1 27-Dec-21 13:00pm    
thank you for the assistance and showing me how to make it more simple. I appreciate it.
While combining the results of logical operations we can make use of brackets in an appropriate manner.
C++
while ((!cin )||(cin.gcount() != 1))
	{
		cin.clear();
		cin.ignore(100, '\n');
		cout << "Not an acceptable account number. Please try again." << endl;
		cin >> accNum;
	}


Output:
Welcome to Nates Wireless 
Please enter your numeric account number: 1234
Please verify the type of service you have with us. 
Enter "r" or "R" for Regular service or
"p" or "P" for Premium service: ^C
 
Share this answer
 
v2
Comments
nmeyer1 27-Dec-21 9:35am    
thank you for pointing that out as that was one of the problems, and thank you for the assistance as I got it figured out and corrected.
thank you for the assistance, as i got it figured out.

this
while ((!cin || cin.gcount() != 1))


needed to be changed to this
while ((!cin) || (cin.fail()))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900