开发者

Why is this 'if' statement looping?

开发者 https://www.devze.com 2023-04-11 19:49 出处:网络
Hey guys I\'m writing part of a code for a project and I\'m stuck on one thing. If this is something good coders figure out on their own at some point (as I\'d like to become a good one, week five wit

Hey guys I'm writing part of a code for a project and I'm stuck on one thing. If this is something good coders figure out on their own at some point (as I'd like to become a good one, week five with c++; so far so good...) and its a trial, say the word and I'll screw, but I've been at debugging for about a half hour and don't understand why my 'if' statement is looping.

The input should look like this:

p 1开发者_Python百科1:34 12:45

Where p indicates if you're done (it'll be 's' if you want it to be out, represented here by 'end').

const int LIST_SPACE = 1000; // this is outside of the main function
string c;                    // and is 1000 because of a parameter set by the teacher
string end = "s";
string start = "p";
int temp_start_hour;
int temp_start_min;
int temp_end_hour;
int temp_end_min;

string colon = ":";
int begin_hours[LIST_SPACE];
int begin_min[LIST_SPACE];
int end_hours[LIST_SPACE];
int end_min[LIST_SPACE];
int i = 0;
do {
    cin >> c; //where c is a string

    if(c != start && c != end)
    {
        cout << "ERROR IN INPUT";
        return 1;
    }

    if(c != end)
    {
        cin >> temp_start_hour >> colon >> temp_start_min;
        cin >> temp_end_hour >> colon >> temp_end_min;
        begin_hours[i] = temp_start_hour;
        begin_min[i] = temp_start_min;
        end_hours[i] = temp_end_hour;
        end_min[i] = temp_end_min;
        cout << begin_hours[i]; //I did this to check if it was storing values
        i++;
    }
 }while(c != end); //ending the do-while loop

I'd really appreciate a nudge in the right direction with this guys. Or advice in general on a concept I'm missing. Thanks!

The output I keep getting, by the way is: (this is for the input 'p 11:34 12:34')

11111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111Segmentation fault (core dumped)


This line is wrong :

cin >> temp_start_hour >> colon >> temp_start_min;

What it means : read an int, then read a string, and finally read another int. The value of the variable colon is not read at all.

You can try the following code to see how it behaves :

string sep = ":";
int a, b;

cin >> a >> sep >> b;

cout << a << endl;
cout << sep << endl;
cout << b << endl;


Your first problem is that 'colon' gets all of ":34", then start_minutes gets the 12 that was supposed to be the next hour. But the real problem is that cin leaves cruft on the stream that is picked up by subsequent calls, so those calls skip asking you for more input and just take the left over characters. Use cin.ignore() after each call as a kludgy patch to make it work, but think alot harder about redesigning the whole thing with the safer functions.


The problem is with the type of the variable colon. The fix is very simple: just change the type of colon from string to char:

//string colon = ":"; //commenting out the old code
char colon; //new code : no need to initialize it

Why string colon causes problem because when the type is string the cin reads all characters beginning from ':' to a character until it encounters a space, when in fact, you intend to read only a single character called':'. For which, the correct data type is char (or you can choose unsigned char as well).


You are not incrementing the c variable

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号