Cell Phone Bill

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 user input to only 6 digits without any accounts starting with leading 0's such as "000265", rejecting such entries prompting the user to re enter a valid account number?

p.s: how do I get the program to print output to a file? Thanks
Use a do-while loop that checks if the number the user entered is greater than 999999. Keep asking until they give you a number that is not
Last edited on
to add to prophets comment, or less than 100000. It'll be something along the lines of this:

do{

//code goes here

}while(accountNum >999999 || accountNum < 100000);
So now I have this as my whole code but now cout is ambiuous, how cant I resolve this?



#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;
*/
    do{
		cout << "Please enter your 6-digit account number: "; 
		cin >> accountNum;
	  }while (accountNum >999999 || accountNum < 100000);
/*	 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;
	
}



Also, how would I get this to print the output in a file?
I don't see why cout is giving you an undefined reference. It works fine when I compiled. Check your compiler.
For your print to file question use fstream.

#include <fstream> // Required header
//Then in your code define the output file to use.
std::ofstream fout("bill.txt");
// Then use it just as you would with cout. Same operators and stream manipulators will work. Including endl.

fout << "This will write to \"bill.txt.\"\n";
It worked now, thanks so much. After I went with the do-while loop the cout became ambiguous on visual studio, but after I restarted it, the program build and it ran flawlessly. Thanks again all!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
void GetData(int ages[], int capacity, int& n)

// Given: access to an array and its capacity
// Action: get input from keyaboard for each cell and put the final number of cells in n
{
	//	cout << "@" << __FUNCTION__ << "@" << endl;
	int i;
	for(i=0; cin && i<capacity; i++) {
		cout << "Enter an age or CTRL-Z when done: ";
		cin >> ages[i];
	} // for
	cin.clear();
	n=i-1;	
	cout << n << " records were read. " << endl;
	return;
} // GetData

void Initialize(int x[],int n,int initValue)

// Given: access to an array of n values
// Action: initialize each of the n cells in the array to 0
{
	//	cout << "@" << __FUNCTION__ << "@" << endl;
	int i;
	for(i=0; i<n; i++) {
		x[i]=initValue;
	} // for
	return;
} // Initialize

void Random(int x[], int low, int high, int n)

// Given: access to an array of n values
// Action: initialize each of the n cells in the array to a random value between low and high
{
	//	cout << "@" << __FUNCTION__ << "@" << endl;
	int i;
	for(i=0; i<n; i++) {
		x[i]=rand(low,high);
	} // for

	return;
} // Random

void Display(const int x[], int n)

// Given: access to an array of n values
// Action: display each cell with its index
{
	//	cout << "@" << __FUNCTION__ << "@" << endl;
	int i;
	for(i=0; i<n; i++) {
		cout << "Cell " << setw(3) << i << ":";
		cout << setw(5) << x[i] << endl;
	} // for

	return;
} // Display

int Sum(const int x[], int n)

// Given: access to an array of n values
// Return: the sum of the first n values of x
{
	cout << "@" << __FUNCTION__ << "@" << endl;
	int result=0;

	return result;
} // Sum

double Average(const int x[], int n)

// Given: access to an array of n values
// Return: the average of the first n values of x or 0.0 if n is 0
{
	cout << "@" << __FUNCTION__ << "@" << endl;
	double result=0.0;

	return result;
} // Average


int Min(const int x[], int n)

// Given: access to an array of n values
// Return: the smallest value of x
{
	cout << "@" << __FUNCTION__ << "@" << endl;
	int result=0;

	return result;
} // Min


int Max(const int x[], int n)

// Given: access to an array of n values
// Return: the largest value of x
{
	cout << "@" << __FUNCTION__ << "@" << endl;
	int result=0;

	return result;
} // Max


int IndexOfMin(const int x[], int n)

// Given: access to an array of n values
// Return: the index of the smallest value of x
{
	cout << "@" << __FUNCTION__ << "@" << endl;
	int result=0;

	return result;
} // IndexOfMin

int IndexOfMax(const int x[], int n)
// Written by: <your name>
// Given: access to an array of n values
// Return: the index of the largest value of x
{
	cout << "@" << __FUNCTION__ << "@" << endl;
	int result=0;

	return result;
} // IndexOfMax

int LinearSearch(int key, const int x[], int n)

// Given: access to an array of n values
// Return: the index of the cell that matches key
{
	cout << "@" << __FUNCTION__ << "@" << endl;
	int result=-1;

	return result;
} // LinearSearch

int BinarySearch(int key, const int x[], int first, int last)

// Given: 	a value to search for (the key)
//		the array to search (x)
//		the index of the first cell to consider
//		the index of the last cell to consider
// Returns:	the index of the cell whose value matches key
//		or -1 if there is no match 
{
	//	cout << “@” << __FUNCTION__ << “@” << endl;
	int result=-1;
	int middle=(first+last)/2;

	if(first>last) {
		result=-1;
	} else if(key<x[middle]) { // if key is in x it must be in first part
		result=BinarySearch(key,x,first,middle-1);
	} else if(key==x[middle]) { // found it! return its index!
		result=middle;
	} else { // key must be > x[middle] so it must be in the last part
		result=BinarySearch(key,x,middle+1,last);
	} // if
	return result;
} // BinarySearch

void Exchange(int& x, int& y)

// Given: access to two arguments, x and y
// Action: exchanges the contents of x and y 
// Example: if x is 5 and y is 3, Exchange(x,y) will make x 3 and y 5
{
	//	cout << “@” << __FUNCTION__ << “@” << endl;
	int temp=x;
	x=y;
	y=temp;
	return;
} // Exchange 

void BubbleSort(int key[], int n)

// Given access to the array and the number of values to sort
// Action: the values in the array are arranged in ascending order
{
	//	cout << “@” << __FUNCTION__ << “@” << endl;
	int i, pass;
	bool needsSorting=true;
	for(pass=1; pass<n; pass++) {
		for(i=0; i<n-1; i++) {
			if(key[i]>key[i+1]) {
				Exchange(key[i],key[i+1]);				
			} // if
		} // for
	} // for
	return;
} // BubbleSort

#endif  // last line 

Last edited on
Topic archived. No new replies allowed.