How to print Content of currently Executing file C++? - c++

How to print all the content of the currently executing file from in itself in C++?
#include <iostream>
using namespace std;
int main() {
// some code that print current file.
}
expected output:
#include <iostream>
using namespace std;
int main() {
// some code that print current file.
}

there can be many ways possible, easy one.
#include <iostream>
using namespace std;
int main() {
string more = "more ";
more.append(__FILE__);
system(more.c_str());
}
Output:
#include <iostream>
using namespace std;
int main() {
string more = "more ";
more.append(__FILE__);
system(more.c_str());
}

Related

Error while performing string copy operation in C++

Please explain why is this giving an error but the other on is running fine
The following code gives the error:
#include <iostream>
#include <string>
#include <conio.h>
#include <math.h>
#include <iomanip>
#include <string.h>
using namespace std;
int main()
{
string s1,s2;
int i;
cout << "Enter the string to copy into another string : ";
getline(cin,s1);
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
cout<<"\n\nCopied String S2 is : "<<s2;
return 0;
}
Error looks like this
But this works perfectly fine
#include <iostream>
#include <string>
#include <conio.h>
#include <math.h>
#include <iomanip>
#include <string.h>
using namespace std;
int main()
{
char s1[100], s2[100], i;
cout << "Enter the string to copy into another string : ";
cin>>s1;
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
cout<<"\n\nCopied String S2 is : "<<s2;
return 0;
}
In your case, s2 is initialized to an empty string with a length of 0, so you can't write past the bounds. If you want to, you must first resize it:
s2.resize(s1.length());
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
Also, c++ std::string does not need a terminating nullbyte, unlike C strings.

Difficulty understanding while loop execution

#include <iostream>
#include <iomanip>
#include <ios>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
using std::cin; // <iostream>
using std::cout; // <iostream>
using std::endl; // <iostream>
using std::setprecision; // <iomanip>
using std::sort; // <algorithm>
using std::streamsize; // <ios>
using std::string; // <string>
using std::vector; // <string>
int main()
{
cout << "Enter your homework grades : " << endl;
double x;
vector<double> homework;
int count = 0;
while(cin >> x)
{
homework.push_back(x);
++count;
if(count == 0)
{
cout << "Error, enter a grade" << endl;
continue;
}
}
return 0;
}
Hi, I'm wondering why my while loop wont print the message (Error, enter a grade) on the screen if the if-statement is within the loop, and seems to only work when its placed outside after the loop executes why is this the case?
You are never going back to zero.
int count = 0;
//...
while(cin >> x)
{
homework.push_back(x);
++count;
if(count == 0) //count will never be zero

Error: Identifier "cout" is undefined. <iostream> included and using namespace std;

I am trying to cout some variables but the compiler says that cout is undefined. I have included iostream and am using namespace std. Removing using namespace std and using std::cout instead changes the issue to "namespace "std" has no member "cout" ". I found some answers saying to add # include "stdafx.h" to the code but Error: cannot open source file "stdafx.h" occurs.
Code is:
#include "Complex.h"
#include <cmath>
#include <iostream>
using namespace std;
Complex::Complex(int PolarOrRectang, float RealOrArg, float ImagOrAng) {
if (PolarOrRectang == 0) {
real = RealOrArg;
imag = ImagOrAng;
else {
real = RealOrArg * cos(ImagOrAng);
imag = RealOrArg * sin(ImagOrAng);
}
};
void Complex::getValue(int PolarOrRectang) {
if (PolarOrRectang == 0) {
cout << real << " +_" << imag << "i" << endl;
} else {
cout << sqrt((real^2) + (imag^2)) << "*e^-" << atan(imag / real)<< endl;
}
};
I'm trying to define a class, so my main is elsewhere.
Running a very basic program that just couts "hello world" works fine, the problem is specific to this code.
Put #include<iostream> at the first position, the order is important
#include "Complex.h"
#include <iostream>
#include <cmath>
PS: Why do you use std:: when you are using "using namespace std;"?

writing data from right to left in file?

instead of saving data and then reversing it i wanted to know if there is any function that can save the output to the file directly from right to left?
for example if i have this simple code:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream fout("filepath");
fout<<"stackoverflow");
return 0;
}
instead of having :
stackoverflow
i want to have:
wolfrevokcats
*this example is just an example don't be mad at me :D
You can reverse the string before writing it. That's the only way I know how to do it because you can't write from "right" to "left"
#include <iostream>
#include <string>
#include <algorithm>
int main(int argc, char **argv) {
std::string thing("my thing");
std::reverse(thing.begin(), thing.end());
std::cout << thing << std::endl;
return 0;
}

Printing with fixed spaces

I want to print two strings (say "ABC" and "DEF") with 5 space characters before "ABC" and that the second string will start 7 characters after the beginning of the first string.
I suspect you're looking for the width() method:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string abc = "ABC";
string edf = "EDF";
cout.width(8);
cout << abc;
cout.width(7);
cout << edf;
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char a[] = "ABC";
char b[] = "EDF";
cout<" "<<a<<" "<<b;
return 0;
}