-2

I have this function that will take the onput of hours and minutes. And if the delimiter isn't a colon than it should give an error. Same if the hours are larger than 23 and minutes larger than 59. With my current loop, the while bool seems to always be true. What have I done wrong? Can't see anything that i possible could change.

int delaString(string &samtalStart, int &timmarStart, int &minuterStart) {
        istringstream is(samtalStart);
        char colon;
        getline(cin, samtalStart);
        is >> timmarStart >> colon >> minuterStart;
        while (colon != ':' || timmarStart > 23 || minuterStart > 59) {
            cout << "Inkorrekt inmatning, var vänligen skriv in en timme mellan 00-23 och minuter mellan  0-59 i formatet tt:mm.\n" << endl;
            getline(cin, samtalStart);
        }
        // is >> timmarStart >> colon >> minuterStart;

     return(timmarStart, minuterStart);
}
Thesar
  • 57
  • 1
  • 1
  • 9
  • Hint: where do you examine the new input? – molbdnilo Dec 16 '17 at 13:38
  • And what do you expect that `return(timmarStart, minuterStart)` does? (Hint: it returns `minuterStart`.) – molbdnilo Dec 16 '17 at 13:41
  • @molbdnilo you mean i should use an if before the while loop? – Thesar Dec 16 '17 at 13:41
  • 1
    You should actually rewrite it so all input is outside of this function and then validate its return values, instead of doing everything in one place. – molbdnilo Dec 16 '17 at 13:42
  • @molbdnilo your right, it worked when i had the input outside, but i had a hard time looping the input after validating the input in the function. – Thesar Dec 16 '17 at 13:43
  • @molbdnilo btw, so the return can only return one value to my main? not both timmarStart and minuterStart? – Thesar Dec 16 '17 at 13:53
  • 1
    @Thesar The function returns an `int` (that's what `int delaString` means). It does not return *two* `int`s. – Angew is no longer proud of SO Dec 16 '17 at 13:55
  • @Angew and a void function vould not help either right? – Thesar Dec 16 '17 at 13:57
  • 1
    @Thesar A `void` function doesn't return anything. If you want to return these two `int`s, create a class for them (`struct TimePoint { int hour; int minute; };`). You could also return a `std::pair`, but the former gives more semantics. – Angew is no longer proud of SO Dec 16 '17 at 13:59
  • @Angew haven't come so far in my learning. Watching a course on Udemy. But the conclusion is, even if I use references, I can not return more than one int? Got to rewatch the latest videos one more time then. Thank you Angew. – Thesar Dec 16 '17 at 14:03
  • 2
    @Thesar You might be interested in the [list of good C++ books](https://stackoverflow.com/q/388242/1782465) maintained here on SO. – Angew is no longer proud of SO Dec 16 '17 at 14:06

1 Answers1

1

In the function you set the value of the colon once then go into an infinite loop a solution to this be to have the stringstream in the while loop like this:

int delaString(string &samtalStart, int &timmarStart, int &minuterStart) {

        char colon;
        getline(cin, samtalStart);
        istringstream is(samtalStart);
        is >> timmarStart >> colon >> minuterStart;
        while (colon != ':' || timmarStart > 23 || minuterStart > 59) {
            cout << "Inkorrekt inmatning, var vänligen skriv in en timme mellan 00-23 och minuter mellan  0-59 i formatet tt:mm.\n" << endl;
            getline(cin, samtalStart);
            istringstream iss(samtalStart);
            iss >> timarStart >> colon >> minuterStart;
        }
        // is >> timmarStart >> colon >> minuterStart;

     return(timmarStart, minuterStart);
}

Hope this helps

Thesar
  • 57
  • 1
  • 1
  • 9
Jake Freeman
  • 1,700
  • 1
  • 8
  • 15