c++ code not working

i tried submitting a code for a program which is running perfectly fine on turbo c++ but doesent seems to work here
this is the code
#include<iostream.h>
#include<conio.h>
void main()
{
int n;
for( ; ; )
{
cin>>n;
if(n==42)
break;
cout<<n;
}
getch();
}

pls suugest a solution :slight_smile:

3 Likes
  1. Use iostream instead of iostream.h and place a line
    using namespace std; after the header file declaration.

  2. Do not use conio.h as it is not a standard header file.

  3. Besides, getch() is not needed as its function is to see output on the screen, which is not needed here.

  4. The main function return type should be int instead of void

  5. You also need to use endl for newline cout<<n<<endl as the numbers are printed on newlines.
    Here is the corrected solution link: CodeChef: Practical coding for everyone

3 Likes

@mongasarthak , the above mentioned would definitely help you , but to avoid such header files related queries you can compile your code on online compliers , for example if you submit this code on ideone clearly it gives an error ,and thus you would know from it , that this header file which you added wouldn’t work on code chef too , so you can thus modify your code accordingly .

These error’s are very common initially so don’t worry much about it .

All the Best and happy coding :slight_smile: .

7 Likes

I too faced the same problem initially . :slight_smile:

use iostream instead of iostream.h and include using namespace std; as the compiler may not be able to compile the old version and also remove conio.h and hence the getch().