Function logic error

Hello everyone,

Do you mind checking my whole coding below, the program is purposely created to compute the cost of the call when certain call is made during certain period on certain day. The function void EnterTime does not run well. I was trying to use char hour[3] and char mins[3] to read two numbers as hour (HH) and minutes (MM) and convert them to integer. I can't enter time. No syntax error, but seems like logic error might be the reason.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <cstdlib>
#include <sstream>

using namespace std;

void EnterDay (char& day, char& day2)
{
    do
    {
        cout << "Enter day : ";
        cin >> day >> day2;

        day = toupper(day);
        day2 = toupper(day2);

        if ((day != 'M' || day2 != 'O') && (day != 'T' || day2 != 'U') &&
            (day != 'W' || day2 != 'E') && (day != 'T' || day2 != 'H') &&
            (day != 'F' || day2 != 'R') && (day != 'S' || day2 != 'A') &&
            (day != 'S' || day2 != 'U'))

        cout << "Wrong input!\nYou may try again\n" << endl;
    }while((day != 'M' || day2 != 'O') && (day != 'T' || day2 != 'U') &&
            (day != 'W' || day2 != 'E') && (day != 'T' || day2 != 'H') &&
            (day != 'F' || day2 != 'R') && (day != 'S' || day2 != 'A') &&
            (day != 'S' || day2 != 'U'));
}


void TestCost(int& duration, int& num, double& call_cost, char& day, char& day2)
{
    double billrate1 = 0.40, billrate2 = 0.25, billrate3 = 0.15;

    do
    {
    cout << "\nEnter num : ";
    cin >> num;
    cout << "\nEnter duration (minutes) : ";
    cin >> duration;

    if ((day != 'S' && day2 != 'A') &&
        (day != 'S' && day2 != 'U') && ((num > 0) && (num < 50)) &&
               (duration > 0)){

        call_cost = duration * billrate1;
        cout.precision(2);
        cout.setf(ios::fixed);
        cout << "The cost of the call : $" << call_cost;
        }
     else if ((day != 'S' && day2 != 'A') &&
              (day != 'S' && day2 != 'U') && ((num >= 50) && (num <= 100)) &&
               (duration > 0)){

        call_cost = duration * billrate2;
        cout.precision(2);
        cout.setf(ios::fixed);
        cout << "The cost of the call : $" << call_cost;
        }

      else if ((day == 'S' && day2 == 'A') &&
                (day == 'S' && day2 == 'U') || ((num > 0) && (num <= 200)) &&
               (duration > 0)){

        call_cost = duration * billrate3;
        cout.precision(2);
        cout.setf(ios::fixed);
        cout << "The cost of the call : $" << call_cost;
        }

        if (duration < 0)
            cout << "\nWrong input, you may try again!";
    }while(duration < 0);

}

void EnterTime(char* hour, char* mins)
{
    stringstream str, str2;

    cout << "\nEnter your time : ";
    cin.get(hour, 3); cin.get(mins, 3);

    str  << hour;
    str2 << mins;

    int w, x;
    str >> w;
    str2 >> x;

    cout << "\nThe time entered is = " << w << ":" << x << endl;

}


void YesNo (char& select)
{
        cout << "\n\nDo you want to continue? ";
        cin >> select;
        select = toupper(select);

}

int main()
{

char day, day2, select, hour[3], mins[3];
int duration, num;
double call_cost;

do
{

    EnterDay(day, day2);

    TestCost(duration, num, call_cost, day, day2);

    EnterTime(hour, mins);

    YesNo(select);

}while (select == 'Y');


return 0;

}
1) implementation of "EnterDay" is weird use std::string (or at least char *) and compare strings rather than char by char

2) 3 digit hour and 3 digit minutes?

3) Read http://www.cplusplus.com/reference/istream/istream/get/ once (more)
Topic archived. No new replies allowed.