Problem with calling a function in a loop

I'm trying to call the function ringpling in a loop but the program don't work as it should. What am I doing wrong?
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
128
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

double cost(string start, string end);
double minutes(int startHour, int startMinute, int endHour, int endMinute);
bool errorCheck(int startHour, int startMinute, int endHour, int endMinute);

void ringpling();

int main()
{
    int input;
    cout << "Put in the integer 1: ";
    cin >> input;
    if(input==1)
        ringpling();
    else
        return 0;
}

void ringpling()
{
    while (true)
    {
        char again = 0;
        string callStart,callEnd;
        
        cout<<"give start time of call: ";
        getline(cin, callStart);
        
        cout<<"give end time of call:   ";
        getline(cin, callEnd);
        
        cout<<cost(callStart,callEnd)<<" kr";
        cout<<endl<<"run again? (y/n): ";
        cin>>again;
        cin.get(); /// somehow cin>> will push the carriage return into the next getline() if I don't do this
        switch(again)
        {
            case 'y':
            case 'Y':
                continue;
            default:
                return;
        }
    }
}



double cost(string start, string end)
{
    /////////// consants given in minute format where possible
    int startHour = 0;
    int startMinute = 0;
    int endHour = 0;
    int endMinute = 0;
    const double moms = 1.25;
    const double pricePerMin = 4*moms;
    
    //////////// make useable numbers from input
    start.replace(start.find(":"), 1, " "); // replace : with whitespace
    end.replace(end.find(":"), 1, " ");
    
    istringstream iss(start); // for making it to a number
    iss >> startHour >> startMinute; // it automatically finds whitespace separator
    
    iss.clear(); // using the same again
    iss.str(end);
    iss >> endHour >> endMinute;
    
    //////////////////// make sure data is OK before making calculations.
    if (errorCheck(startHour,startMinute,endHour,endMinute))
        return 0;
    //////////////////// calculations
    /// find the total cost and return it
    return minutes(startHour,startMinute,endHour,endMinute)*pricePerMin;
    
}
////////////////////////////
/////// function to make sure we have OK data input
bool errorCheck(int startHour, int startMinute, int endHour, int endMinute)
{
    // check if call ends before it start, or if it is out of 24hr clock
    if ((((startHour * 60) + startMinute) >= ((endHour * 60) + endMinute))||startHour>23||startHour<00||endHour>23||endHour<00||startMinute>60||startMinute<00||endMinute>60||endMinute<00) // >= because we cant do anything with 0 minute calls anyway
    {
        cout << "time error!!!";
        return true;
    }
    else
        return false;
}
//////////////////////
//// calculations function
double minutes(int startHour, int startMinute, int endHour, int endMinute)
{
    const int minute = 1;
    const double discountMin = 0.35;
    const int fullPriceStart = 8*60;
    const int fullPriceEnd = 18*60+30;
    const int discountIf = 30;
    const double discount = 0.15;
    
    double newMinutes = 0;
    
    /// all calculations are done in minutes
    int startInMinutes = startHour * 60 + startMinute;
    int endInMinutes = endHour * 60 + endMinute;
    
    /// count minutes. for each discount minute count 0.35 minutes
    for (int i=startInMinutes;i<endInMinutes;i++)
    {
        if (i >= fullPriceStart && i < fullPriceEnd)
            newMinutes += minute;
        else
            newMinutes += discountMin;
    }
    /// check if call was longer than 30 minutes and apply discount.
    if (endInMinutes-startInMinutes>discountIf)
    {
        newMinutes -= newMinutes*discount;
    }
    return newMinutes;
}


The code works just fine when it isn't in a loop, like this.
1
2
3
4
int main()
{
    ringpling();
}

Which loop do you mean?
No loop here:
1
2
3
4
int main()
{
    ringpling();
}


No loop here:
1
2
3
4
5
6
7
8
9
10
int main()
{
    int input;
    cout << "Put in the integer 1: ";
    cin >> input;
    if(input==1)
        ringpling();
    else
        return 0;
}


However, the second version does have a cin >> input; which doesn't tidy up after itself.
Try adding a cin.ignore(1000, '\n'); after the cin >> so that the contents of the input buffer (including trailing newline character) will be removed.

To be safer, if an invalid integer is entered, then the error flags for the input stream would need to be cleared too:
1
2
3
4
5
    int input;
    cout << "Put in the integer 1: ";
    cin >> input;
    cin.clear();            // reset flags
    cin.ignore(1000, '\n'); // empty buffer 

The value 1000 is an arbitrary large number here, it assumes the user didn't type more than that many characters at the prompt.
Sorry for that I always think that the if-statement is a loop even if I know is not. But thanks anyway that solved my problem!
Topic archived. No new replies allowed.