Public Group

# C++ Problem, seeking assistance

This topic is 2848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a C++ assignment in which I need to compute a phone bill, basically. The Assignment is as follows:

Quote:
 Write a program that computes the cost of a long-distance call and the total cost of all the calls made. The cost of the call is determined according to the following rate schedule:Any call started between 8:00 a.m. (0800 hours) and 6:00 p.m. (1800 hours)during the weekdays is billed at the rate of $0.20 per minute.Any call started after 6:00 p.m. (1800 hours) and 11:00 p.m. (2300 hours)during the weekdays is billed at the rate of$ 0.15 per minute.Any call made during the weekend (Saturday and Sunday) and after 11:00 p.m. and before 8:00 a.m. every day is billed at the rate of $0.10 per minute.The input will consist of the day (weekday or weekend), the time call started, and the length of the call in minutes. The time is to be input in 24-hour notation. The day of the week will be read as a string. The time and the length will be integer values. You do not have to worry about a call going from one rate to another. For example if the call started at 745 and the length of the call is 30 minutes, your entire call will be calculated at the rate of the time call started. Declare all the rates as named constants in your program.Use loops and if statements in your program. Do not forget to include the string library. Create your own test data to test all the if statements.Sample output of the programEnter the day weekday or weekend weekdayEnter the time the call started 900Enter the length of the call 20weekday 20 minute call started at 900 hrs will cost$ 4.00Do you have another y/n yEnter the day weekday or weekend weekendEnter the time the call started 100Enter the length of the call 20weekend 20 minute call started at 100 hrs will cost $2.00Do you have another y/n nTotal cost$

Thats a sloppy copy and paste, but thats everything you need to know. Now I've done a majority of the work and I just have a few questions about it. For one, my terminal won't compile (not because its wrong but because its telling me the command doesn't exist). So I need to know what I did wrong, how I can fix it, and any other advice. Thank you in advance. The code is as follows:

#include <iostream>using namespace stdint main(){string weekday;string weekend;string day = weekday || weekend;int time;int length;cout << "Enter the day weekday or weekend \n";cin >> day;cout << "Enter the time the call started \n";cin >> time;cout << "Enter the length of the call \n";cin >> length;int rate;if (time > 0800 > 1800; day = weekday;)    {    rate = 0.20;    }if else (time > 1800 > 2300; day = weekday;)    {    rate = 0.15;    }if else (time > 2300 > 0800; day = weekend;)    {    rate = 0.10;    }int cost = rate*length;cout << day; << length; << "minute call started at "; << time; << "hrs will cost $"; << cost; << endl;//Do you have another y/n?//cout << "Total cost$  ";}

Mod Edit by Evil Steve: Changed code tag to a quote to fix forum formatting.

##### Share on other sites
"if else" should be "else if", also you should not terminate expressions inside if's braces with a semicolon.

EDIT: after more carefully looking into your code you seem to know very little about c syntax and Boolean algebra. you may want to review these.
more specifically it makes no sense to seperate expressions inside an if statement as they need to evaulate to true or false, you want to use binary operators instead such as ANDs and ORs. also there is a major difference between = and == in c/c++, = is the assignment operator and == is the equality operator.
day = weekend assigns weekend to day, day == weekend returns true for equality and false for inequality.

##### Share on other sites
if else (time > 1800 > 2300; day = weekday;)

It should be
 else if (time > 1800 > 2300, day = weekday)

Right?

##### Share on other sites
You're missing a return statement from main as well. Plus you shouldn't put semicolons in-between the stream operators:

cout << day << length << "minute call started at " << time << "hrs will cost $" << cost << endl; #### Share this post ##### Link to post ##### Share on other sites Ok yeah I knew I forgot the return. Anything else that could use some tweaking? Can anyone check if (after the mentioned tweaks) it compiles and what the return output is? #### Share this post ##### Link to post ##### Share on other sites Thats the least of your problems, you did not initialize the string objects so your if statements make no sense, you seperate your expressions inside if statements which shows a lack of understanding of logic operations and boolean algebra and more. your program will not compile as the syntax is all wrong and even if it does compile it wont work the way you expect it to. #### Share this post ##### Link to post ##### Share on other sites Just after a quick glance at your code it appears there are a couple of things I would point out to you. 1) I am not sure what you are doing with the variables weekday or weekend. It appears that you assign day a bool value by using day = weekend || weekday, which unless I just have never used this type of syntax, doesn't seem correct, especially considering that you assign day an input value right after this assignment so the information that was put in day is removed anyway. FYI weekday and weekend are initially null string so basically your saying NULL or NULL is equal to day, which if I remember right is the same as false || false or 0 || 0. I have no idea what that would actually place in day or if this is where the command error is coming from but either way this isn't the correct way to do what you are trying to do. 2) Your boolean statements are syntactically wrong as well. For instance I am assuming you want your time to be between 0800 and 1800 as well as the day of the week to be a weekday. So a fixed version of that line would be as follows: if((time > 800 || time < 1800) && (day == "Monday" || day == "Tuesday" || day == "Wedsnday" || day == "Thursday" || day == "Friday")){ rate = .20;}else if(...){} 3) Your cout statements at the bottom are broken up by the semicolon. You need to just have a semicolon at the end of the cout statement. e.g. as follows: cout << day << length << "minute call started at " << time << "hrs will cost$" << cost << endl;

Hope that helps and it should clear some things up for you.

EDIT: Thanks evil steve for pointing out my awesome || mistake whoops :)

[Edited by - toogreat4u on September 30, 2010 8:15:17 PM]

##### Share on other sites
Thats very helpful, but I think that my professor is just going to input "weekday" or "weekend" and not the actual days of the week, so how would I be able to rightfully distinguish between weekday and weekend in my loop?

##### Share on other sites
0. Nitpick: This is C++, not C.

1. weekday and weekend are just variable names, whereas you want a string containing the value "weekend" or "weekday", I.e. string weekday = "weekday";

if (time > 0800 > 1800; day = weekday;)
You probably want:
if(time > 800 && time < 1800 && day == weekday)
2a. You shouldn't have leading 0's on a number (That tells the compiler that the number is in base 8, not base 10 (Which you probably don't want))
2b. You need to use the && operator to say "and", you can't chain together operator >
2c. You want == to compare, not = (Which means "assign to")

##### Share on other sites
OK, well let me fix up the code a bit, repost it, and see what else needs to be done.

1. 1
Rutin
25
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 9
• 46
• 41
• 23
• ### Forum Statistics

• Total Topics
631749
• Total Posts
3002051
×