gpt4 book ai didi

c++ - 程序跳过用户输入

转载 作者:行者123 更新时间:2023-11-28 00:05:38 25 4
gpt4 key购买 nike

我正在用 c 编写一个简单的计算器,用户输入两个数字,然后选择对它们应用的操作。(Mul、Add、Div、Sub)该程序可以工作,除了它跳过了它应该采取的部分操作数的用户输入。我做错了什么

#include<stdio.h>
#include<sstream>
#include<iostream>
#include<conio.h>
#include<string.h>

using std::cout;
using std::cin;

using namespace std;

int main() {

char Operative[100];

int a;
int b;
int c;


printf("Enter First Number\n");
scanf("%d",&a);

printf("Enter First Number\n");
scanf("%d",&b);

printf("\nPlease Enter Operation(M,A,D,S)");
gets(Operative);

//getline(cin,Operative);

if (Operative == "M")
{
c = a*b;
printf("Multiplication value is %d",c);

}

else if (Operative == "A")
{
c = a+b;
printf("Addition value is %d",c);

}

else if (Operative == "D")
{
c = a/b;
printf("Division value is %d",c);

}

else if (Operative == "S")
{
c = a-b;
printf("\nSubtraction value is %d",c);

}

最佳答案

使用strcmp 比较字符串值。 == 比较指针:

if (strcmp (Operative,  "M") == 0) ...

关于c++ - 程序跳过用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35812087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com