I am a newbie to competitive programming and I recently started into it. I started learning C++ for competitive programming. From all the resources I have read I understood that we need to have some backend functions ready for us to be fast in competitions. So, I have prepared all of it and I have read somewhere that we can take input from file and write output to file rather than using terminal to give inputs. this I have done using freopen() and I ran into an issue.
The issue is that I couldn't read string input from the file(input.txt) or write to the output file(output.txt) in C++. but the reading of integers, characters, floating point numbers, everything is fine but there is only problem with the string input. I could even input strings using character arrays it works well. But sometimes using character array is not better for the program. So , if anyone could help with that to me I would appreciate it. one more thing is that when we try to take string input the programs executes but nothing happens.
here are my codes and input and outputs **ps:**input refers to data in input.txt at the time of execution and output refers to data in output.txt after execution
C++ version in my PC - C++14(Mingw compiler)
```this first code is where problem exists```
#include<iostream>
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
string a;
cin>>a;
// here I even tried getline(cin,a) but it also doesn't work as program executes
// but nothing will be written to output
cout<<a<<"\n";
return 0;
}
input - abcdef
output- (BLANK)
----------
#include<iostream>
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int a;
cin>>a;
cout<<a;
return 0;
}
input - 9
output- 9
----------
#include<iostream>
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
char a[100];
cin>>a;
cout<<a<<"\n";
return 0;
}
input - abcdefg
output - abcdefg
----------
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
string a="abcdef";
cout<<a<<"\n";
return 0;
}
input - (BLANK)
output - (BLANK)