C++ Problem, seeking assistance

Started by
28 comments, last by iMalc 13 years, 6 months ago
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.
Advertisement
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.
Thanks for the help, but I still need help with the y/n part and the total cost part.
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.
Quote:Original post by Out-Take
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.
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.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

I tried doing it like this
while (ans == 'y');{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;    }double cost = rate*length;cout << day << " " << length << " minute call started at " << time << "hrs will cost $" << cost << endl;cout << "Do you have another? y/n";cin >> ans;}

but when I type 'y' and hit enter, it ends the loop. I could also use some help with the decimal places, cause none of the things the other guys said seemed to have worked.
You're going to need the compiler for future assignments anyway.

Also, you seem to have ignored numerous posts I've made regarding your flawed if...else statements.

And again, you still need to include the string library. Until you fix all of the problems you have you should not continue work on another part of the code.
but everything compiles and I get the output I need up until that y/n part. So as much as I appreciate the help, my code seems to be working well for me...up until that damned y/n part.
You need to change the int cost and int rate to float cost and float rate.

else if (time > 2300 && time < 800 && day == weekend)

This will not do what you want it to. This will only set the rate to .10 if it is between 2300 and 800 on the weekend. It should be
else if ((time > 2300 && time < 800) || day == weekend)


Also, you are reinitializing your cost variable at the end of each loop... This means you will not be able to display the total cost before program exit. You need to add each new call to the total cost.

Finally, did you initialize 'ans' as
char ans = 'y';

?

Edit: Also, I'm fairly certain that 'setprecision()' needs to go immediately before the number you are making precise. Could be wrong, but I have never seen it the way you have it.
Say time equals 1800 exactly and day equals weekday. What happens?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement