C/C++ Why am I getting a Permission denied error when compiling my c++ code?

  • Thread starter Thread starter chemistry1
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion revolves around a user encountering issues while compiling a C++ program. The initial code fails to execute, leading to a "permission denied" error when trying to create the executable file. Suggestions include checking if the output directory exists and ensuring the user has the necessary permissions. The user is advised to run Visual Studio as an administrator and to check for any lingering processes that might be locking the executable file. The conversation also highlights the importance of including the correct headers, such as <string>, to access the string data type. The user learns that initializing a string variable with a value is unnecessary and that using getline can help capture user input correctly. Ultimately, the problem is resolved by enabling C++11 support in the compiler settings, which was necessary for the code to function properly. The user expresses confusion over how a syntax issue could lead to such significant problems, indicating a learning curve in their programming journey.
chemistry1
Messages
108
Reaction score
0
Hi, I'm having a serious trouble when I compile my code :

Code:
#include <iostream>

using namespace std;

int main()
{
string nom("x");
cout << "Vous avez quel age ?";
cin >> nom;
cout << "Vous avez-vous donc" << nom << "ans?" ;
return 0 ;
}

Here's the resume ; I click on compile&run and my console do open, but it doesn't execute the code.(nothing happens.) I CLOSE the console and re click on compile & run, but this time, an error shows up saying : 1d.exe , cannot open output file bin\Debug\test.exe permission denied. And that's not all, because the .exe has totally disapeared. So, basically, I can't run anything and need to delete my file and recreate a new one. The moment I add this code, the same thing happens. I have no idea what to do. (By the way, I just began learning.)

thanks !
 
Technology news on Phys.org
Try

Code:
    string nom;
    cout << "Vous avez quel age ?";
    getline(cin, nom);
 
chemistry1 said:
Hi, I'm having a serious trouble when I compile my code :

Code:
#include <iostream>

using namespace std;

int main()
{
string nom("x");
cout << "Vous avez quel age ?";
cin >> nom;
cout << "Vous avez-vous donc" << nom << "ans?" ;
return 0 ;
}

Here's the resume ; I click on compile&run and my console do open, but it doesn't execute the code.(nothing happens.) I CLOSE the console and re click on compile & run, but this time, an error shows up saying : 1d.exe , cannot open output file bin\Debug\test.exe permission denied. And that's not all, because the .exe has totally disapeared. So, basically, I can't run anything and need to delete my file and recreate a new one. The moment I add this code, the same thing happens. I have no idea what to do. (By the way, I just began learning.)
What operating system are you running? From what your wrote, I'm guessing you might be using Visual Studio on Windows. If you're getting a permission denied error, try starting Visual Studio as an administrator.
 
Im using windows and code block. And I am the administrator
 
After the program locks up, try running task manager, go to process and see if test.exe is still running. Not likely, but could the issue be the name? Try something other than "test.exe".

I assume that bin\debug\... directory exists?

You could try visual c++ express (it's free) to see if you have the same problem.
 
If bin\debug directory exists, check the properties of this directory to see if you as an admin have permissions (particularly execute permission) there. If not, you can add yourself. That should take care of the permission problem.
 
chemistry1 said:
it doesn't execute the code.(nothing happens.)

"Doesn't execute" doesn't follow form "nothing happens". Can be program does execute, just didn't reach teh code showing anything on the screen.

I CLOSE the console and re click on compile & run, but this time, an error shows up saying : 1d.exe , cannot open output file bin\Debug\test.exe permission denied.

That could mean old process is still running, file is locked and it is not possible to replace it with a new copy of freshly compiled exe.
 
What would be the suggestion ?
 
You already got several, have you tried any of them?

Have you checked if something simple, like

Code:
#include <iostream>

using namespace std;

int main()
{
   cout << "Hello world!";
   return 0 ;
}

works?
 
  • #10
Well, yes it does work, but only if I do not put my code in before. If I do, forget it, nothing works anymore and I need to delete my file and create a new one so things begin working again. (My .exe goes somewhere else.)

Also, I think string nom("x"); might be a problem. I've should have putten char('x') since it's only one character.
 
  • #11
You were told to try to declare it as just
Code:
string nom;

You don't need to init it with a value (be it string "x" or a single character 'x').
 
  • #12
Ok good, I created a new project and did what you asked. It works. ( It didn't in my non working file)

Could you explain me what happened exactly ? ... I'm in the learning phase, so I don't know much. WHy did my things stop working with what I wrote ? thanks
 
  • #13
Don't you have to use also:

Code:
#include <string>

?
 
  • #14
Well, I don't know, haven't seen it. I barelly begun, it's been 2 weeks xD What does it exactly do ?
 
  • #15
Try this

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string nom;
    cout << "Vous avez quel age ? ";
    getline(cin, nom);
    cout << "Vous avez-vous donc " << nom << " ans?" << endl ;
    return 0 ;
}
 
  • #16
Oh, it looks like my problem has stopped. Someone has suggested me that my c++11 was maybe disabled. He told me to go to the compiler settings and put it to std=c++11 and guess what, it worked ! But still, I find it weird that a syntax problem could cause all this mess...
 
  • #17
chemistry1 said:
What does it exactly do ?

'#include <string>' gives you access to the 'string' data type and its associated operations, like '#include <iostream>' gives you access to 'cout', 'getline", the '>>' operator for stream output, etc.

I'd expect that trying to declare a 'string' variable without first doing #include <string>' would give you a compiler error message. Maybe this has changed in more recent versions of C++ than the ones I've used.
 

Similar threads

Back
Top