【发布时间】:2011-12-03 10:46:59
【问题描述】:
大家好,我正在为一个项目编写部分代码,但我被困在一件事上。如果这是一个优秀的程序员在某个时候自己想出来的事情(因为我想成为一个优秀的程序员,使用 c++ 的第五周;到目前为止一切都很好......)并且它是一个试验,说出这个词然后我'会搞砸的,但我已经调试了大约半个小时,不明白为什么我的“if”语句在循环。
输入应如下所示:
p 11:34 12:45
其中 p 表示您是否完成(如果您希望它退出,它将是 's',这里用 '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
我真的很感激与这些家伙一起朝着正确的方向轻推。或者就我缺少的概念提供一般建议。谢谢!
顺便说一句,我不断得到的输出是:(这是输入'p 11:34 12:34')
11111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111Segmentation fault (core dumped)
【问题讨论】:
-
整个项目涉及比较调用列表,以查看一个计划是否比另一个更好。你的问题的答案是肯定的,所以如果你不能帮助我理解,但是当我说输入几乎没有触及这个项目的表面时,请相信我。真的,我只是在概念上对为什么会发生这种情况感到困惑。
-
start和end持有什么? -
start 和 end 是分别包含 'p' 和 's' 的字符串。 c 是一个字符串,包含输入中的一个字符。
-
您在处理时间输入时弄乱了流操作符。
-
什么是
colon?最好的猜测,你的输入已经结束了,然后所有来自cin的进一步读取都变成了 noops,因为你从不检查 eof
标签: c++ loops if-statement while-loop do-while