简体   繁体   English

在 C++ 中编程 If else 语句

[英]Programming In c++ If else statement

I was writing a c++ code but it returns error.我正在编写一个 C++ 代码,但它返回错误。 I have just started c++ and i don't know how to rectify it this is the error :我刚刚开始使用 C++,我不知道如何纠正它,这是错误:

tempCodeRunnerFile.cpp:5:1: error: '::main' must return 'int'
     5 | void main()
       | ^~~~
tempCodeRunnerFile.cpp: In function 'int main()':
tempCodeRunnerFile.cpp:8:5: error: 'clrscr' was not declared in this scope
    8 |     clrscr();
      |   

this is my code: I want to input two numbers and return "Variable one is greater" if first variable is greater and if second is greater then "second variable is greater"这是我的代码:如果第一个变量更大,我想输入两个数字并返回“变量一个更大”,如果第二个更大,则“第二个变量更大”

#include<iostream>
#include<conio.h>
using namespace std;

void main()
{

    clrscr();
    int vari,vari2;

    cout<<"Enter a Variable: ";
    cin>>vari;
    cout<<"Enter Another Variable: ";    
    cin>>vari2;

    if (vari>vari2){
        cout<<"Variable 1 was greater";
    }
    else if (vari=vari2)
    {
        cout<<"Variables Are Equal"; 
    }
    else
    {
        cout<<"Variable 2 is Greater";
    }



    getch();
}

clrscr() is an obsolete function that clears the console screen that was used in some old Borland Turbo C++ compilers. clrscr()是一个过时的函数,用于清除一些旧的 Borland Turbo C++ 编译器中使用的控制台屏幕。 Your program should work also without it, so you can delete it.您的程序也应该在没有它的情况下运行,因此您可以将其删除。

There's another error, though:不过还有一个错误:

else if (vari=vari2)

This does not do comparison between those variables, instead it assigns vari2 to vari1.这不会在这些变量之间进行比较,而是将 vari2 分配给 vari1。 Fix it like this:像这样修复它:

else if (vari==vari2)

vari=vari2 is an assignment, and it's value is actualy the value of vari after the assignment, converted to bool. vari=vari2是一个赋值,它的值vari=vari2是赋值后的vari=vari2的值,转换为bool。

The statement should be vari == vari2语句应该是vari == vari2

Before going to answer any of your error, I would like to know if you were running the code in VS code for only a selective statements?在回答您的任何错误之前,我想知道您是否仅针对选择性语句在 VS 代码中运行代码? Because the tempCodeRunnerFile.cpp file only generates in VS code when you run a specific section of code instead of whole code file using Code Runner extension.因为tempCodeRunnerFile.cpp文件仅在您运行特定代码部分而不是使用 Code Runner 扩展的整个代码文件时在 VS 代码中生成。 So that may be your first problem.所以这可能是你的第一个问题。

Let's solve your errors now.现在让我们解决您的错误。 The first error error: '::main' must return 'int' 5 | void main() | ^~~~第一个错误error: '::main' must return 'int' 5 | void main() | ^~~~ error: '::main' must return 'int' 5 | void main() | ^~~~ error: '::main' must return 'int' 5 | void main() | ^~~~ shows the main function must return an integer value for your code. error: '::main' must return 'int' 5 | void main() | ^~~~显示主函数必须为您的代码返回一个整数值。 So instead of using void main() use int main() .因此,不要使用void main()使用int main() Don't forget to add return 0;不要忘记添加return 0; statement at the end.最后声明。 Your first error is solved now.您的第一个错误现已解决。

About your second error, I don't have any clear answer but if I guess it's due to using clrscr() .关于你的第二个错误,我没有任何明确的答案,但如果我猜这是由于使用clrscr() As much I know , clrscr is a Borland TurboC++ non-standard function(very old), and isn't present in other compilers, so it's may be due to that.据我所知, clrscr是 Borland TurboC++ 非标准函数(非常老),并且在其他编译器中不存在,所以可能是由于这个原因。 To clear the screen in modern compilers use the system("cls");要清除现代编译器中的屏幕,请使用system("cls"); code which is a part of stdlib.h header file.代码是stdlib.h头文件的一部分。 (NOTE: use #include<stdlib.h> in beginning) (注意:在开始时使用#include<stdlib.h>

Apart these errors another thing wrong with your code is that you used vari=vari2 which is not done for equality check.除了这些错误之外,您的代码的另一个错误是您使用了vari=vari2 ,这不是用于相等性检查。 So do vari==vari2 .所以做vari==vari2

That's all I know.这是我知道的全部。

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

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