I couldn't give string as input from my input file(input.txt) to c++ program in vscode

Viewed 156

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)
1 Answers

At first, I wanted to tell everybody that I am not pretty sure about my answer, I am new to Stackoverflow and do not have enough reputation that's why I couldn't write comments. Answering the question is the only way that I can share my opinion or research about this question. So, please consider me as a newbie and give me your suggestion so that I can improve myself. Thank You.

Let's come to my opinion:

  1. I saw you are using cin and cout in your code without adding (std::) in front of them and neither you use "using namespace std". So my question is that how could run your program without getting an error.

  2. I just copy your code and try to run in my computer. And I find out that your code worked perfectly in every cases, weather I input int or string. it just worked fine. But I find out a cause where it didn't work maybe this is the problem your facing. And that is when you edit your output.txt file and you didn't save that file. Then you get nothing in your output.txt file, weather you take int or string as an input. So make sure you saved all three file and then run the main program.

That's all I got. Please let me know, my answer is helpful or Not?

Related