I was writing a c code but it returns error. I have just started c and i don't know how to rectify it this is the error :
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();
}
CodePudding user response:
clrscr()
is an obsolete function that clears the console screen that was used in some old Borland Turbo C compilers. 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. Fix it like this:
else if (vari==vari2)
CodePudding user response:
vari=vari2
is an assignment, and it's value is actualy the value of vari after the assignment, converted to bool.
The statement should be vari == vari2
CodePudding user response:
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? 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. So that may be your first problem.
Let's solve your errors now. The first error error: '::main' must return 'int' 5 | void main() | ^~~~
shows the main function must return an integer value for your code. So instead of using void main()
use int main()
. Don't forget to add 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()
. 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. To clear the screen in modern compilers use the system("cls");
code which is a part of stdlib.h
header file. (NOTE: use #include<stdlib.h>
in beginning)
Apart these errors another thing wrong with your code is that you used vari=vari2
which is not done for equality check. So do vari==vari2
.
That's all I know.