Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么只有第一个循环工作而其余的循环不工作?_C++ - Fatal编程技术网

C++ 为什么只有第一个循环工作而其余的循环不工作?

C++ 为什么只有第一个循环工作而其余的循环不工作?,c++,C++,我是一个初学者,对编码几乎一无所知。 我尝试用C++语言中的所有三个循环打印一个表,但只有第一个循环工作。 请看一看并解释这背后可能的原因。 这是我的密码:- #include <iostream> using namespace std; int main() { int n, i=1, result=1; cout << "Enter the table you want: "; cin >> n; /

我是一个初学者,对编码几乎一无所知。 我尝试用C++语言中的所有三个循环打印一个表,但只有第一个循环工作。 请看一看并解释这背后可能的原因。 这是我的密码:-

#include <iostream>
using namespace std;
int main()
{
    int n, i=1, result=1;
    cout << "Enter the table you want: ";
    cin >> n;

    //using for loop
    cout<<"using for loop";
    for (i = 1; i <= 10; i++)
    {
        result=n*i;
        cout<<endl<<n<<" x "<<i<<" = "<<result;
    }


    //using while loop
    cout<<"\n\nUsing while loop";
    while (i<=10)
    {
        result= n*i;
        cout<<endl<<n<<" x "<<i<<" = "<<result;
        i++;
    }


    //using do-while loop
    cout<<"\n\nusing do-while loop";
    do
    {
        result=n*i;
        cout<<endl<<n<<" x "<<i<<" = "<<result;
    }while(i<=10);
}
#包括
使用名称空间std;
int main()
{
int n,i=1,结果=1;
cout>n;
//用于循环
不能包含
使用名称空间std;
int main()
{
int n,i=1,j=1,k=1,结果=1;
cout>n;
//用于循环

cout发生这种情况的原因是代码中有一些错误

#include <iostream>
using namespace std;
int main()
{
    int n, i = 1, result = 1;
    cout << "Enter the table you want: ";
    cin >> n;

    //using for loop
    cout << "using for loop";
    for (i = 1; i <= 10; i++)
    {
        result = n * i;
        cout << endl << n << " x " << i << " = " << result;
    }


    //using while loop
    cout << "\n\nUsing while loop";
    i = 1; //In the original code, i was not reset to 0, therefore the loop ended instantly.
    while (i <= 10)
    {
        result = n * i;
        cout << endl << n << " x " << i << " = " << result;
        i++;
    }


    //using do-while loop
    cout << "\n\nusing do-while loop";
    i = 1; //i is reset by me again
    do
    {
        result = n * i;
        cout << endl << n << " x " << i << " = " << result;
        ++i; //You also forgot to increment i after every loop.
    } while (i <= 10); //the same thing happened here as in the while
}
#包括
使用名称空间std;
int main()
{
int n,i=1,结果=1;
cout>n;
//用于循环

在第一次循环后,请不要尝试打印出
i
的值,然后查看它是什么。您还没有重置
i
的值。当程序执行任何您不期望的操作时,请打印出决策中使用的变量,并验证它们是否正确。几乎所有编译器都附带的一个优秀工具是调试器。使用调试器,您可以以您的速度执行程序,并观察程序的运行情况。这非常有启发性,在提高程序员生产率方面可能仅次于编译器。这需要一些实践,但您越早掌握调试器的使用,就越快可以节省大量时间。通过Grabthar的锤子,这是一个多么大的救星啊只使用代码的答案不受欢迎,因为它们往往会产生错误。通过添加询问者的错误解释、您的更改以及更改是如何解决问题的,很容易挽救这个答案。
#include <iostream>
using namespace std;
int main()
{
    int n, i = 1, result = 1;
    cout << "Enter the table you want: ";
    cin >> n;

    //using for loop
    cout << "using for loop";
    for (i = 1; i <= 10; i++)
    {
        result = n * i;
        cout << endl << n << " x " << i << " = " << result;
    }


    //using while loop
    cout << "\n\nUsing while loop";
    i = 1; //In the original code, i was not reset to 0, therefore the loop ended instantly.
    while (i <= 10)
    {
        result = n * i;
        cout << endl << n << " x " << i << " = " << result;
        i++;
    }


    //using do-while loop
    cout << "\n\nusing do-while loop";
    i = 1; //i is reset by me again
    do
    {
        result = n * i;
        cout << endl << n << " x " << i << " = " << result;
        ++i; //You also forgot to increment i after every loop.
    } while (i <= 10); //the same thing happened here as in the while
}