我正在使用c ++制作计算器,但编译器发出错误

Shi*_*Jha -8 c++

我是c ++的新手.最近我完成了所有循环(在我的在线教程中给出).

我决定创建一个计算器,它首先从用户那里获取一个函数

  1. A(总和)
  2. S(减法)
  3. M(用于乘法)

我相信你会帮助我.但编译器给出了一些错误.(DEV-C++).我遇到了很大麻烦.

//The program begins here
#include <iostream>

#include <define.h>    //This is my own header file
#include <string>
using namespace std;

int main(){


int x;


int y;


char z;


char a,s,m,d,c;
    do 
    {cout<<"Enter the function below :"<<NEWLINE;
    cout<<"Following are the possible functions:  "<<"1."<<"a-add"<<NEWLINE<<"2."<<"s-subtract"<<NEWLINE<<"3. "<<"m-multiply"<<NEWLINE<<"4. "<<"d-divide"<<NEWLINE;
cin>>z;
while (z!=c);   
    }
    if (z=a){    /*here is the error place. it tells to give a "while" `before '(' token. (but I dont know why)...`*/
      cout<<"Please enter your first digit to be added"<<NEWLINE;
      cin>>x;
        cout<<"Your first digit is "<<x<<NEWLINE<<"Please enter 2nd digit to be added";
        cin>>y;
        cout<<"You entered "<<y<<"."<<"The sum of"<<x<<" and "<<y<<" is "<<x+y;

    else if (z=s){        /*Here It tells me that "else without a previous if".             //BUT I have given IF before this.*/
        cout<<"Please enter the first digit "<<NEWLINE;
        cin>>x;
        cout<<"You entered "<<x<<" Please enter 2nd digit: "<<NEWLINE;
        cin>>y;
        cout<<"Your equation is"<<x<<"-"<<y<<"="<<x-y;
    }
    else if(z=m){
        cout<<"Enter the first digit"<<NEWLINE;
        cin>>x;
        cout<<"Your 1st digit is"<<x<<". Enter the 2nd digit"<<NEWLINE;
        cin>>y;
        cout<<"Your equation is"<<x<<"+"<<y<<"="<<x+y;
    }
    }
    cin.ignore();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

seh*_*ehe 5

你的时间错了.这是编译器告诉你的:

test.cpp|23 col 8| error: expected ‘while’ before ‘(’ token
||      if (z = a) { // here is the error place. it tells to give a "while" before '(' token. (but I dont know why)...
Run Code Online (Sandbox Code Playgroud)

所以,解决它:

do {
    cout << "Enter the function below :" << NEWLINE;
    cout << "Following are the possible functions:  "
         << "1."
         << "a-add" << NEWLINE << "2."
         << "s-subtract" << NEWLINE << "3. "
         << "m-multiply" << NEWLINE << "4. "
         << "d-divide" << NEWLINE;
    cin >> z;
} while (z != c);
Run Code Online (Sandbox Code Playgroud)

还有更多问题:

if (z = a) { // here is the error place. it tells to give a "while" before '(' token. (but I dont know why)...
Run Code Online (Sandbox Code Playgroud)

应该 z==a


    else if (z = s)
Run Code Online (Sandbox Code Playgroud)

应该是} else if (z==s)......


更多的事情是错的.a,s,m,c从来没有初始化...你的意思是'a','s'等?

我最好的选择,至少 - 编译:

#include <iostream>
//#include <define.h> //This is my own header file
#include <string>

using namespace std;
int main() {
    char z = ' ';

    do {
        std::cout << "Enter the function below :"
            << "\n";
        std::cout << "Following are the possible functions:  "
            "1. a-add\n"
            "2. s-subtract\n"
            "3. m-multiply\n"
            "4. d-divide\n";
        std::cin >> z;
        if (z == 'a') { // here is the error place. it tells to give a "while" before '(' token. (but I dont know why)...
            std::cout << "Please enter your first digit to be added"
                << "\n";
            int x, y;
            std::cin >> x;
            std::cout << "Your first digit is " << x << "\n"
                << "Please enter 2nd digit to be added";
            std::cin >> y;
            std::cout << "You entered " << y << "."
                << "The sum of" << x << " and " << y << " is " << x + y;
        } else if (z == 's') { /*Here It tells me that "else without a previous if".             //BUT I have given IF before
                               this.*/
            std::cout << "Please enter the first digit "
                << "\n";
            int x, y;
            std::cin >> x;
            std::cout << "You entered " << x << " Please enter 2nd digit: "
                << "\n";
            std::cin >> y;
            std::cout << "Your equation is" << x << "-" << y << "=" << x - y;
        } else if (z == 'm') {
            std::cout << "Enter the first digit"
                << "\n";
            int x, y;
            std::cin >> x;
            std::cout << "Your 1st digit is" << x << ". Enter the 2nd digit"
                << "\n";
            std::cin >> y;
            std::cout << "Your equation is" << x << "+" << y << "=" << x + y;
        }
    } while (z != 'c');

    std::cin.ignore();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

  • @ShivamProgramer请阅读答案. (3认同)
  • @ShivamProgramer http://coliru.stacked-crooked.com/a/235921fc0941​​871d - 另外,我想提醒您,我们没有义务"解决您的问题".事实上,你有义务一次创建一个问题并修复它们,然后你得到的代码片段比瘟疫中的蟋蟀有更多的错误. (2认同)