Problem with run again loop

The program lets you put in a start time and a end time and then calculates a price of the duration of the call. The problem that I have in my main section is when I try to run the program again the output is not the same. When I first run the program the output looks like this(when I haven't declared any input).

What time did the call start? (hh:mm): 


But when I run the program again it looks like this,

What time did the call start? (hh:mm): What time did the call end? (hh:mm): 


This means that I can't write any input for the start time. So the program won't work.

PS. The variables and constants are written in swedish, sorry for that.

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
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

double kostnad(string start, string end);
double minuter(int startTimme, int startMinut, int slutTimme, int slutMinut);
bool felCheck(int startTimme, int startMinut, int slutTimme, int slutMinut);

int main()
{
    char ch;
    
    do
    {
        string callStart,callEnd;
        
        cout<<"What time did the call start? (hh:mm): ";
        getline(cin, callStart);
        
        cout<<"What time did the call end? (hh:mm): ";
        getline(cin, callEnd);
        
        cout<<kostnad(callStart,callEnd)<<" kr" << endl;
        
        cout << "Run program again(y/n)? ";
        cin >> ch;
        do
        {
            ch = toupper(ch);
        }while( !(ch == 'Y' || ch == 'N'));
        
    }while(ch == 'Y');

    return 0;
}


double kostnad(string start, string slut)
{
    int startTimme = 0, startMinut = 0, slutTimme = 0, slutMinut = 0;
  
    string::size_type startPos = start.find(":");
    if(startPos != string::npos)
        ;
    else
    {
        cout << "Endast : är tillåtet" << endl;
        return 0;
    }
    
    string::size_type slutPos = slut.find(":");
    if(slutPos != string::npos)
        ;
    else
    {
        cout << "Endast : är tillåtet" << endl;
        return 0;
    }

    start.replace(start.find(":"), 1, " ");
    slut.replace(slut.find(":"), 1, " ");
    
    istringstream iss(start);
    iss >> startTimme >> startMinut;
    
    iss.clear();
    iss.str(slut);
    iss >> slutTimme >> slutMinut;
    
    if (felCheck(startTimme,startMinut,slutTimme,slutMinut))
        
    return minuter(startTimme,startMinut,slutTimme,slutMinut);
    
}


bool felCheck(int startTimme, int startMinut, int slutTimme, int slutMinut)
{
    if (((startTimme * 60) + startMinut) >= ((slutTimme * 60) + slutMinut))
    {
        cout << "Tidsfel vid inmattning ";
        return true;
    }
    
    if (startTimme>23||startTimme<00||slutTimme>23||slutTimme<00||startMinut>60||startMinut<00||slutMinut>60||slutMinut<00)
    {
        cout << "Tidsfel vid inmattning ";
        return true;
    }
    else
        return false;
}


double minuter(int startTimme, int startMinut, int slutTimme, int slutMinut)
{
    int startInMinutes = startTimme * 60 + startMinut;
    int endInMinutes = slutTimme * 60 + slutMinut;
    
    
    int totMinuter = 0;
    int const fullpristaxa=4;
    double pris;
    const double moms=1.25;
    const double rabatt=0.35;
    const double rabatt30min=0.85;
    
    totMinuter=endInMinutes-startInMinutes;

    if((startTimme>=8 && startMinut>=0) && (slutTimme<=18 && slutMinut<=30))
        pris=totMinuter*fullpristaxa*moms;
    else
        pris=(fullpristaxa*rabatt)*totMinuter*moms;
    
    if(totMinuter>30)
    {
        pris=pris*rabatt30min;
    }
    
    return pris;
}
Last edited on
There are some input problems. Try changing line 29 to
cin >> ch; cin.ignore(256,'\n');

Let's just say that some of those Swedish words don't come across very well in English!
Thanks that solved the problem! But what do 256 stand for?
256 is just "enough" extra characters. As long as you don't sit on the space bar and put that many spaces in the input buffer you should be OK. Most of the time it will just read up to the newline character '\n'.
Topic archived. No new replies allowed.