C++ 使用goto时跳过For循环
这是我查找字符串回文的代码。 最初的执行效果很好。 但当它再次执行程序时,它会转到标签,跳过cin语句,直接执行所有内容,最后到达末尾。 当我在C++ 6中使用它时,这并没有发生,但是在VisualStudio 2015中发生了。 提前感谢你的帮助C++ 使用goto时跳过For循环,c++,visual-c++,C++,Visual C++,这是我查找字符串回文的代码。 最初的执行效果很好。 但当它再次执行程序时,它会转到标签,跳过cin语句,直接执行所有内容,最后到达末尾。 当我在C++ 6中使用它时,这并没有发生,但是在VisualStudio 2015中发生了。 提前感谢你的帮助 #include "stdafx.h" #include<iostream> using namespace std; void main() { char string[100], ch; int x, i, j,
#include "stdafx.h"
#include<iostream>
using namespace std;
void main()
{
char string[100], ch;
int x, i, j, flag = 0;
label:
cout << "Enter a string. Max Length 99 : ";
cin.getline(string, 100);
x = strlen(string);
cout << "Checking For Palindrome...........";
for (long i = 0; i < 5000000; ++i)
{
cout << "";
}
cout << endl;
for (i = 0, j = x - 1; i < x / 2; ++i, --j)
{
if (string[i] == string[j])
continue;
else
{
flag = 1;
break;
}
}
if (flag == 1)
cout << "The String You Entered Is Not A Palindrome";
else
cout << "The String You Entered Is A Palindrome";
cout << "\nDo you want to execute again? (Y/N)";
cin >> ch;
if (ch == 'y' || ch == 'Y')
goto label;
else
cout << "See You Later :)";
}
#包括“stdafx.h”
#包括
使用名称空间std;
void main()
{
字符串[100],ch;
int x,i,j,flag=0;
标签:
不能回答,只能是一个一般性的评论。不要使用goto
。Ever.@Sty我不会说Ever,但99.9%的情况最好用循环构造来解决。用包装那个笨蛋,而goto
在这里不给你买任何东西。你不应该使用goto
,但它不是罪魁祸首,你需要清除ncin>>ch;
之后的ew行字符,即cin.ignore(std::numeric\u limits::max(),'\n');
检查标志
变量。必须在转到标签之前重置它;
。