它應該是一個函式,它根據所選的運算子查找兩個數字的和、差等,但是當我strcmp()用來檢查所選的運算子時,我得到了錯誤expected 'const char *' but argument is of type 'char **'
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <string.h>
int calc(int ran1,int ran2,char op){
if(strcmp(op, " ")==0){
return ran1 ran2;
}
if(strcmp(op, "-")==0){
return ran1-ran2;
if(strcmp(op, "*")==0){
return ran1*ran2;
}
if(strcmp(op, "/")==0){
return ran1/ran2;
}
}
}
int main(){
int ran1=25;
int ran2=5;
char op=" ";
printf("%d", calc(ran1, ran2, op));
}
uj5u.com熱心網友回復:
似乎這個錯誤資訊
預期為“const char *”,但引數的型別為“char **”
不對應于呈現的代碼,因為程式中沒有char **使用該型別的運算式。
由于大括號的放置無效,函式中也存在邏輯錯誤
int calc(int ran1,int ran2,char op){
if(strcmp(op, " ")==0){
return ran1 ran2;
}
if(strcmp(op, "-")==0){
return ran1-ran2;
if(strcmp(op, "*")==0){
return ran1*ran2;
}
if(strcmp(op, "/")==0){
return ran1/ran2;
}
}
}
不過對于初學者來說這個初始化
char op=" ";
是不正確的。看來你的意思
char op = ' ';
也就是說,您需要使用整數字符常量而不是字串文字來初始化該op型別的物件。char' '" "
由于引數op的型別為 char
int calc(int ran1,int ran2,char op){
那么它可能不會在一個呼叫中使用strcmp類似
if(strcmp(op, " ")==0){
==在 this 和其他類似的 if 陳述句中只使用相等運算子就足夠了
if ( op == ' ' )
考慮到您可以使用 switch 陳述句代替 if 陳述句。例如
switch ( op )
{
case ' ':
//...
case '-':
//...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/357597.html
