我正在使用c++制作计算器,但编译器给出了一个错误
I was making a calculator using c++ but the compiler is giving an error
我是c++的新手。最近我完成了所有的循环(在我的在线教程中给出)。
我决定创建一个计算器,它将首先从用户获取一个函数
- a (for sum)
- S(用于减法)
- M(用于乘法)
//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;
}
你的while放错地方了。这是编译器告诉你的:
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)...
所以,修复它:
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)...
应为z==a
else if (z = s)
应该是} 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-addn"
"2. s-subtractn"
"3. m-multiplyn"
"4. d-dividen";
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;
}
相关文章:
- C++,我收到一个无法理解的编译器错误
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 删除映射和分割错误中的一个过去结束元素
- 为什么我的 heap.h 文件给我一个LNK2001错误?
- 写一个分数类,我无法弄清楚这些错误
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 一个标头库中的错误
- 给定一个整数数组,需要在Max_Heap上运行操作。得到错误"segmentation fault",有什么想法吗?(C++)
- 使用Visual Studio在虚幻引擎中创建一个新的类c ++给了我太多的错误
- 我正在尝试制作一个自平衡机器人,但编译时存在错误。我不知道如何解决它
- 试图修复一个错误,该错误不会让我开始编程其余部分
- MSVC 无法根据模板参数进行数学运算,这是一个错误吗?
- 我正在尝试一个傻瓜 C++ 练习,我遇到了一个错误,说类 'GraduateStudent' 没有任何名为 'advisor' 的字段
- 零四元数和任何向量都不为零的特征积,这是一个错误吗?
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- 在使用堆栈为下一个最大数字编写代码时面临 SIGSEGV(分段错误)
- LNK2019错误一个DLL链接到MFC DLL
- 错误:一个声明中有多个类型
- 带有两个运算符的C++11模糊错误(一个左值-第二个右值).底部的