C++ 比较c+中的字符+;

C++ 比较c+中的字符+;,c++,char,C++,Char,如果我运行我的程序,它不会将在“BITE Funktion waehlen”之后输入的值与sin、cos或tan进行比较。相反,它总是直接跳到else语句 #include <iostream> #include <string> using namespace::std; int funktions_wahl(char funktion, int genauigkeit) { char sin; char cos; char tan;

如果我运行我的程序,它不会将在“BITE Funktion waehlen”之后输入的值与
sin
cos
tan
进行比较。相反,它总是直接跳到
else
语句

#include <iostream>
#include <string>

using namespace::std;

int funktions_wahl(char funktion, int genauigkeit)
{
    char sin;
    char cos;
    char tan;
    int x;
    if (funktion==sin) {
        cout << "sin von " << x << " =" << 2*x*genauigkeit; //Wie sin als fkt hier benutzen? 2x vorübergehend.
    }
    else if (funktion==cos) {
        cout << "cos von " << x << " =" << 4*x*genauigkeit; //Gleiches Spiel wie oben.
    }
    else if (funktion==tan) {
        cout << "tan von " << x << " =" << 8*x*genauigkeit; //Gleiches Spiel wie oben;
    }
    else {
        cout << "Keine gültige Variable eingegeben\r";
    }
    return 0;
}


int main() {
    char f;
    int g=0;
    cout << "Taschenrechner 1.0\r" << "Bitte Funktion eingeben: ";
    cin >> f;
    cout << "Genauigkeit wählen: ";
    cin >> g;
    funktions_wahl(f, g);
}
#包括
#包括
使用namespace::std;
国际金融中心(char funktion,国际金融中心)
{
炭素;
char-cos;
炭炭;
int x;
if(funktion==sin){
cout
charsin;
char-cos;
炭炭;
int x;
if(funktion==sin){

cout问题是您没有初始化变量
sin
cos
tan
(实际上应该是常量),这导致了未定义的行为:

  • 对于调试构建,编译器可能确实会逐步执行您的程序,从而比较未初始化的随机数

  • 对于优化的构建,编译器可能会优化未初始化的局部变量之间的比较,并且只实现错误情况(
    cout),谢谢您的回答

    我按照你的建议改了。但是仍然可以输入例如sin而不是s吗

        #include <iostream>
        #include <string>
    
        using namespace::std;
    
    
    
    int funktions_wahl(char funktion, int genauigkeit, int x)
     {
    char sin='s';
    char cos='c';
    char tan='t';
    if (funktion==sin) {
        cout << "sin von " << x << " =" << 2*x*genauigkeit << "\r"; //Wie sin als fkt hier benutzen? 2x vorübergehend.
    }
    else if (funktion==cos) {
        cout << "cos von " << x << " =" << 4*x*genauigkeit << "\r"; //Gleiches Spiel wie oben.
    }
    else if (funktion==tan) {
        cout << "tan von " << x << " =" << 8*x*genauigkeit << "\r"; //Gleiches Spiel wie oben;
    }
    else {
        cout << "Keine gültige Variable eingegeben\r";
    }
    return 0;
    }
    
    
    
    
    
    int main() {
    char f;
    int g=0;
    int x=0;
    cout << "Taschenrechner 1.0\r" << "Bitte Funktion eingeben: ";
    cin >> f;
    cout << "Genauigkeit wählen: ";
    cin >> g;
    cout << "x wählen: ";
    cin >> x;
    funktions_wahl(f, g, x);
    
    #包括
    #包括
    使用namespace::std;
    int funktions_wahl(char funktion,int genauigkeit,int x)
    {
    charsin='s';
    char cos='c';
    char tan='t';
    if(funktion==sin){
    
    CUT不是“Funktionswahl”一个名词吗?为什么你在其中插入了一个下划线?是的,我也考虑过。因为我对C++非常陌生,你有什么建议我应该改变它吗?Char没有工作。这更可读性的问题。叫它“代码> FunkssWHLL< /代码>。我的意思是,你不写“死亡Functs瓦尔”,而是。“死吧",对吗?至少对StackOverflow来说,用英语编写程序更好。@cad:我是德语母语,读代码中的德语单词让我感到畏缩。这是一个非常错误的做法。我从未在奥地利的任何公司的专业代码中看到过它,除了一些真正特殊的领域特定词汇。不过,在FuxtsWaHL,代码> FunkToStsWaHL,代码> FunkToStsWaHL等是无关的。C++中几乎所有的通用命名约定是宏和宏的ALL上限。其他所有的都是基于意见的。@ CAD:顺便说一下,“VaunssWHHL”和“Funktions Wahl”都是。是正确的德语单词。两者之间的区别非常微妙。这就是为什么OP在使用下划线时(语法上)是正确的。而且很可能
    int x=genauigkeit;
    没有必要将它们设为静态。不,
    cin>>f
    只会消耗“sin”中的第一个“s”。这将使
    cin>>g
    行为不端。您可以使用
    std::string
    阅读和比较单词。首先,因为这是对您的问题的更新,最好的方法是按如下方式编辑您的问题:(1)开始编辑您的问题(2)转到问题的结尾并附加更新(3)然后附加更新的问题。
    const char sin = 's';
    const char cos = 'c';
    const char tan = 't';
    
    string f;
    ...
    const string sin = "sin";
    const string cos = "cos";
    const string tan = "tan";
    
        #include <iostream>
        #include <string>
    
        using namespace::std;
    
    
    
    int funktions_wahl(char funktion, int genauigkeit, int x)
     {
    char sin='s';
    char cos='c';
    char tan='t';
    if (funktion==sin) {
        cout << "sin von " << x << " =" << 2*x*genauigkeit << "\r"; //Wie sin als fkt hier benutzen? 2x vorübergehend.
    }
    else if (funktion==cos) {
        cout << "cos von " << x << " =" << 4*x*genauigkeit << "\r"; //Gleiches Spiel wie oben.
    }
    else if (funktion==tan) {
        cout << "tan von " << x << " =" << 8*x*genauigkeit << "\r"; //Gleiches Spiel wie oben;
    }
    else {
        cout << "Keine gültige Variable eingegeben\r";
    }
    return 0;
    }
    
    
    
    
    
    int main() {
    char f;
    int g=0;
    int x=0;
    cout << "Taschenrechner 1.0\r" << "Bitte Funktion eingeben: ";
    cin >> f;
    cout << "Genauigkeit wählen: ";
    cin >> g;
    cout << "x wählen: ";
    cin >> x;
    funktions_wahl(f, g, x);