简体   繁体   中英

#include <cmath> stopped working in C++20?

Summarize the problem: My goal is to generate odd numbers up to a limit, store them in a vector then output the square of them.

Describe what you've tried: So far, I used the #include < cmath > at the start before int main(), then I used a couple of if statements to check whether the limit is zero, if so I output an error message. If the limit is >0 then I squared the odd numbers using pow(num,2) however, this does not work and gives wrong answer. For example, it gives the square of 13 as 600ish, that is obviously wrong. Please give advice. My full code is here, it is very simple so I did not put much comments in there:

#include<iostream>
#include<format>
#include<cmath>
#include<vector>
using namespace std;


 int main()
 {  
int limit{};
cout << "Enter a limit of odd integers: \n";
cin >> limit;

vector<int>oddnumb(limit);

if (limit == 0)
{
    cout << "You MUST enter a number>0, then restart the program. \n";
    return 0;
}

if (limit > 0)
{
    cout << "Odd numbers are as follows: \n";
    for (size_t i{}; i < limit-1; ++i)
    {
        oddnumb[i] += ++i;

        cout << oddnumb[i] << endl;
        
    }
    cout << "Squared odd numbers follow: \n";
    for (size_t i{}; i < limit - 1; ++i)
    {
        oddnumb[i] += ++i;

        cout << pow(oddnumb[i],2) << endl;

    }

}

I've written up a working version of what you want to do. I urge you to figure it out yourself first, that's the best way to learn, and then check your version against what I've got here. Note that this is certainly not the best way to do it. But I wrote it in a way that should be easy to understand. In programming there are lots of ways to tackle any problem, the best way is arguably... the way that works! Good luck in your journey with C++!

#include <cmath>
#include <iostream>
#include <vector>
int main()
{
    double input{};
    while (input < 1)//validate that input is greater than 0 here by looping if it isn't
    {
        std::cout << "Enter a limit of odd integers: ";
        std::cin >> input;
        if (input < 1)//if a valid range is entered this is never shown
        {
            std::cout << "Enter a valid range. Greater than 0. \n\n";
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
    }

    std::vector<double> oddNumbers{};
    std::cout << "Odd numbers:\n";
         //notice we're starting this loop at 1, since 0 is even we just avoid it
    for (double i = 1.0; i <= input; i += 2.0) //you can add more than just 1 to i
    {
        oddNumbers.push_back(i);// here we're putting the value of i at the back of the vector
        std::cout << i << " squared is " << pow(i, 2.0) << '\n';
         //we could avoid using a vector by printing the squared value of i within this loop
    }

    std::cout << "Squared Odd Numbers:\n";
         //we do need to start this loop at 0 to access the first element of the vector
    for (unsigned int i = 0; i < oddNumbers.size(); i++)
         //we also increase i by just one each time to access each element we stored
    {
        std::cout << pow(oddNumbers[i], 2.0) << '\n';
    }
    return 0;
}

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