简体   繁体   English

在我的计算器程序中使用if / else if

[英]Using if/else if in my calculator program

I'm working on a project by where I have to create a simple program that works based of the user input. 我正在一个项目上,根据该项目,我必须创建一个基于用户输入工作的简单程序。 I've gone with a basic calculator but I'm having trouble getting my if/else if statements to work. 我已经使用了基本的计算器,但是我的if / else if语句无法正常工作。 Basically, if the user types in "Addition", I want the program to say "...I will help you with addition!", and so on for whether the user says "Subtraction", "Division", and "Multiplication". 基本上,如果用户键入“加法”,我希望程序说“ ...我将帮助您加法!”,以此类推,以便确定用户是否说“减法”,“除法”和“乘法”。 。

I'm new to this and so this has already taken me hours upon hours, not looking for you to do it for me but to point out my erorrs and advise so that I can learn from it will be great. 我对此并不陌生,所以这已经花费了我数小时的时间,不是想让您为我做,而是指出我的错误并提出建议,以便我可以从中学到东西。

TIA. TIA。

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>

using namespace std;

//user inputs what he needs help with/program output
char Inpsum()
{
cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of assistance today?" << endl;
char inpsum[20];
cin >> inpsum;
char output;
if (inpsum == "Addition")
{
    cout << "Great! I'll help you with addition!" << endl;
}
else if (inpsum == "Subtraction")
{
    cout << "Great! I'll help you with subtraction!" << endl;
}
else if (inpsum == "Division")
{
    cout << "Great! I'll help you with division!" << endl;
}

else if (inpsum == "Multiplication")
{
    cout << "Great! I'll help you with multiplication!" << endl;
}


return 0; 

REST OF CODE 其余代码

//addition function
void Add() {
float add1, add2;
cout << "Please enter two values you want added together" << endl;
cin >> add1;
cin >> add2;
cout << "The answer is: " << (add1 + add2) << endl;
}

//subtraction function
void Subt() {
float subt1, subt2;
cout << "Please enter two values you want subtracted" << endl;
cin >> subt1;
cin >> subt2;
cout << "The answer is: " << (subt1 - subt2) << endl;
}

//division function
void Div()
{
    float div1, div2;
    cout << "Please enter two values you want divided" << endl;
    cin >> div1;
    cin >> div2;
    cout << "The answer is: " << (div1 / div2) << endl;
}

//multiplication function
void Mult() {
float mult1, mult2;
cout << "Please enter two values you want multiplacted" << endl;
cin >> mult1;
cin >> mult2;
cout << "The answer is: " << (mult1 * mult2) << endl;
}



int main()
{
Inpsum(); //user inputs what they want help with
Add();
Subt();
Div();
Mult();

    return 0 ; 
}

This code is all wrong you need to learn about C++ correctly first, here is the corrected code 这段代码是您首先需要正确了解C ++的所有错误,这是正确的代码

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include<string>
using namespace std;

//addition function
float Add(float add1, float add2)
{

    return (add1 + add2);
}
//subtraction function
float Subt(float subt1, float subt2) {

    return (subt1 - subt2);
}

//division function
float Div(float div1, float div2)
{

    return (div1 / div2);
}
//multiplication function
float Mult(float mult1, float mult2)
{

    return (mult1 * mult2);
}
void input(float &num1, float &num2)
{
    cout << "\nEnter First Number : ";
    cin >> num1;
    cout << "Enter Second Number : ";
    cin >> num2;
}
//user inputs what he needs help with/program output
void Inpsum()
{
    cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of assistance today?" << endl;
    float num1;
    float num2;
    string inpsum;
    cin >> inpsum;
    if (inpsum == "adding")
    { //if user enters "adding"
        cout << "Great!, I will help you with " << (inpsum) << endl;
        input(num1, num2);
        cout << "\nAnser Is " << Add(num1, num2);
    }//then output = "...i will help with adding"
    else if (inpsum == "subtraction") //otherwise, if user enters "subtraction"
    {
        cout << "Great!, I will help you with " << (inpsum) << endl; //then output = "...i will help with subtraction"
        input(num1, num2);
        cout << "\nAnser Is " << Subt(num1, num2);
    }
    else if (inpsum == "division") //if user enters "division" 
    {

        cout << "Great!, I will help you with " << (inpsum) << endl; ////then output = "...i will help with division
        input(num1, num2);
        cout << "\nAnser Is " << Div(num1, num2);
    }
    else if (inpsum == "multiplication") //if user enters "muliplication"
    {
        cout << "Great, I will help you with " << (inpsum) << endl; ////then output = "...i will help with multiplication"
        input(num1, num2);
        cout << "\nAnser Is " << Mult(num1, num2);
    }
    else
    {
        cout << "Enter A Correct Mathematical Operation";
    }
}
    int main()
    {
        Inpsum(); //user inputs what they want help with
        cout<<endl;
        system("pause");
        return 0;
    }

First of all, instead of using char array, use std::string . 首先,不要使用char数组,而要使用std::string

Secondly, if-else statements have syntax errors. 其次, if-else语句存在语法错误。

Basic structure of if-else statements is like if-else语句的基本结构类似于

if(condition)
{
   //code
}
else
if(condition)
{
  //code
}

More on if-else statements in C++ 有关C ++中if-else语句的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM