Code doing infinite loops. Please help!!

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;

int main ()

{
int length_of_call;
double time_call_started, cost_of_call;
char day_of_week, firstchar, secondchar;
char response;

firstchar = 'M','T','W','F','S';
secondchar = 'o','u','e','h','r','a';
day_of_week = (firstchar + secondchar);
cout << "This program computes the cost of a long-distance call!" <<endl;


do
{
cout << "Enter time you started phone call: \n" ;
cin >> time_call_started;

cout << "Enter day you made phone call: \n";
cin >> day_of_week;

cout << "Enter length of your phone call in minutes: \n";
cin >> length_of_call;

if ( ( time_call_started >= 8 ) && ( time_call_started <= 18 ) )
{
if ( day_of_week == 'M', 'o', 'T', 'u', 'W', 'e', 'h', 'F', 'r')
{
cout << "Your bill concludes to $ " << ( length_of_call * .40 ) <<endl;
}
}

if ( ( time_call_started < 8) || ( time_call_started > 18 ))
{
if ( day_of_week == 'M', 'o', 'T', 'u', 'W', 'e', 'h', 'F', 'r' )
{
cout << "Your bill concludes to $" << ( length_of_call * 0.25 ) <<endl;
}
}
if ( day_of_week == 'S', 'a','u')
{
cout << " Your bill concludes to $" << ( length_of_call * .15 ) << endl;
}
cout << "Would you like to start again?\n";
cin >> response;
}
while (response == 'y'|| 'Y');

return 0;
}
basically what I see in your code is like this, is some incorrect approaches to what you are trying to accomplish.

This mess:
1
2
3
4
5
char day_of_week, firstchar, secondchar;
char response;

firstchar = 'M','T','W','F','S';
secondchar = 'o','u','e','h','r','a';


should be:
1
2
3
4

char day_of_week[2];

char days_of_week[7] = {"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"};


This didn't make any since to me and I had never seen the construct in c++
 
if ( day_of_week == 'M', 'o', 'T', 'u', 'W', 'e', 'h', 'F', 'r' )


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
       // 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.
Last edited on
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

Reference:
http://www.cplusplus.com/doc/tutorial/variables/
[Scroll Down]
http://www.cplusplus.com/reference/string/
http://www.cplusplus.com/reference/clibrary/cctype/
http://www.cplusplus.com/reference/clibrary/cstring/ http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
Last edited on
Topic archived. No new replies allowed.