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.
Related
I have a russian Roulette script written with C++. If two randomly generated numbers are the same, the script deletes a specified file.
People suggested to me that I should use C++17 for using the <filesystem> library in order to run file-related operations correctly. The removing operation runs if the conditions are matching. The if block runs correctly, but removing the file isn't happening.
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <filesystem>
//include filesystem and replace remove() with filesystem libs remove function
using namespace std;
namespace fs = std::filesystem;
int main(){
int minNumber = 1, maxNumber = 6;
int possibility,chamberNumber;
srand(time(0));
possibility = rand() % (maxNumber - minNumber + 1 ) + minNumber;
chamberNumber = rand() % (maxNumber - minNumber + 1 ) + minNumber;
cout << "First Number: " <<possibility<<endl<<"Second Number: " << chamberNumber<< endl;
if (possibility == chamberNumber){
std::filesystem::remove("C:\\Users\\mypath\\Desktop\\cppRoulette\\delete.txt");
cout << "You're Dead " <<possibility<< endl;
}
// else{
// cout << possibility << endl;
// }
return 0;
}
I use this line to compile my code:
g++ -std=c++17 rulet.cpp -o output
Here is a screenshot of the compiled output. Notice "delete.txt" still stands.
I am using WSL Debian, because I am using VS Code and native terminals don't work proper with g++. I may like smoothness of Linux a bit more, though.
I am looking for a proper and easier way to deal with files, just like in Python. I am attending a C++ crash course, so I am trying to learn it, switching to Python isn't on the table.
Your file path is wrong. I just got your code to work on my system by changing the path from E:\Test\delete.txt to /mnt/e/Test/delete.txt.
Under WSL, all Windows drives (C:, E:, etc.) are mounted under the /mnt directory, in subdirectories that match the drive letter (/mnt/c/, /mnt/e/, etc). In order to convert your Windows path for use in WSL, you need to do the following:
Replace all backslashes (\) with forward slashes (/).
Remove the colon (:) after the drive letter.
Convert the drive letter to lower case.
Prepend the string "/mnt/" to the path.
After this, your program works, and will delete the target file.
Recently I want to enhance CMD in Win10 by myself using C++. I don't want to change the original framework of it but to translate the command. At first i wrote something like this:
#include <unistd.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string initial = "retr0# ";
string s;
while(1)
{
cout << initial;
getline(cin,s);
if(s!="exit")
{
system(s.c_str());
cout << "------" << endl;
}
else break;
}
system("pause");
return 0;
}
But I found that if you entered command like "E:" or something else to change the directory, it is impossible for the new thread to inherit the context. My question is, how to solve the problem like this?
In most operating systems (including 1970 era Unix), the working directory is specific to each process.
The system function will run another process. So even if you change its working directory, it only affects the process started by system, not the process running your program.
So you need to define a syntax (perhaps the same cd as Windows CMD has) and parse and implement that command in your own program. You could use SetCurrentDirectory or _chdir
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.
I don't know what's happened here. Here is the code sample:
#include<stdio.h>
#include<iostream>
#include<string>
#include <Windows.h>
using namespace std;
int main()
{
char my_current_path[1024];
string current_path(R"(C:)");
if (!SetCurrentDirectory(current_path.c_str()))
cout << "cant change to that directory.";
GetCurrentDirectory(1024, my_current_path);
std::cout << my_current_path << endl;
system("pause");
return 1;
}
What I'm doing here is trying to change the directory to some directories.
There is two strange things with my code.
(1 Strange thing)
When I try to change to "c:" like that:
#include<stdio.h>
#include<iostream>
#include<string>
#include <Windows.h>
using namespace std;
int main()
{
char my_current_path[1024];
string current_path(R"(C:)");
if (!SetCurrentDirectory(current_path.c_str()))
cout << "cant change to that directory.";
GetCurrentDirectory(1024, my_current_path);
std::cout << my_current_path << endl;
system("pause");
return 1;
}
It's not working and not changing the path (and its the good thing.) but its not show me the message that shown when the directory isn't changed:
cout << "cant change to that directory.";
Why is that? (When I try to change something like "efef:" or "exist?" it is showing me that's messege. But Why here it doesn't show me, and also doest change the current working directory?
(Second Strange thing)
When I change the directory to "G:" Its for some reason working..
#include<stdio.h>
#include<iostream>
#include<string>
#include <Windows.h>
using namespace std;
int main()
{
char my_current_path[1024];
string current_path(R"(G:)");
if (!SetCurrentDirectory(current_path.c_str()))
cout << "cant change to that directory.";
GetCurrentDirectory(1024, my_current_path);
std::cout << my_current_path << endl;
system("pause");
return 1;
}
That code compiled, showing that I successfully changed the directory to "G:".
After changing to that path, I'm trying to change to "C:" (Which before, didn't did anything) and now its working! but in strange way, its not moving to "C:\" But to:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE
And its weird that before its not worked, and now when I'm in "G:" path and trying to move to "c:" again, then its move to path that I didn't even wanted!
If you want to specify the root directory on a drive, you must include a backslash, i.e., C:\.
The syntax you are using, C:, has a different meaning. The command shell separates the concepts of the current drive and the current directory on each drive, so that you can switch between drives quickly without having to retype the full path each time. This feature was introduced (a long time ago) for backwards compatibility reasons (see below) but is actually very useful; power users will sometimes map multiple drive letters to the same drive or network share specifically in order to take advantage of this functionality.
Although only the command shell keeps track of the current directory for each drive, the Win32 API knows how to look them up, and does so whenever you specify a drive but no path. As a special case, if the drive specified is the current drive, it expands to the actual current directory rather than the saved current directory.
So in your example the incomplete path C: is expanded to the current directory for the C drive; since your current directory is already on the C drive, SetCurrentDirectory(R"(C:)") is a no-op. If you want to change to the root directory, you should use SetCurrentDirectory(R"(C:\)") instead.
In your testing, there was no saved current directory for the G: drive, and it was not the current drive, so the effect was to set the current directory to G:\, i.e., you got the behaviour you expected but not for the reason you were expecting. If you had launched the program from the command shell and a current directory had been saved for the G drive, you'd have wound up there instead.
The same thing applies to opening files; C:file.txt will open file.txt from the current directory for the C drive, whereas C:\file.txt will open file.txt from the root directory.
See Raymond Chen's blog post, Why does each drive have its own current directory? for a discussion of the history of this feature.
you can use the two APIs for retrieving and setting the current directory: GetCurrentDirectory() and SetCurrentDirectory() only provide a valid path for the latter:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
char czCurDir[MAX_PATH] = "";
char czNewDir[MAX_PATH] = "";
// retrieve the current directory
GetCurrentDirectory(MAX_PATH, czCurDir);
cout << "Current directory: " << czCurDir << endl;// C:\Users\Raindrop7\Desktop\New folder
// set the value for the new directory NB: C:\\ not C:\ because the first backslah is an escape character
strcpy(czNewDir, "C:\\");
// set the current directory to "C:\"
SetCurrentDirectory(czNewDir);
// retrieve the current directory
GetCurrentDirectory(MAX_PATH, czCurDir);
cout << "current directory: " << czCurDir << endl;// C:\
return 0;
}
First change drive to single F or G drive-
Example::
if( !SetCurrentDirectory("F:") )
{
cout <<"can't open the directory"<< endl;
}
then give full path-
Example::
if(!SetCurrentDirectory("F://Python_Django_Webdevelopment//StudentManagement"))
{
cout <<"can't open the directory"<< endl;
}
It works for me.
I'm really struggling at the moment, originally had other issues with eclipse itself, that seems to have been resolved. Code looks right to me (compared to example code for loading files) however I'm not able to load anything as the error I put in is always triggered. No building errors atm. What am I doing wrong? Tried with both eclipse (mac) and Code::blocks (win vm), both seem to be having issues. the data files themselves are in the same folder as the .cpp file.
#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
using namespace std;
int main() {
cout << "Choose which data file to load (1-4)" << endl;
int file;
cin >> file;
ifstream data;
switch (file) {
case 1:
data.open("dataSet1.txt");
case 2:
data.open("dataSet2.txt");
case 3:
data.open("dataSet3.txt");
case 4:
data.open("dataSet4.txt");
}
if (!data) {
cerr << "File not Loaded" << endl;
return -1;
}
string FullData[61];
for (int i=0; i=60; i++){
data >> FullData[i];
cout << FullData[i] << endl;
}
return 0;
}
EDIT: Got the program to stop showing the error, and it seems to be loading the files, however my assign/display loop doesn't seem to be working now as it displays only the last data point over and over again.
the data files themselves are in the same folder as the .cpp file
Being in the same folder as the .cpp is not important, the dataset files should be in the same folder as the compiled binary program.
It can also be that there is a working directory setting that does not point on the directory where your dataset files are. All that is being passed into the open member function is a string which means that interpreting what that string means depends on the environment settings.
Same issue. In my case as #Gluk36 pointed, the problem was the working directory settings.
In that case, you must deselect "Use default settings" and set where the binary is. I attach you a screenshot for your reference from eclipse CDT 4.9 under linux.
You must have break statement after each case
like:
case1://something;
break;
and you must close the stream with close() function
data.close();