简体   繁体   中英

How to initialize many local variables in C++

#include <iostream>
#include <iomanip>
using namespace std;

//Declares the prototype of function half().
float half1(float num1, float halfvalue1);
float half2(float num2, float halfvalue2);
float half3(float num3, float halfvalue3);

int main()
{
    //Declares variables
    float num1, num2, num3, halfvalue1, halfvalue2, halfvalue3;
    //asks for values
    cout << "Enter 3 real numbers and I will display their halves: " << endl << endl;
    //stores values
    cin >> num1, num2, num3;
    //return half and assign result to halfvalue
    halfvalue1 = half1(num1, 2.0);
    halfvalue2 = half2(num2, 2.0);
    halfvalue3 = half3(num3, 2.0);
    //set precision
    cout << fixed << showpoint << setprecision (3);
    //Prints message with results
    cout << halfvalue1 << halfvalue2 << halfvalue3 << " are the halves of " << num1 << num2 << num3 << endl;

    return 0;
}

//function definition half
float half1(float num1, float halfvalue1)
{
    return num1 / halfvalue1;
}

float half2(float num2, float halfvalue2)
{
    return num2 / halfvalue2;
}

float half3(float num3, float halfvalue3)
{
    return num3 / halfvalue3;
}

The warnings are:

warning C4700: uninitialized local variable 'num2' used warning C4700: uninitialized local variable 'num3' used

I had full success when I was just using one variable but now I am unsure how to fix that problem.

The line cin >> num1,num2,num3; evaluates to three separate expressions:

  1. cin >> num1
  2. num2 (discarded because there are no side effects)
  3. num3 (also discarded)

The commas are taken as operators, not an initializer list.

Try this instead:

cin >> num1;
cin >> num2;
cin >> num3;

or this:

cin >> num1 >> num2 >> num3;

As pointed, error is in cin >> num1, num2, num3; with comma operator.

Using array may clean code:

#include <algorithm>
#include <iostream>
#include <iomanip>

float half(float num)
{
    return num / 2;
}

int main()
{
    // Declares variables
    float nums[3]; // or std::array<float, 3> nums; or std::vector<float> nums(3);
    float halfvalues[3];

    //asks for values
    std::cout << "Enter 3 real numbers and I will display their halves: " << std::endl
              << std::endl;
    //stores values
    for (auto& v : nums) {
        std::cin >> v;
    }
    //return half and assign result to halfvalue
    std::transform(std::begin(nums), std::end(nums), std::begin(halfvalues), &half);

    //set precision
    std::cout << std::fixed << std::showpoint << std::setprecision (3);
    //Prints message with results
    for (const auto& v : halfvalues) {
        std::cout << " " << v;
    }
    std::cout << " are the halves of ";
    for (const auto& v : nums) {
        std::cout << " " << v;
    }
    std::cout << std::endl;
}

Live Demo

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