Error in Visual Code while running a C++ Code - c++

I was trying to run a C++ program in VS Code.
Program:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int n;
cout<<"enter the no "<<endl;
cin>>n;
int ans=0,i=0;
while(n!=0){
int bit=n&1;
ans= (bit * pow(10, i) ) + ans;
n=n>>1;
i++;
}
cout<<ans;
}
This program isn't working in my pc. But whenever I try to run same program in online compiler on another pc, It works fine.
Like If I enter the input as 6, then it prints 109 instead of 110.
Why is the program not working properly on my PC?
I also tried to debug to check the program but Debugger prints the output to Debug Console even though ExternalConsole is set to true. From Debug Console, It doesn't take the input. Giving the error, Unable to perform this action because process is running.
Why my vscode prints on Debug Console instead of ExternalConsole?

Related

Problem when executing a simple C program in Eclipse IDE

I'm trying to run a simple C program in Eclipse and I am new to Eclipse.
When running the command I see output of the program for a very brief moment in the console, but instead for the program to wait for input, output disappears and and an empty console is shown instead. Is this a problem of the program or something related to the settings of the Eclipse IDE?
Edit: On the console menu there is a terminate button and when I pres it the output of the program shows but it doesn't wait for input and the program is terminated. Any help?
Image:
Here is the code
//The input is a series of numbers and the number X. Find
//how many times the number X is met in the series
#include<stdio.h>
int main(){
int m[20];
int n, X, cnt=0;
printf("Insert maximum length of the series (n<=20)\n");
scanf("n= %d",&n);
printf("Insert X the number you are looking for\n");
scanf("X= %d",&X);
while(n<1||n>20){
printf("Bad input, n should be between 1 and 20\n");
scanf("n= %d",&n);
}
for(int i = 0 ; i < 20 ; i++){
scanf(" %d", &m[i]);
if(m[i]==X)cnt++;
}
printf("X is found %d times\n", cnt);
return 0;
}
and here is the empty console:
Edit: I've tried rewriting the code using cout and cin but then the compiler in Eclipse complains on the line #include<iostream.h> saying that there is an unresolved inclusion. Sounds like a separate question to me.
The answer is here: click
Basically the output of the standard input output is buffered and setvbuf(stdout, NULL, _IONBF, 0); solves the problem.

Error with running graphics porgrams in codeblocks

I am using codeblocks 17.2 for c++ and i am struggling to successfully get an output while running a graphics program. I have added all the linker settings and files successfully but there is still a problem. When i run a basic graphics program, It compiles and runs properly but there is a blank output with this line
Process returned -1073741819 (0xC0000005) execution time : 2.891 s
Press any key to continue.
this is my code
#include<iostream>
#include<graphics.h>
#include<conio.h>
using namespace std;
int main()
{
int gd = DETECT;
int gm;
initgraph(&gd,&gm, "C:\\TC\\BGI");
circle(300,300,50);
closegraph();
getch();
}

C++ Visual Studio Access is Denied error

I installed Microsoft Visual Studio 2017 and am using the Developer Command Prompt to compile and run C++
I wrote a simple program, that has a function that returns Absolute value of a signed int, accepts an input from the user and prints that abs value.
I compiled the code and it worked, ran it once and it worked, then when I typed the program name again to run a second time, I get Access is Denied, unless I recompile, I can only run the program once before I get Access is Denied error...
I didn't have this problem on a simple Hello World program I wrote so not sure what is going on
Any help is appreciated=
my code is simply ---
#include <iostream>
using namespace std;
signed int Abs(signed int x);
int main()
{
signed int n;
cout << "Enter n to get signed int abs value of: ";
cin >> n;
cout << "Signed int: ";
signed int s = Abs(n);
cout << s;
return 0;
}
signed int Abs(signed int x) {
return (x + (x >> 31)) ^ (x >> 31);
}
in my terminal i type
cl /EHsc signedint.cpp
and then
signedint
and it works once
when i type
signedint
a second time i get the error-
Access is denied.
that is all it says.
Solved the problem myself, it was a problem with norton antivirus interfering.
To fix this problem, you have to go into settings in norton antivirus, Antivirus settings, click on "Scans and Risks", and add your project folder to Exclude from Auto Protect, Sonar and Download Intelligence Detection and Exclude from Scans by clicking the configure button next to those two options.

Unable to read input using scanf()

I am trying to run the following simple code to check if scanf() is working.
#include<stdio.h>
int main(){
int num=0;
scanf("%d",&num);//Scanf not working
printf("The number is %d",num);
return 0;
}
However, it gives me an output of zero even though I enter some value for the variable 'num'.
For further clarification, I just installed turbo c++ in my new laptop. Is this causing some problem?

Eclipse C++ Runs on Windows Command Prompt

I wrote a simple C++ code. When I click "Run" on Eclipse, first a Windows command prompt appears with my program. After executing it, Eclipse console starts to work. Is there a way to deactivate command prompt and just use Eclipse console?
#include <iostream>
int main(){
int x = 0,y = 0;
std::cout <<"type2 numbers and I will make a magic trick."<< std::endl;
std::cin >> x >> y;
std::cout<<"Ta daa "<<x<<" and "<<y<<" equals to "<<x+y<<std::endl;
return 0;
}
When I erase std:cin part, command prompt appears and dissappears less than a second so I guess everytime I run my code, this command prompt will appear.