C++ 使用完美方块的计数器控制While循环

C++ 使用完美方块的计数器控制While循环,c++,while-loop,C++,While Loop,我是编程新手,我的老师并不是真的教我们东西。我需要制作一个程序,使用pow函数查找前20个完美正方形。需要是一个计数器控制的循环。我真的不知道这意味着什么,但我在下面尽了最大努力。请帮帮我 #include <iostream> #include <stdio.h> #include <cmath> using namespace std; int main () { int a, b; cout << "Enter the n

我是编程新手,我的老师并不是真的教我们东西。我需要制作一个程序,使用
pow
函数查找前20个完美正方形。需要是一个计数器控制的循环。我真的不知道这意味着什么,但我在下面尽了最大努力。请帮帮我

#include <iostream>
#include <stdio.h>
#include <cmath>

using namespace std;

int main ()
{
    int a, b;
    cout << "Enter the num:";
    scanf("% d", &a);
    b = sqrt (a);
    if ((b * b) == a)
        cout << "The given number is a perfect square";
    else
        cout << "The given number is not a perfect square";
    getch();
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
INTA,b;

cout如果我理解正确,那么计数器控制的循环意味着它应该是一个有变量(计数器)和条件(循环)的循环,当变量回答条件时,它应该循环。 在您的示例中,整数
i
将是计数器,循环将是
for
循环:

#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
    for(int i = 1; i < 21; i++)
    {
       cout << i << "'s perfect square is " << pow(i, 2) << endl;
    }
}
#包括
#包括
使用名称空间std;
int main()
{
对于(int i=1;i<21;i++)
{

CUT看起来是以前任务的解决方案。你尝试过循环任务吗?你首先包括一个C++头,然后你包括一个C头,然后你又包含了一个C++头。我希望这段代码不是来自你的老师。你看过你的教科书,想知道什么是一个“反控”循环是什么?你的老师问?另一个学生?非常感谢你的帮助!我的老师基本上想让我们自学,因为“这是最好的学习方法”,没问题,欢迎你。如果这回答你的问题,考虑接受这个答案。