简体   繁体   中英

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. Clearly the code takes an input (hz) and then gets the overtone frequencies. 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;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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