简体   繁体   English

如何接受用户输入,对该输入进行数学运算,并将结果存储在数组中? 在 C++

[英]how to take user input, do math to that input, and store the results in an array? in C++

I feel like there is a better way to do this getOvertones function with an array.我觉得有更好的方法可以用数组来完成这个 getOvertones function 。 Clearly the code takes an input (hz) and then gets the overtone frequencies.显然,代码采用输入 (hz),然后获取泛音频率。 I know there must be a more proficient way to do this.我知道必须有更熟练的方法来做到这一点。 Any help is greatly appreciated as I am trying to teach myself.非常感谢任何帮助,因为我正在努力自学。

double getOvertones()
{
    double root;
    std::cout << "enter frequency" << '\n';
    std::cin >> root;
    double fundamental = root;
    double second = root * 2.00;
    double third = root * 3.00;
    double fourth = root * 4.00;
    double fifth = root * 5.00;
    std::cout << fundamental << '\n' 
              << second      << '\n' 
              << third       << '\n' 
              << fourth      << '\n' 
              << fifth       << '\n';
    return fundamental;
};

I was trying things like:我正在尝试这样的事情:

double frequency;
cin >> frequency;

double overtones[5];

for i in overtones...

I'm not sure a this point how to fill an array with math results basically.我不确定这一点如何基本上用数学结果填充数组。

double getOvertones()
{
    double root;
    double root2;
    std::cout << "enter frequency in hz" << '\n';
    std::cin >> root;

 for (double i = 1; i < 6; i++)
 {
     root2 = root * i;
     std::cout << root2 << '\n';
 }

    return root2;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM