C++ Visual Studio Access is Denied error - c++

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.

Related

Error in Visual Code while running a C++ Code

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?

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.

Enabling `-std=c++14` flag in Code::Blocks

I have installed Code::Blocks for Windows and want to compile C++14 code like generic lambdas but the binary version of Code::Blocks that I've installed from codeblocks.org doesn't support the flag -std=c++14.
How do I update the compiler and enable -std=c++14 flag for Code::Blocks?
To compile your source code using C++14 in Code::Blocks, you, first of all, need to download and install a compiler that supports C++14 features.
Here’s how you can do it on Windows:
Download MinGW from here (particular build) or from official site to choose options
Extract it to for example: C:\ (result will be C:\MinGW)
Open Code::Blocks
Go to Settings => Compiler.
Go to “Toolchain Executables”.
In the top field “Compiler’s installation directory”, change the directory to the one where you extracted the compiler. E.g C:\MinGW.
Change all the necessary files under “Program Files” to match the files under C:\MinGW\bin:
Before you hit “OK”, go to the leftmost tab “Compiler settings”.
Select “Compiler Flags”.
For simplicity, right-click in the list somewhere and select “New Flag”:
Type in the following and click "OK", and tick the box of the flag you just created:
Lastly, you need to specify the debugger path. Go to "Settings" => "Debugger", click "Default" on the left-hand side and enter the new full path of the executable:
Now, try to compile a program with C++14 features:
#include <iostream>
#include <string>
using namespace std;
auto main() -> int
{
auto add_two([](auto x, auto y){ return x + y; });
cout << add_two("I"s, "t"s) << " works!" << endl;
}
May a humble newbie make one small suggestion? A small modification to test C++14 code, to allow resulting .exe file to be run independently of the IDE it was created in, slightly modified test program follows:
#include <iostream>
#include <string>
using namespace std;
auto main() -> int
{
auto add_two([](auto x, auto y){ return x + y; });
cout << add_two("I"s, "t"s) << " works!" << endl;
cout << "press enter to continue..." << endl;
cin.ignore(10, '\n');
cin.get();
}
Thank you all, peace to all fellow coders, especially Igor Tandetnik.

Can't run a very basic code in Visual Studio 2013 - Operator ">>" error / build error

I have always used Linux Text Editor for C/C++ coding.
I'm totally new to Visual Studio. I downloaded Visual Studio 2013 and wrote a very basic code.
Before I tell you my problem, I think I should mention how I opened the new project so that it's not the matter of opening a wrong new project!
So here it is:
File > New > Project > Visual C++ > Win32 Console Application > ok > (Window appears saying "Welcome to Win32 Application wizard") > Next > checked the box saying "Empty Project" under Additional options > Finish
So then, I just right click on the "Source Files" > Add > New Item > C++ File(.cpp) > Add
Then my desired window appears so that I can write some code that should work!!
I wrote the following (very basic code) in Visual Studio 2013:
#include <iostream>
using namespace std;
int main()
{
cout << "What's your name?";
string name;
cin >> name;
cout << "Enter your age: ";
int age;
cin >> age;
cout << "In a decade, you will be " << age + 10 << "years old!" << endl;
return 0;
}
To run it, when I press "Local Windows Debugger", a box appears saying:
"There were build errors. Would you like to continue and run the last successful build?"
I pressed "Yes"
Then it shows another box saying "Unable to start the program....the system cannot find the file specified"
The following errors I get to see in the error list:
no operator ">>" matches the operands
operand types are: std::istream >> std::string
I would appreciate your help.
Thanks.
You forgot to include C++'s string library.
Adding
#include <string>
at the beginning of your file should make it work.
You can either do:
std::string name;
or include string class at the top:
#include <string>
You did not include string class like #include<string>
Use cin.getline(name) to take input in string otherwise it will take just a word as input, because ">>" operator ignore white spaces (space, tab etc..)
When you find this Box Message: "There were build errors. Would you like to continue and run the last successful build?" Do not press YES because it uses the last error free build to execute your program.

C++ console program closing before completion.

My Program is a simple sum finder. the code of which I will post at the end.
But it asks for the first number. Upon entering it asks you for a second number.
After entering the second number, the console window closes before showing the results. When I first build and tested in Visual C++ 2010 it ran fine, but this problem only occurs when running the .exe from the build location. Any tips?
Here is the code If testing yourself please re-assemble:
#include "stdafx.h" // for Visual Studio users
#include <iostream>
int main()
{
using namespace std;
int no1, no2, sum ;
cout << "\nEnter the first number = " ;
cin >> no1 ;
cout << "\nEnter the Second number = " ;
cin >> no2 ;
sum = no1 + no2 ;
cout << "\nThe sum of "<< no1 <<" and "<< no2 <<" = "<< sum ;
return 0 ;
}
One way to keep the console open until you're satisfied with it is to add a cin at the end of it - Before closing it will then wait for the user to close it or to enter a line of input.
That's because the window closes when the program is finished running. Use std::cin.get() to keep the window open while it waits for input:
int main()
{
// ...
std::cin.get(); // keep the window open; wait for a character
return 0;
}
Add cin.get() before return 0;
Console applications are actually meant to be executed directly from the console. If you run them directly on it, after the program is done you'll be left with your console window, containing all of the output given by your program. Plus, you'll get a little more used to using the command prompt, which is pretty cool and useful sometimes. :-D
If you have any doubts about how to run a program from the console on a windows environment, take a look at this answer (Compiling C-code from the Command Prompt in Windows?) or maybe this one (How to run a c program using command prompt).