C++ run error with command sfc /scannow - c++

I'm new here and I wanted to write a C++ source code that executes the command:
sfc.exe /scannow
But it doesn't work. Windows-resource protection can't start repair service.
I'm a student and I'm in my 10th grade and I do not have computer science at school, so I wanted to ask the question here.
Here is my code excerpt:
#include<stdlib.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
system("sfc.exe /scannow");
}
The compiler brings no errors.
I am using the GNU/GCC compiler, Windows 10 1803 and Code::Blocks(IDE)
I hope you can understand it :)

If you are using 64 bit system but your compiler is 32 bit means you will get this error. If you are using visual studio c++ and your pc is 64 bit system means change the platform to 64 bit.
For more information, ref this answer https://stackoverflow.com/a/20872354/6599346

Your code works, if you delete the # before main() and using namespace and just for completion I added return 0; at the end.
Code:
#include<stdlib.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
system("sfc.exe /scannow");
return 0;
}
Run the code with administrator rights and everything should be okay.

Related

Cannot create new text files by C++ in Codeblocks in Mac

I have a C++ code in Codeblocks in Mac OS that should create some .txt files and write something on them. It worked on Windows, however, it does not run correctly on Mac. It does not make any errors, and it announces that the procedure is terminated with status 0; however, it does not create any .txt files!! What can be the problem?
It even does not claim that Codeblocks needs special permission. Moreover, the execution window background is White; different from the usual execution window of Codeblocks, which is Black.
This is the code:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
int main(){
for (int i=1;i<=1;i++){
ofstream fout ("test1.txt");
int n=rand()%10+1;
// int n=15;
fout<<n<<endl;
}
return 0;
}
This code just does not create any .txt file in Mac, while it worked on Windows.
"test1.txt",this is a relative path, you maybe hava use clion,you must know where relative path, but it is using a absolute path.

Can you open a file from a an online compiler (repl.it) using C++

I want to see if it's possible to open a file, write and save it using an online compiler. The reason why I am using that is because I am using a really old macbook that has a hard time running xcode or codeblocks. The file I want to open is saved on my desktop under the file name "image.ppm". This is the code :
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
char filename[100];
ifstream input;
cin.getline(filename, 100);
input.open(filename);
}
When I run it nothing happens and I assume it's because the online compiler can't see the image.ppm file
Whether or not repl.it will let you read/write files is irrelevant; there is of course no way it can see files on your machine.

why I cant run this why this c++ code giving errors in visual studio ,though it is running well in code blocks after deleting #include "stdafx.h"

why this c++ code is giving error in visual studio though it runs well in code blocks after deleting #include "stdafx.h"
#include "stdafx.h"
#include <iostream>
using namespace std;
void chintumal();
int main() {
chintumal();
return 0;
}
void chintumal() {
cout << "this is anurag pradhan ! \n";
}
screenshot of visual studio with entered code
You can have only one main function in your code. Looks like you have another main defined in ConsoleApplication1.cpp file.
Actually I'm a beginner hence facing little issues ,actually i made a project in visual studio and in the same project created two files with one one main function in each, hence my code was not running !

Dev C ++ no output

I am trying to write a simple project on Dev C++ , but it seems it is not working like it should on windows 8.
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
cout<<"hello You~~"<< endl;
system("PAUSE");
return 0;
}
After compiling it, it shows an empty black screen. Am I doing something wrong?
There are known issues with running Dev-C++ 4.9.9.2 on Windows 8 (it's not that surprising, it's a program from 2005). Try a newer IDE - if you like Dev-C++, you can try Orwell Dev-C++.

mpf_set_d causes an illegal instruction in c++

I'm using GMP and getting an illegal instruction that I have found to be caused by mpf_set_d. I'm programming in Netbeans with cygwin in c++.
Thanks in advance!
Edit:
I have a folder containing the following files:
.dep.inc, cyggcc_s-seh-1.dll, cyggmp-10.dll, cyggmpxx-4.dll, cygstdc++-6.dll, cygwin1.dll, my executable, gmp.h, gmpxx.h, libgmp.a, libgmp.la, libgmp.lai, libgmp.lai, libgmp.libcmd, libgmpxx.a, libgmpxx.la, libgmpxx.lai, main.cpp, main.o, main.o.d, Makefile, text file needed for program and folders needed for program.
I have tried many things, one of which was to add an executable to this folder that ran the following code:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include "gmpxx.h"
#include <stdarg.h>
#include <string>
using namespace std;
int main()
{
mpf_t a;
mpf_init(a);
mpf_set_d(a,3.1415926535);
cout << "works" << endl;
}
Running this on my own computer with GMP installed gives me 'works' after which it closes, on another computer that does not have GMP installed it throws an error and closes. The error is an exception: status_illegal_instruction.
I can't give you my code but I can post snippets. Please tell me if more information is necessary!
Thanks again.
Edit 2:
The same counts for mpf_set_str as well as the c++ wrapper.