我的计划有什么问题

编程入门 行业动态 更新时间:2024-10-25 08:27:22
本文介绍了我的计划有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

#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

推荐答案

你有一个 char 数组( F [10] )。将这些数组与常量字符串进行比较需要使用C函数 strcmp 。 使用时 You have a char array (f[10]). Comparing such arrays with a constant string requires using the C function strcmp. When using if(f=="add")

你要比较两个永远不会相同的指针。 在你的情况下,最好使用 std :: string 而不是支持使用 == 进行比较运算符或 string :: compare - C ++ Reference [ ^ ]功能。然后使用 getline(string) - C ++ Reference [ ^ ]读取字符串对象的输入。

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.

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。 调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。 调试器 - 维基百科,免费的百科全书 [ ^ ] 掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ] 调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 /> 当代码不做ex的时候你已经接近了一个错误。 You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect. The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect. Debugger - Wikipedia, the free encyclopedia[^] Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^] The debugger is here to show you what your code is doing and your task is to compare with what it should do. When the code don't do what is expected, you are close to a bug.

更多推荐

我的计划有什么问题

本文发布于:2023-10-22 23:00:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么   计划

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!