注册 登录
编程论坛 C++教室

新手求教打印余弦函数的问题?

aikongfu 发布于 2012-08-26 18:55, 426 次点击
程序代码:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    double y;
    int x, val;
    for (y = 1; y >= -1; y -= 0.1)
    {
        val = acos(y) * 10;
        for (x = 1; x < val; x++)
            cout << " ";
        cout << "*";
        for (; x < 62 - val; x++)
            cout << " ";
        cout << "*";
    }
    return 0;
}

为什么打印出来是这个样子
只有本站会员才能查看附件,请 登录
4 回复
#2
liudw22012-08-27 01:26
程序代码:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    double y;
    int x, val;
    for (y = 1; y >= -1; y -= 0.1)
    {
        val = acos(y) * 10;
        for (x = 1; x < val; x++)
            cout << "*" << " ";
        for (; x < 62 - val; x++)
            cout << "*" << " ";
    }
    return 0;
}
for循环没有加大括号
#3
pangding2012-08-27 03:32
回复 2楼 liudw2
你有运行过自己改后的程序吗?
#4
pangding2012-08-27 03:37
每行后面加个换行符就行了:
程序代码:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double y;
    int x, val;
    for (y = 1; y >= -1; y -= 0.1)
    {
        val = acos(y) * 10;
        for (x = 1; x < val; x++)
            cout << " ";
        cout << "*";
        for (; x < 62 - val; x++)
            cout << " ";
        cout << "*";

        cout << endl;  // 加这行。
    }
    return 0;
}

#5
aikongfu2012-08-27 10:05
谢谢
1