未初始化的局部变量使用错误

时间:2016-09-23 07:11:08

标签: c++

所以这是我应该运行的代码,但它没有,因为我在我的代码中得到错误“未初始化的局部变量'rNumeral',我认为这是源于main。想不通为什么,我错过了什么?谢谢

#include <iostream>
#include <string>


using namespace std;

class romanType
{

public:

    int convertNum(char rNum);

    void decimalPrint();

    void romanPrint();

    romanType(int store);
    //Constructor with parameter

    romanType();

    char roman[7];
    string num;
    int length = 0;
    string dNum;
    int equals;

};


void romanType::romanPrint()
{
    cout << "The Roman numeral is: " << roman << endl;
}


void romanType::decimalPrint()
{
    cout << "The Decimal number is: " << equals << endl;
}


int romanType::convertNum(char rNum)
{

    int subtractSum = 0;
    int totalNum = 0;


    for (int i = 0; i< dNum.length(); i++)

    {

        switch (roman[i])
        {
        case 'M':
            totalNum += 1000;
            if (subtractSum < 1000)
                totalNum -= subtractSum * 2;


        case 'm':
            totalNum += 1000;
            if (subtractSum < 1000)
                totalNum -= subtractSum * 2;
            break;

        case 'D':
            totalNum += 500;
            if (subtractSum < 500)
                totalNum -= subtractSum * 2;
            break;

        case 'd':
            totalNum += 500;
            if (subtractSum < 500)
                totalNum -= subtractSum * 2;
            break;

        case 'C':
            totalNum += 100;
            if (subtractSum < 100)
                totalNum -= subtractSum * 2;
            break;

        case 'c':
            totalNum += 100;
            if (subtractSum < 100)
                totalNum -= subtractSum * 2;
            break;

        case 'L':
            totalNum += 50;
            if (subtractSum < 50)
                totalNum -= subtractSum * 2;
            break;

        case 'l':
            totalNum += 50;
            if (subtractSum < 50)
                totalNum -= subtractSum * 2;
            break;

        case 'X':
            totalNum += 10;
            if (subtractSum < 10)
                totalNum -= subtractSum * 2;
            break;

        case 'x':
            totalNum += 10;
            if (subtractSum < 10)
                totalNum -= subtractSum * 2;
            break;

        case 'V':
            totalNum += 5;
            if (subtractSum < 5)
                totalNum -= subtractSum * 2;
            break;

        case 'v':
            totalNum += 5;
            if (subtractSum < 5)
                totalNum -= subtractSum * 2;
            break;

        case 'I':
            totalNum += 1;
            if (subtractSum < 1)
                totalNum -= subtractSum * 2;
            break;

        case 'i':
            totalNum += 1;
            if (subtractSum < 1)
                totalNum -= subtractSum * 2;
            break;
        default: 
            cout << "not valid!" << endl;
        }

        totalNum = totalNum + subtractSum;
        equals = totalNum;
        return equals;

    }
};



int main()
{
    romanType output;


    int rNumeral;
    char entry;
    romanType roman = romanType(rNumeral);

    // input for entry
    cout << "Please enter a Roman numeral (Capitalized only): " << endl;
    cin >> rNumeral;

    // print options
    cout << "Print Decimal or Roman Numeral? Type 1 for Decimal, 2 for Roman Numeral: " << endl;
    cin >> entry;

    if (entry == '1')
    {
        cout << "You chose to view the decimal conversion." << endl;
        roman.decimalPrint();


    }
    else if (entry == '2')
    {


        cout << "You chose to view the Roman numeral." << endl;
        roman.romanPrint();
    }
    else
        cout << "Error: bad input" << endl;
    return 0;
    exit(1);


}

1 个答案:

答案 0 :(得分:0)

这在gcc编译时没有警告:

#include <iostream>

void foo(int i)
{
  std::cout << i << std::endl;
}

int main()
{
  int r;
  std::cin >> r;
  foo(r);
}

而这会生成未初始化的变量警告(正确):

#include <iostream>

void foo(int i)
{
  std::cout << i << std::endl;
}

int main()
{
  int r;
  foo(r);
  std::cin >> r;
}

示例编译器输出:

/tmp/gcc-explorer-compiler116823-58-135fozf/example.cpp: In function 'int main()':
11 : warning: 'r' is used uninitialized in this function [-Wuninitialized]
foo(r);
^
Compiled ok