C ++ Stack:未初始化的局部变量

时间:2013-04-06 21:39:38

标签: c++

#include "iostream"
using namespace std;

int main()
{
int a,d,D,c,C,b,B,r,R;
char f,y,Y,n,N;

cout << "Press D - d, for dog,C -c for cat, B - b, for bird or R - r for Reptile:";
cin >> a;

if (a == d || a == D || a == c || a == C || a == b || a == B || a == r || a == R);

        if (a == d || a == D || a == c || a == C)

                if (a == d || a == D )
                 cout << "Is it neutered(Y/N)";
        cin >> f;
        if (f == Y || f == y)
                cout << "50 Euros.";
        else if (f == n || f == N)
                cout << "80 Euros.";

        if (a == c || a == C )
                cout << "Is it neutered(Y/N)";
        cin >> f;
        if (f == Y || f == y)
                cout << "40 Euros.";
        else if (f == n || f == N)
                cout << "60 Euros.";
        if (a == r || a == R || a == B || a == b)
                cout <<"10 Euros.";

if (a != d && a != D && a != c && a != C && a != b && a != B && a != r && a != R)
                cout << "Error!";

        system("pause");


        return 0;}

我收到错误:

...\12b.cpp(21): warning C4390: ';' : empty controlled statement found; is this the intent?
...\12b.cpp(20): error C4700: uninitialized local variable 'd' used
...\12b.cpp(20): error C4700: uninitialized local variable 'D' used
...\12b.cpp(20): error C4700: uninitialized local variable 'c' used
...\12b.cpp(20): error C4700: uninitialized local variable 'C' used
...\12b.cpp(20): error C4700: uninitialized local variable 'b' used
...\12b.cpp(20): error C4700: uninitialized local variable 'B' used
...\12b.cpp(20): error C4700: uninitialized local variable 'r' used
...\12b.cpp(20): error C4700: uninitialized local variable 'R' used
...\12b.cpp(25): error C4700: uninitialized local variable 'Y' used
...\12b.cpp(25): error C4700: uninitialized local variable 'y' used
...\12b.cpp(27): error C4700: uninitialized local variable 'n' used
...\12b.cpp(27): error C4700: uninitialized local variable 'N' used

为什么我会收到此错误?我不明白??

2 个答案:

答案 0 :(得分:3)

好的,让我们让这段代码变得更容易。我认为你打算做的事情是这样的:

char selection;
cout << (as before)
cin >> selection;

selection = tolower(selection);

switch(selection) {
case 'd':
   // stuff for dogs here
   break;
case 'c':
   // stuff for cats
   break;
// etc.
default:
   // error case
}

右?

答案 1 :(得分:2)

我认为很明显,而不是

if (a == d || ...

你真的想要

if (a == 'd' || ...

即。您想要将变量a与字符'd'进行比较。你已经多次犯了同样的错误,所以要修复它们,看看还剩下什么错误。

当你应该a时,你错误地将int声明为char。你有f的权利,但出于某种原因不适用于a