尝试使用for循环输出10行

Trying to output 10 lines using for loop

本文关键字:循环 输出 10行 for      更新时间:2023-10-16

在程序中输入数字时,我试图获得以下输出:

https://i.stack.imgur.com/mhjlR.jpg

但我只得到一行而不是全部十行。有人能带我走过这个吗?非常感谢。我下面的代码:


#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char ans='y';
int num = 0;
while (ans=='y' || ans=='Y')
{
system("cls");
system("color f0");
cout<<"ttt****************************************"<<endl;
cout<<"ttt*                                      *"<<endl;
cout<<"ttt*                                      *"<<endl;                   
cout<<"ttt*      Square-Cube Program             *"<<endl;
cout<<"ttt****************************************n"<<endl;
cout<<"Please enter a number to square,cube, and raise to the 4th power: ";
cin>>num;
cout<<"t"<<"Number"<<"t"<<"Square"<<"t"<<"Cube"<<"t"<<"4th Power"<<endl;
cout<<"t"<<"------"<<"t"<<"------"<<"t"<<"----"<<"t"<<"---------"<<endl;
for(int num=0; num<100; num++,num+=5){
    cout<<"t"<<num<<"t"<<pow(num,2.0)<<"t"<<pow(num,3.0)<<"t"<<pow(num,4.0)<<endl;
cout<<"Would you like to continue (Y or N)? ";
cin>>ans;
}
cout<<"ntttT H A N K  Y O U"<<endl;
system("pause");
return 0;
}

我想你是想这么做的:

for(int i=0; i<10; i++)
{
    cout<<"t"<<num<<"t"<<pow(num,2.0)<<"t"<<pow(num,3.0)<<"t"<<pow(num,4.0)<<endl;
    num+=5;
}