程序将整数乘以10 在编程的第3章中:使用C++的原理和实践要求你“编写一个程序,它接受一个操作,后面跟着两个操作数,并输出结果。例如:”

程序将整数乘以10 在编程的第3章中:使用C++的原理和实践要求你“编写一个程序,它接受一个操作,后面跟着两个操作数,并输出结果。例如:”,c++,C++,这就是我所做的 #include<iostream> #include<cmath> #include<string> #include<vector> #include<algorithm> using namespace std; // This little program does addition, substraction, multiplication, and division as long as the numbe

这就是我所做的

#include<iostream>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

// This little program does addition, substraction, multiplication, and division as long as the number of operators doesn't exceed two.

int main()

{

cout << "Bonvole enigu du nombrojn post unu el tiuj ĉi matematikaj operaciloj: +,-, *, or /  \n";
cout << "Please enter two numbers preceded by either of these mathematical operators: +,-, *, or /  \n";

string s;
int a, b;
cin >> s >> a >> b;
    if (s == "+") {
        cout << a + b;
        }
    else if (s == "-") {
        cout << a - b;
        }
    else if (s == "*") {
        cout << a * b;
        }
    else (s == "/"); {
        cout << a / b << "\n";
        }
return 0;
}
问题在于:

else (s == "/"); {
    cout << a / b << "\n";
    }

else
子句实际上是一个no-op,因为它只是一个比较,最终的
cout
语句是无条件执行的,与所选择的运算符无关。

如果我在else(s==“/”)之后删除分号(;),我会收到错误消息@localhost~]$g++-std=c++11/home/chetan/Programs/Bjarne/Bjarne_c3.cpp/home/chetan/Programs/Bjarne/Bjarne_c3.cpp:在函数“int main()”中:/home/chetan/Programs/Bjarne/Bjarne_c3.cpp:29:21:错误:预期“;”在“{”标记之前(s==“/”{^
@localhost ~]$ ./a.out 
Bonvole enigu du nombrojn post unu el tiuj ĉi matematikaj operaciloj: +,-, *, or /  
Please enter two numbers preceded by either of these mathematical operators: +,-, *, or /  
+ 2 3 
50
[chetan@localhost ~]$ ./a.out 
Bonvole enigu du nombrojn post unu el tiuj ĉi matematikaj operaciloj: +,-, *, or /  
Please enter two numbers preceded by either of these mathematical operators: +,-, *, or /  
- 3 2 
11
[chetan@localhost ~]$ ./a.out 
Bonvole enigu du nombrojn post unu el tiuj ĉi matematikaj operaciloj: +,-, *, or /  
Please enter two numbers preceded by either of these mathematical operators: +,-, *, or /  
* 2 3 
60
[chetan@localhost ~]$ ./a.out 
Bonvole enigu du nombrojn post unu el tiuj ĉi matematikaj operaciloj: +,-, *, or /  
Please enter two numbers preceded by either of these mathematical operators: +,-, *, or /  
/ 3 2 
1
else (s == "/"); {
    cout << a / b << "\n";
    }
else if (s == "/") {
    cout << a / b << "\n";
    }
else {
    (s == "/");
}
cout << a / b << "\n";