Break 语句不起作用

发布于 2024-11-27 18:29:44 字数 580 浏览 1 评论 0原文

这个程序应该将 name 的字符数存储在变量 str_length 中(其中 name < 50 个字符以“.”结尾)我很困惑为什么这段代码只吐出第一个字符,这意味着它从循环中中断当 i = 0 时,对于诸如“Jonathan”之类的名字。难道它不应该解析字符串直到找到“.”,然后才从 for 循环中中断吗?

#include <iostream>
#include <cstring>

using namespace std;

int main()
{

 string name;
 int str_length;

 cout << "What's your name" << endl;

 cin >> name;

 for (int i = 0; i < 50; i++)
 {
     cout << name[i];
     if (name[i] == '.')
        str_length = i;
        break;         
 }

 cout << endl;

 system("PAUSE");

 return 0;  
 }

This program should store the number of characters of name in the variable str_length (where name < 50 characters terminated by a ".") I'm baffled as to why this code only spits out the first character, meaning it breaks from the loop when i = 0, for a name such as "Jonathan." Shouldn't it parse through the string until it finds the ".", only then breaking from the for loop?

#include <iostream>
#include <cstring>

using namespace std;

int main()
{

 string name;
 int str_length;

 cout << "What's your name" << endl;

 cin >> name;

 for (int i = 0; i < 50; i++)
 {
     cout << name[i];
     if (name[i] == '.')
        str_length = i;
        break;         
 }

 cout << endl;

 system("PAUSE");

 return 0;  
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

骄傲 2024-12-04 18:29:44

你有:

for (int i = 0; i < 50; i++)
{
    cout << name[i];
    if (name[i] == '.')
       str_length = i;
       break;         
}

这实际上是:

for (int i = 0; i < 50; i++)
{
    cout << name[i];

    if (name[i] == '.')
    {
       str_length = i;
    }

    break;         
}

你想要:

for (int i = 0; i < 50; i++)
{
    cout << name[i];
    if (name[i] == '.')
    {
       str_length = i;
       break;  
    }       
}

你在每个循环结束时breaking。

You have:

for (int i = 0; i < 50; i++)
{
    cout << name[i];
    if (name[i] == '.')
       str_length = i;
       break;         
}

Which is actually:

for (int i = 0; i < 50; i++)
{
    cout << name[i];

    if (name[i] == '.')
    {
       str_length = i;
    }

    break;         
}

You want:

for (int i = 0; i < 50; i++)
{
    cout << name[i];
    if (name[i] == '.')
    {
       str_length = i;
       break;  
    }       
}

You are breaking at the end of each loop.

塔塔猫 2024-12-04 18:29:44

您在 if 条件周围缺少 {}。所以 break 是在没有任何条件的情况下执行的,因此循环在第一次迭代时退出。

You are missing {} around if condition. So break is executed without any condition, hence the loop is exited in the first iteration itself.

仅此而已 2024-12-04 18:29:44

这表明即使对于一行 if 语句也不使用大括号也是危险的。我怀疑你这样做了:

 if (name[i] == '.')
    str_length = i;

然后添加了 break 。如果你这样做了:

 if (name[i] == '.') { 
    str_length = i;
 }

在前面,在花括号内添加中断是很自然的,并且你不会遇到这个错误。

This demonstrates the danger of not using braces even for one line if statements. I suspect you did this:

 if (name[i] == '.')
    str_length = i;

and then added the break later. If you had done this:

 if (name[i] == '.') { 
    str_length = i;
 }

up front, it would have been natural to add the break inside the curly braces, and you wouldn't have had the bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文