Do While Loop - Undeclared标识符

时间:2014-09-21 08:51:35

标签: c++ loops while-loop

美好的一天,我想问为什么我的变量是未申报的,尽管我最初宣称它。 我在while循环中的typeSel变量确实有一个下划线,发现未声明的标识符。 谢谢你的帮助

int quantity, typeSel; // quantity, modify type
double weight;
string w,q; // weight, quantity
char modSel; // modify selection


if( modSel =='y'|| modSel == 'Y'){

    do{
        cout << "What do you want to change : " << endl;
        cout << "1. Flavour" << endl << "2. Weight" << endl << "3. Quantity" << endl;
        cin >> typeSel;
    }while(typSel != 1 && typSel != 1 && typSel != 1);

    if(typeSel == 1){
        Flavour();
    }
    else if(typeSel == 2){

    }
    else if(typeSel == 3){

    }
}

4 个答案:

答案 0 :(得分:2)

typSeltypeSel 不是相同的标识符。

让我们近距离观察它们,也许它会更清晰:

typSel
typeSel
   ^
   |
   +-- here

: - )

此外,虽然我感谢有些人可能并不完全信任计算机,但要求验证变量并非特定值不应该三次次。您可能想要检查除1以外的其他几个值。

变化:

}while(typSel != 1 && typSel != 1 && typSel != 1);

成:

} while (typeSel != 1 && typeSel != 2 && typeSel != 3);

你应该没事。

答案 1 :(得分:0)

这是一个错字:typSel!= typeSel

我建议更仔细地阅读调试器错误消息:“未声明的标识符”表示该变量尚未在任何地方声明(或者编译器无法找到它)。

答案 2 :(得分:0)

拼写错误:)

在你使用

的时候
    typSel

但是真实的是

    typeSel

:p:p:)

答案 3 :(得分:0)

您使用了两个不同的变量,而不是一个。

int quantity, typeSel;

在您使用时,它会创建Integer类型variable-typeSel:

while(typSel != 1 && typSel != 1 && typSel != 1);

但你还没有初始化typSel。

相关问题