尝试使用for循环输出10行

时间:2015-03-23 22:43:59

标签: c++

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

http://i.imgur.com/POIlhb2.png

但我只得到一行而不是全行。 有人可以带我走过这个吗? 谢谢。 我的代码如下:


#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<<"\t\t\t****************************************"<<endl;
cout<<"\t\t\t*                                      *"<<endl;
cout<<"\t\t\t*                                      *"<<endl;                   
cout<<"\t\t\t*      Square-Cube Program             *"<<endl;
cout<<"\t\t\t****************************************\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<<"\n\t\t\tT H A N K  Y O U"<<endl;
system("pause");

return 0;
}

1 个答案:

答案 0 :(得分:4)

我认为你打算这样做:

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;
}