本文介绍了我的计划有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>

using namespace std;
class calc
{
    int a,b,c;
public:
    void add()
    {
        cout<<"Enter values";
        cin>>a>>b;
        c=a+b;
        cout<<c;
    }
    void mul()
    {
        cout<<"enter values";
        cin>>a>>b;
        c=a*b;
        cout<<c;
    }
};
int main()
{
    calc obj;
    char f[10];
    cout<<"Select from the option 1. add, 2. mul";
    cin>>f;
    if(f=="add")
        obj.add();
    else if(f=="mul")
        obj.mul();
    else
        cout<<"invalid choice";

        return(0);

}





我的尝试:



我已经执行了这个程序但是当我输入add或mul时它显示无效的选择。

PS我是编程新手。



提前感谢



What I have tried:

I have executed this program but when ever i input add or mul it shows invalid choice.
PS i am new to programming.

thanks in advance

推荐答案

if(f=="add")



你要比较两个永远不会相同的指针。



在你的情况下,最好使用 std :: string 而不是支持使用 == 进行比较运算符或 []功能。然后使用 []读取字符串对象的输入。


you are comparing two pointers which will never be identical.

In your case it would be better to use a std::string instead which supports comparing using the == operator or the string::compare - C++ Reference[^] function. Then use getline (string) - C++ Reference[^] to read the input into the string object.



这篇关于我的计划有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:58
查看更多