[英]C++ Program reading '*' as 'a'
I am working on a short little calculator program that takes command line arguments and performs the relevant operations. 我正在研究一个简短的小型计算器程序,该程序需要命令行参数并执行相关操作。 It all works fine except with multiplication.
除了乘法之外,一切都很好。
When I type "./calc 3 * 3" in the command line, the program spits out an error, and when I "cout" the char it stored as operator it says "a". 当我在命令行中键入“ ./calc 3 * 3”时,程序会吐出一个错误,当我“ cout”存储为运算符的字符时,它会显示“ a”。 All the other operators work fine.
其他所有操作员都可以正常工作。
Can you guys figure out why it's not accepting '*' as a char? 你们能弄清楚为什么它不接受'*'作为字符吗?
Here is the code and some sample output: 这是代码和一些示例输出:
#include <iostream>
#include <cstdlib>
using namespace std;
const int MINIMUM_ARGUMENTS = 4; //must have at least 4 arguments: execution command, a first number, the operator, and a second number;
double Add(double x, double y);
double Subtract(double x, double y);
double Multiply(double x, double y);
double Divide(double x, double y);
int main(int argc, char* argv[])
{
if (argc < MINIMUM_ARGUMENTS) //"less than" because counting execution command as first argument
{
cout << "\nMust have at least " << MINIMUM_ARGUMENTS << " arguments (including execution command)." << endl;
}
else
{
double num1 = atof(argv[1]); //stores first argument as double
char operation = *argv[2]; //stores second argument (operator) as char
double num2 = atof(argv[3]); //stores third argument - second number
double result = 0; //will store result of arithmetic
cout << '\n';
switch (operation) //determines which function to call based on the char (operator) argument
{
case '+':
result = Add(num1, num2);
cout << result << endl;
break;
case '-':
result = Subtract(num1, num2);
cout << result << endl;
break;
case '*':
result = Multiply(num1, num2);
cout << result << endl;
break;
case '/':
result = Divide(num1, num2);
cout << result << endl;
break;
default:
cout << "Error." << endl;
}
}
cin.clear();
cout << "\nPress enter to quit." << endl;
cin.ignore();
return 0;
}
double Add(double x, double y)
{
return x + y;
}
double Subtract(double x, double y)
{
return x - y;
}
double Multiply(double x, double y)
{
return x * y;
}
double Divide(double x, double y)
{
return x / y;
}
Sample Output: 样本输出:
-bash-4.1$ ./calc 10 - 5
5
Press enter to quit.
-bash-4.1$ ./calc 4 + 9
13
Press enter to quit.
-bash-4.1$ ./calc 10 / 2
5
Press enter to quit.
-bash-4.1$ ./calc 5 * 5
Error.
Press enter to quit.
The * sign is used by OS (bash here) to select all files from current dir. OS使用*符号(此处为重击)从当前目录中选择所有文件。 so change your sign.
所以改变你的标志。
I suggest 'x' 我建议'x'
While sending *
use single quotes '*'
or escape it using \\*
. 发送
*
使用单引号'*'
或使用\\*
对其进行转义。
Simply *
will not work because on command line *
means "everything in the current directory". *
根本无法使用,因为在命令行上*
表示“当前目录中的所有内容”。
*
is a wildcard that is used to select all . *
是用于选择全部的通配符。 Generally used in selecting all files in a directory . 通常用于选择目录中的所有文件。
For example : 例如 :
delete *.exe
: This command would delete all the files with extension ".exe " from the folder . delete *.exe
:此命令将从文件夹中删除所有扩展名为“ .exe”的文件。
If used in bash, *
would be treated as a Wildcard, so you should consider some other sign to perform the required operation. 如果在bash中使用,
*
将被视为通配符,因此您应考虑使用其他符号来执行所需的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.