C++ Some variables “used uninitialized in this function”. Why?

前端 未结 2 806
滥情空心
滥情空心 2021-01-28 10:06

This is my code:

#include 
using namespace std;

void input_function(int hour, int minutes);
void calcuation(int hour, int minutes, char meri         


        
相关标签:
2条回答
  • 2021-01-28 10:52

    The error you're getting about uninitialized variables means that the program is trying to use the variables without them being assigned a value. Wikipedia defines them as follows:

    In computing, an uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one. As such, it is a programming error and a common source of bugs in software.

    To fix these errors you might try to give your variables initial values. The actual number used shouldn't matter unless its to big for the integer.

    Its also very likely that when you fix that, that your program will not do what you expect. That is because currently your functions are accepting parameters, but not actually changing the variables in your int main() function.

    There are a couple of ways to fix this. You could pass by reference:

    void input_function(int& hour, int& minutes)
    {
      //things happen, the & sign before the variables is the important part
    }
    

    Doing this would let the function interact with the variables that you actually want to use, and is probably the best way to tackle this.

    Or if that's confusing might also try declaring them as global variables or making your functions return integer values.

    Best of luck!

    0 讨论(0)
  • 2021-01-28 11:05

    Your input_function should pass by reference.
    If you pass by value only, it would not affect anything.

      #include <iostream>
        using namespace std;
    
        void input_function(int& hour, int& minutes);
        void calcuation(int hour, int minutes, char meridien);
        void output_function(int hour, int minutes, char meridien);
    
        int main ()
        {
          int hour, minutes;
          char answer, meridien;
    
          do
            {
              input_function(hour, minutes);
              calcuation(hour, minutes, meridien);
              output_function(hour, minutes, meridien);
    
    
              cout << "Would you like to try again? (Y/N)\n";
              cin >> answer;
    
            } while(answer == 'Y' || answer == 'y');
    
          cout <<"Goodbye\n";
    
          return 0;
        }
    
        void input_function(int& hour, int& minutes)
        {
          cout << "Enter the hour in 24 hour notation: (Ex. 23)\n";
          cin >> hour;
    
          cout << "Enter the minutes:\n";
          cin >> minutes;
        }
    
        void calcuation(int hour, int minutes, char meridien)
        {
          if (hour > 12)
            {
              hour = hour - 12;
              meridien = 'P';
            }
          else 
            {
              meridien = 'A';
            }
        }
    
        void output_function(int hour, int minutes, char meridien)
        {
          cout << "Your time in 12 hour notation is:\n";
          cout << hour << ":" << minutes << endl;
    
          if (meridien == 'P')
            cout << "P.M.\n";
    
          else 
            cout << "A.M\n";
        }
    
    0 讨论(0)
提交回复
热议问题