C++ Problem, seeking assistance

Started by
28 comments, last by iMalc 13 years, 6 months ago
#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?
Advertisement
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.
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.
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' :)
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.
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++
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);
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 (:
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?
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.

This topic is closed to new replies.

Advertisement