So I was asked to do a programs that would calculate the amount of money it would cost to make a long distance call. We were given 3 scenarios. (You can tell from the program). Another thing that was asked it to use pairs of char values, which are stored in two variables of type char: Mo, Tu, We, Th, Fr, Sa, Su. This is the closest I got to making the program run, but it keeps on doing an infinite loop. Honestly, I don't know what to do. Anything will help.
#include <iostream>
#include <cmath>
using namespace std;
Another note I would ask the day of the week first, then ask for the times. The reason is I can verify the date before I take any times. The following code would be the way you do the mess you tried to do in that funky if.
// this is using my replacement stuff to your code...
bool found = false;
bool weekend = false;
while(!found)
{
cout << "Enter day you made phone call: \n";
cin >> day_of_week;
for(int Index = 0; Index < 7; Index++)
{
if(days_of_week[Index] == day_of_week)
{
found = true;
if(Index > 4)
{
weekend = true;
}
}
}
}
// when I reach here I should have had a valid day of the week.
// now I can enter my times.
I hope this helps a little bit to your programs problems.
this is incorrect:
while (response == 'y'|| 'Y');
should be:
while ( (response == 'y') || (response == 'Y'));
My last edit was made to make it match the original problem.
Firstly,use [code]Code here[/code] tags.
Now, looking at your code: firstchar = 'M','T','W','F','S';
This is doing the Following--
First assigning the value S to firstchar, then F and so on until it becomes M;
You should either create an array of chars for Each day like: char Mond[] = "Monday";
or even better use std::string
Similarly for comparison, you should do it like this: if (day_of_week == "Monday" || day_of_week == "monday"/*And so on*/) //for std::strings ONLY
Also think if I typed mO for day_of_week, your program will give that the input was wrong (As per your intention, not the program's).
You can use a loop an the cctype Library to set them to the same specified case.
If you plan to use C-Strings (char arrays) then you must include <string.h>
or <cstring>
and compare as follows: if (strcmp(day_of_week, "Monday") == 0 || strcmp (/*So on*/)/*...*/)
As to your main question:
Why is this an infinite Loop: while (response == 'y'|| 'Y');
will be evaluated like this:
Is response equal to 'y'?
whatever it is (Say false): false||'Y'
The ASCII code of 'Y' is 89. As all non-zero values are TRUE, this means, this is the same as false||true
This will always result in true