基本控制台计算器(在一个变量中存储字符串)C++

问题描述:

我想在C++中创建一个基本控制台计算器。我在将一个字符串存储在cin命令的变量中时遇到了一些问题。基本控制台计算器(在一个变量中存储字符串)C++

下面是一些澄清程序:

#include <iostream> 
using namespace std; 

int main() 
{ 
    string type_cal; 

    cout << "Please enter the type of calculation you would like to use: \n"; 
    cout << "1. Addition \n"; 
    cout << "2. Subtraction \n"; 
    cout << "3. Multiplication \n"; 
    cout << "4. Division \n \n"; 

    cin >> type_cal; 

    if (type_cal = "Addition" or "1") 
    { 
     int a; 
     int b; 
     int sum; 

     cout << "Please enter a number to add: \n"; 
     cin >> a; 

     cout << "Please enter another number: \n"; 
     cin >> b; 

     sum = a + b; 

     cout << "The sum of those numbers is: " << sum << endl; 

     return 0; 
    } 
} 

目前我在另外相,因为我最近就遇到了这个问题。快速回答将不胜感激,谢谢!

+0

对于初学者来说,我的编译器给了我[一个有用的警告(HTTP://coliru.stacked-crooked的.com /一个/ 0084edc5fb537aa7)。 – chris

+0

一个问题是'='不是'=='。一个编译器警告应该能够解决这个问题,但是一个好习惯总是把常量放在** left **的任何比较中,这样如果你错误地输入'=',它总是会编译失败。也就是说,如果你犯了同样的错误,''1“== type_cal'就不能编译。 – Davislor

+0

看起来您已成功从'cin'中读取一个字符串,并将其放入'type_cal'中。是什么让你觉得它没有被正确读取? –

if(type_cal =“Addition”或“1”)根本没有意义。

if(type_cal == "Addition" || type_cal == "1") { 
} 
+0

好的,谢谢你,我忘记了如何使用or操作符。 – JDH

好的,我发现这个问题,或实际上用作||。在c + +(感谢aerkenemesis),和=是不一样的==这意味着等于(另一个感谢Lorehed)。程序现在工作正常。

+0

这不是唯一的问题。 'x == y || z'是错误的(并且通常源于英语中所有事物的常见错误);你需要'x == y || x == z'。 –

+0

谢谢!我一直在试图找出这个问题一天吧! – JDH

+0

不知道为什么,因为当时每个人都在评论中告诉你,更不用说其他答案(你当时评论过)。 –

对于那些谁是好奇,这里是我的简单的计算器的新修订版:

#include <iostream> 
using namespace std; 

float addition(); 
float subtraction(); 
float multiplication(); 
float division(); 

int main() 
{ 
    string type_cal; 

    cout << "Please enter the type of calculation you would like to use: \n"; 
    cout << "1. Addition " << endl; 
    cout << "2. Subtraction " << endl; 
    cout << "3. Multiplication " << endl; 
    cout << "4. Division" << endl << endl; 

    cin >> type_cal; 

    if(type_cal == "Addition") 
    { 
     addition(); 
    } 

    if(type_cal == "Subtraction") 
    { 
     subtraction(); 
    } 

    if(type_cal == "Multiplication") 
    { 
     multiplication(); 
    } 

    if(type_cal == "Division") 
    { 
     division(); 
    } 

    return 0; 
} 



float addition() 
{ 
    float a; 
    float b; 
    float sum; 

    cout << "Please enter a number to add: " << endl; 
    cin >> a; 

    cout << "Please enter another number: " << endl;; 
    cin >> b; 

    sum = a + b; 

    cout << "The sum of those numbers is: " << sum << endl; 
} 




float subtraction() 
{ 
    float c; 
    float d; 
    float difference; 

    cout << "Please enter a number to subtract: \n"; 
    cin >> c; 

    cout << "Please enter another number: \n"; 
    cin >> d; 

    difference = c - d; 

    cout << "The difference of those numbers is " << difference << endl; 
} 




float multiplication() 
{ 
    float e; 
    float f; 
    float product; 

    cout << "Please enter a number to multiply: \n"; 
    cin >> e; 

    cout << "Please enter another number: \n"; 
    cin >> f; 

    product = e * f; 

    cout << "The product of those numbers is " << product << endl; 
} 




float division() 
{ 
    float g; 
    float h; 
    float quotient; 

    cout << "Please enter a number to divide: \n"; 
    cin >> g; 

    cout << "Please enter another number: \n"; 
    cin >> h; 

    quotient = g/h; 

    cout << "The quotient of those numbers is " << quotient << endl; 
}