# C++ Problem, seeking assistance

This topic is 2632 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.

##### Share on other sites
#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 > 800 && time < 1800 && day == weekday)    {    rate = .20;    }else if (time > 1800 && time < 2300 && day == weekday)    {    rate = .15;    }else if (time > 2300 && time < 800 && day == weekend)    {    rate = .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$  ";return(0);}

Ok after looking at it a bit, I think that everything except the "Do you have another y/n?" and the "Total cost $" part are right. Now for the y/n part, how would I word that in order to get back into the loop? Would I have to move it inside of the loop or is it fine there? And for total cost would I have to assign its own int or what operation would I need to use? #### Share this post ##### Link to post ##### Share on other sites I have to say, all points made so far are good advice. I'm baffled a professor would let you get to the point of doing this assignment without even understanding how variables and if statements work. You did not include the string library, by the way. He says to do so in the instructions. A better way to do this would be to check whether the input was either "weekday" or "weekend", and then use an embedded if statement to determine what time period the call took place in. You would reduce the number of if statements this way. As in: if (day == "weekday") {if (time >= 800 && time < 1800) {rate = 20;}else if (time >= 1800 && time < 2300) {rate = 15;}else {rate = 10;}}else {rate = 10;} Remember to divide the output number by 100 as it will be in cents the way it is! You need to use whole numbers with int, you can't use decimals. Also, change your variables to this: string day;int time;int length;int rate; You do not need the other two strings, and you do not need to initialize 'day' with a value as it is assigned one by the user. One more thing! :D If you are going to keep track of the total cost, you will also need int cost; Again, when outputting, divide by 100, as such cout << day << " " << length << " minute call started at " << time << " hrs will cost$" << cost/100 << endl;

Although now that I think about it, I'm not sure it will properly divide your number by 100 unless it is a float datatype. I haven't programmed in a while.

##### Share on other sites
So after the If statements all I would have to do is rate/100 and that would give me my decimal, correct?

As for my professor, I have to say I was taught Java better than this (granted its been a while, so I forget a lot of what I learned). But all she does is talk in lecture, so we don't see how the code should be applied. The only time I really get to learn anything is in our once a week lab. Given the instruction in the class, I'm lucky to understand this much.

##### Share on other sites
See my last addition to my post. You may need to experiment a little bit to see if cost/100 will output correctly. If it doesn't, and drops the decimal, then simply change the datatype from 'int' to 'float' for both rate and cost, and change it in the if statements where it says
rate = 20;

etc. to say

rate = .20;

You would no longer need to divide by 100 if you did this.

Edit:
Also, when outputting, if it drops the final 0 (ie. '$2.3' instead of '$2.30') simply add << "0" to your output. Worst case scenario, it says a call costs '$.150' :) #### Share this post ##### Link to post ##### Share on other sites Thats partially the problem. For some reason when I use the ./a.out or the g++ command in my terminal it tells me that the command is invalid or doesn't exist. So I'm not able to check the output, otherwise this would be a lot easier for me. #### Share this post ##### Link to post ##### Share on other sites On second thought, reguarding it dropping the final 0: Change the datatype from int to float. Now, in your output, do this: cout << day << " " << length << " minute call started at " << time << " hrs will cost$" << setiosflags(ios::fixed) << setprecision(2) << cost << endl;

What this does, is sets the number of decimal places you want to show to.

Finally:
Are you using Linux or Windows? You may have an easier time with this if you use and compile using an IDE such as VC++ or Code::Blocks or Dev-C++

##### Share on other sites
I'm using a mac. I'm using the same type of terminal as I use in the labs though, so that doesn't make much sense, considering that terminal is unix based. I figured they all acted the same.

Also wouldn't this work?
cout.setf(ios::fixed);cout << setprecision(2);

##### Share on other sites
http://www.edparrish.com/common/macgpp.php

This provides a quick tutorial in installing G++ in a Mac environment and then writing, saving, and compiling code. Tell me if you get it working please, thank you (:

##### Share on other sites
Um, its gonna take an hour to download, and then I have to set it up... Ah, not the best situation. Well anyways is there any other things that I could do to fix this?

##### Share on other sites
You can't compile code if you don't have a compiler installed. A terminal is just an interface to the computer, you can't expect it to be the same everywhere (indeed, it will likely be different on each one).

You could go to the lab.

##### Share on other sites
I have to in order to turn the damned thing in, but I want to make sure its right before I go to the lab. The computers there don't have the best internet (sometimes no internet) so if I run into a problem I can't ask a lab instructor or you guys. So overall does everything look good?
#include <iostream>using namespace stdint main(){string day;int time;int length;int rate;string weekday = "weekday";string weekend = "weekend";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;cout.setf(ios::fixed);cout << setprecision(2);if (time > 800 && time < 1800 && day == weekday)    {    rate = 20;    }else if (time > 1800 && time < 2300 && day == weekday)    {    rate = 15;    }else if (time > 2300 && time < 800 && day == weekend)    {    rate = 10;    }int cost;int cost = (rate/100)*length;cout << day << length << "minute call started at " << time << "hrs will cost $" << cost << endl;//Do you have another y/n?//cout << "Total cost$  ";return(0);}

Also how would I go about doing the y/n and total cost part? Those were the only two parts I had no idea about.

##### Share on other sites
You could use codepad.org to check your logic. Just replace the I/O with fixed statements for the moment. So instead of "cin >> time" use "time = 1234".

You could structure your conditional logic better:
if(day == weekday){    if(time > 800 && time < 1800)    {        rate = 20;    }    else // off peak    {        rate = 15;    }}else // weekend{    rate = 10;}

This is not only shorter, but it is much clearer to a human what is going on. Your conditions aren't strict enough, consider what might happen in your latest code if time = 800.

##### Share on other sites
Thanks for the help, but I still need help with the y/n part and the total cost part.

##### Share on other sites
So for the y/n part, I need to know how to get it to go back into the loop if you type 'y' and how to total up the cost afterwards. Apart from that everything is running smoothly and doing what its supposed to.

##### Share on other sites
Quote:
 Original post by Out-TakeSo for the y/n part, I need to know how to get it to go back into the loop if you type 'y' and how to total up the cost afterwards. Apart from that everything is running smoothly and doing what its supposed to.
You need a loop. I recommend a while loop, and a string to hold the user's response. Wrap the entire program in that while loop. At the end of the loop ask the user whether they want to continue.

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628710
• Total Posts
2984324

• 23
• 11
• 9
• 13
• 14