Seriously Stuck On Classwork

I have to write a program for my class that uses user set functions, here is the code I have:

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
#include <iostream>
using namespace std;
double regularBill(double min);
double premiumBill(double dayMin, double nightMin);
int main()
{
	double min, dayMin, nightMin;
	char service, don;
	cout<<"Do you want the regular service or the premium service?"<<endl;
	cin>>service;
	if(service='R'||'r')
	{
		cout<<"Enter the amount of minutes you need."<<endl;
		cin>>min;
		cout<<"The cost of "<<min<<" minutes is $"<<regularBill<<"."<<endl;
	}
	else if(service='P'||'p')
	{
		cout<<"Do you want minutes for day or night?"<<endl;
		cin>>don;
		if(don='D'||'d')
		{
			cout<<"Enter the amount of minutes you need."<<endl;
			cin>>dayMin;
			cout<<"The cost of "<<dayMin<<" minutes is $"<<premiumBill<<"."<<endl;
		}
		else if(don='N'||'n')
		{
			cout<<"Enter the amount of minutes you need."<<endl;
			cin>>nightMin;
			cout<<"The cost of "<<nightMin<<" minutes is $"<<premiumBill<<"."<<endl;
		}
	}
	return 0;
}
double regularBill(double min)
{
	double regularBill=10.00+(0.20*(min>50));
	return regularBill;
}
double premiumBill(double dayMin, double nightMin)
{
	double premiumBill=25.00+(0.12*(dayMin>75))||25.00+(0.05*(nightMin>100));
	return premiumBill;
}


The program is supposed to print the total price of a phone plan, but I cannot get the output to work. This was supposed to be turned in today, and I currently have a zero on it, as I have not turned it in yet. Any help would be greatly appreciated.
Your conditions inside your 'if' statements don't convey what you think they do.
if(service='R'||'r') should be if(service == 'R'|| service == 'r')
This applies to all your conditions. Note the distinction between the '=' and '==' operators. Also remember that you can't chain together logical comparisons like you did; you must specify both sides of the comparison every time.

Next, your functions expect parameters. When you call these functions currently, you do so like this: cout<<"The cost of "<<dayMin<<" minutes is $"<<premiumBill<<"."<<endl;
However, the call should look more like this:
 
cout << "The cost of " << dayMin << " minutes is $" << premiumBill(dayMin, nightMin) << "." << endl;
Notice that the call to premiumBill now has parameters being passed to it. You'll have to figure out logically if it makes sense to pass both dayMin and nightMin every time you invoke the premium function, but the way it is declared now you must pass it two parameters.
Last edited on
booradley60 wrote:

Your conditions inside your 'if' statements don't convey what you think they do.
if(service='R'||'r') should be if(service == 'R'|| service == 'r')

This applies to all your conditions. Note the distinction between the '=' and '==' operators. Also remember that you can't chain together logical comparisons like you did; you must specify both sides of the comparison every time.

Next, your functions expect parameters. When you call these functions currently, you do so like this: cout<<"The cost of "<<dayMin<<" minutes is $"<<premiumBill<<"."<<endl;
However, the call should look more like this:
cout << "The cost of " << dayMin << " minutes is $" << premiumBill(dayMin, nightMin) << "." << endl;
Notice that the call to premiumBill now has parameters being passed to it. You'll have to figure out logically if it makes sense to pass both dayMin and nightMin every time you invoke the premium function, but the way it is declared now you must pass it two parameters.


Thanks, I think I fixed everything you mentioned. But I still cannot get the output I need. Here is the current output:

output wrote:
----jGRASP exec: G:\Computer Programming\C++\a.exe

Do you want the regular service or the premium service?
r
Enter the amount of minutes you need.
45
The cost of 45 minutes is $1.

----jGRASP: operation complete.

----jGRASP exec: G:\Computer Programming\C++\a.exe

Do you want the regular service or the premium service?
p
Enter the amount of minutes you need.
60
The cost of 60 minutes is $1.

----jGRASP: operation complete.
line 38 is making me sceptical. Look at this : (min>50) that is a logic operation not arithmetic
same counts for line 43. I get the feeling that it is misplaced.

I played a little bit with your code. I tested the regular option (r). I get only two possible results:
when minutes are <= 50 it always outputs $10
when minutes are >=50.1 it always outputs $10.2

( before i run your code i did correct some obvious mistakes as TheUnholy says above)
Last edited on
mynicks wrote:

line 38 is making me sceptical. Look at this : (min>50) that is a logic operation not arithmetic
same counts for line 43. I get the feeling that it is misplaced.

I played a little bit with your code. I tested the regular option (r). I get only two possible results:
when minutes are <= 50 it always outputs $10
when minutes are >=50.1 it always outputs $10.2

( before i run your code i did correct some obvious mistakes as TheUnholy says above)


That is the output I am supposed to get with the regular option, but I just keep getting $1. With the premium option, I can't get the right output at all.
Do you have a copy of the assignment description that you can share with us?
booradley60 wrote:

I have a paper about it, but it just has the code that the teacher gave the students. It says to refer to 2 problems in different chapters of the textbook. I would go into the book to find the problems, but we do not have textbooks at home for our programming class.
Last edited on
I was trying to guess what your assignment may be. It seems like if you select the regular option you would pay a $10 base rate, and then 20 cents for every minute beyond 50.

I figured the premium plan offered different rates and made the distinction between day and night minutes.
booradley60 wrote:

I was trying to guess what your assignment may be. It seems like if you select the regular option you would pay a $10 base rate, and then 20 cents for every minute beyond 50.

I figured the premium plan offered different rates and made the distinction between day and night minutes.


You are right about the regular option. The premium plan is a base rate of $25 and 12 cents for every minute above 75 (for day), and 5 cents for every minute above 100 (for night).
Ok, so for the regular option, you invoke the regularBill function.
In main:
1
2
3
4
5
6
    if(service =='R' || service == 'r')
    {
        cout << "Enter the amount of minutes you need." << endl;
        cin >> min;
        cout << "The cost of " << min << " minutes is $" << regularBill(min) << "." << endl;
    }

The regularBill function (Notice I've changed the parameter to an int. I don't think you are worried about fractions of a minute, so an int will be fine.):
1
2
3
4
5
6
7
8
9
10
double regularBill(int min)
{
    double baseRate = 10.00;
    double totalCharge = baseRate;
    if (min > 50)
    {
        totalCharge = totalCharge + (0.2 * (min - 50));
    }
    return totalCharge;
}


Then, for premium bills, I don't think the user should decide between day or night. They should be asked for both no matter what.
Last edited on
booradley60 wrote:

Ok, so for the regular option, you invoke the regularBill function.
In main:
1
2
3
4
5
6
    if(service =='R' || service == 'r')
    {
        cout << "Enter the amount of minutes you need." << endl;
        cin >> min;
        cout << "The cost of " << min << " minutes is $" << regularBill(min) << "." << endl;
    }


The regularBill function (Notice I've changed the parameter to an int. I don't think you are worried about fractions of a minute, so an int will be fine.):
1
2
3
4
5
6
7
8
9
10
double regularBill(int min)
{
    double baseRate = 10.00;
    double totalCharge = baseRate;
    if (min > 50)
    {
        totalCharge = totalCharge + (0.2 * (min - 50));
    }
    return totalCharge;
}


Then, for premium bills, I don't think the user should decide between day or night. They should be asked for both no matter what.


I got the regular option working fine. But I still cannot figure out how to do the premium bill. Should I have two functions for the premium? One for day and one for night?
Here's how I interpreted premium. Premium has a different base rate, and it also charges differently for nights compared to days. But any individual customer likely uses it during both days and nights.
In main:
1
2
3
4
5
6
7
8
9
else if(service == 'P' || service == 'p')
    {
        cout << "Enter the amount of daytime minutes." << endl;
        cin >> dayMin;
        cout << "Enter the amount of nighttime minutes." << endl;
        cin >> nightMin;
        cout << "The cost for " << dayMin << " day minutes and " << nightMin << " night minutes is $";
        cout << premiumBill(dayMin, nightMin) << endl;
    }


Then, in the function itself, you do the additional rate calculations for both daytime overage charges and nighttime overage charges.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double premiumBill(int dayMin, int nightMin)
{
    double baseRate = 25.0;
    double totalCharge = baseRate;

    if (dayMin > 75)
    {
        totalCharge = totalCharge + (0.12 * (dayMin - 75));
    }

    if (nightMin > 100)
    {
        totalCharge = totalCharge + (0.05 * (nightMin - 100));
    }

    return totalCharge;
}
Here is my current code:

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
#include <iostream>
using namespace std;
double regularBill(int min);
double premiumBill(int dayMin, int nightMin);
int main()
{
	double min, dayMin, nightMin;
	char service, don;
	cout<<"Do you want the regular service or the premium service?"<<endl;
	cin>>service;
	if(service=='R'||'r')
	{
		cout<<"Enter the amount of minutes you need."<<endl;
		cin>>min;
		cout<<"The cost of "<<min<<" minutes is $"<<regularBill(min)<<"."<<endl;
	}
	else if(service=='P'||'p')
	{
		cout<<"Enter the amount of minutes you need for daytime."<<endl;
		cin>>dayMin;
		cout<<"Enter the amount of minutes you need for nighttime."<<endl;
		cin>>nightMin;
		cout<<"The cost of "<<dayMin<<" daytime minutes is $"<<premiumBill(dayMin, nightMin)<<"."<<endl;
		cout<<"The cost of "<<nightMin<<" nighttime minutes is #"<<premiumBill(dayMin, nightMin)<<"."<<endl;
	}
	return 0;
}
double regularBill(int min)
{
	double baseRate= 10.00;
	double totalCharge=baseRate;
	if(min>50)
	{
		totalCharge=totalCharge+(0.2*(min-50));
	}
	return totalCharge;
}
double premiumBill(int dayMin, int nightMin)
{
	double baseRate=25.00;
	double totalCharge=baseRate;
	if(dayMin>75)
	{
		totalCharge=totalCharge+(0.12*(dayMin-75));
	}
	if(nightMin)
	{
		totalCharge=totalCharge+(0.05*(nightMin-100));
	}
	return totalCharge;
}


The premium bill is not outputting what it should. When I run it and enter p, it just runs the code for the regular bill. I do not see what I am doing wrong.
Last edited on
Be careful, you corrected one part of your 'if' but not the other. Note the difference between this
if(service=='R'||'r') and this if(service=='R'|| service == 'r') This applies to your 'else if' case as well.

What's happening here? if(service=='R'||'r') The OR operator [||], returns true if either of its arguments are true. In this case, the arguments are:
1. service == 'R'
2. 'r'

Let's say we entered 'p' for service. What happens?
1. service == 'R' is FALSE. service is 'p', not 'R'.
2. 'r' is TRUE! Why? 'r' is a character point with some non-zero ASCII code. Any number when coerced to a true/false value becomes true. The only exception is the number 0. The number 0 when interpreted as a true/false value yields false.

So, given the evaluation of those conditions, if(service=='R'||'r') is the same as if(false || true). We know from how the || operator works that this evaluates to if(true), which means we're going to execute the code inside of the if block.
Last edited on
booradley60 wrote:
Be careful, you corrected one part of your 'if' but not the other. Note the difference between this if(service=='R'||'r') and this if(service=='R'|| service == 'r') This applies to your 'else if' case as well.

What's happening here? if(service=='R'||'r') The OR operator [||], returns true if either of its arguments are true. In this case, the arguments are:
1. service == 'R'
2. 'r'

Let's say we entered 'p' for service. What happens?
1. service == 'R' is FALSE. service is 'p', not 'R'.
2. 'r' is TRUE! Why? 'r' is a character point with some non-zero ASCII code. Any number when coerced to a true/false value becomes true. The only exception is the number 0. The number 0 when interpreted as a true/false value yields false.

So, given the evaluation of those conditions, if(service=='R'||'r') is the same as if(false || true). We know from how the || operator works that this evaluates to if(true), which means we're going to execute the code inside of the if block.


Thank you, I got the program working perfectly now. I feel really stupid now, I should have remembered that from just a couple weeks ago lol. Thanks.
Topic archived. No new replies allowed.