-1

My code is:(Using Atom) Kindly NOTE all the errors have been removed but output is wrong. At the end of the question output is mentioned.

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
  char ch ;
  int a, arr[2], i=0;
  ifstream f(“num.txt”);
  while(!f.eof())
  {
    f>>ch;
    arr[i]=a;
  }
  f.close();
  for(int i = 0 ; i<3 ; i++)
  {
    cout<<a[i]<<'\n';
  }
  return 0;
}

The data of 'num.txt' is '123'

Error:
C:\Users\sy304\Documents\CPP\num_text.cpp:10:14: error: stray '\342' in program 
ifstream f(“num.txt”); 
^ 
C:\Users\sy304\Documents\CPP\num_text.cpp:10:15: error: stray '\200' in program 
ifstream f(“num.txt”); 
^ 
C:\Users\sy304\Documents\CPP\num_text.cpp:10:16: error: stray '\234' in program 
ifstream f(“num.txt”); 
^ 
C:\Users\sy304\Documents\CPP\num_text.cpp:10:24: error: stray '\342' in program 
ifstream f(“num.txt”); 
^ 
C:\Users\sy304\Documents\CPP\num_text.cpp:10:25: error: stray '\200' in program 
ifstream f(“num.txt”); 
^ 
C:\Users\sy304\Documents\CPP\num_text.cpp:10:26: error: stray '\235' in program 
ifstream f(“num.txt”); 
^ 
C:\Users\sy304\Documents\CPP\num_text.cpp: In function 'int main()': 
C:\Users\sy304\Documents\CPP\num_text.cpp:10:17: error: 'num' was not declared in this scope 
ifstream f(“num.txt”); 
^~~ 
C:\Users\sy304\Documents\CPP\num_text.cpp:19:14: error: invalid types 'int[int]' for array subscript 
cout<<a[i]<<'\n'; 
^ 

I am new to Atom and not able to get the solution of these errors.

Output:

51
1982955789
859839168
  • 3
    Looks like you're using some non-ascii quotes. Here, use these: `"num.txt"`. – JohnFilleau Feb 21 '20 at 03:50
  • Can you put the stack trace in a code block so it doesn't look so jumbled. – Todd Feb 21 '20 at 03:57
  • 1
    "Notepad" is not a text editor that's suitable for writing and editing C++ code. You need a real text editor. – Sam Varshavchik Feb 21 '20 at 04:00
  • 1
    @SameerYadav I wasn't kidding about my comment. If you replace your smart quotes with the regular quotes I provided it will clear up those errors. – JohnFilleau Feb 21 '20 at 05:01
  • @John It would appear you knowingly answered the question in the comments? Comments are intended to ask for more information or to suggest improvements, not for answering questions. If a you have an answer to a question that warrants an answer, post the answer as an answer please. – JaMiT Feb 21 '20 at 05:13

1 Answers1

0

You are indexing the variable a, which is an int, not an array. Also, in the while loop, you are only accessing the first element of array arr (because i is 0), and you are assingnig allways a's value, that's not set anywhere. You should check f.fail() after reading into ch or at least do something like

std::array<int, 2> arr;

for(int i = 0; (std::cin >> ch) && i < arr.size(); ++i) {
    arr[i];
}

If you don't know in advance how many numbers you have to read use std::vector instead and push_back the values.

nsm
  • 319
  • 1
  • 9
  • 2
    Addressing the last error reported by the compiler is usually not an effective way to resolve the errors. In this case the last error is independent of the the earlier ones, but often the later errors are artifacts, existing only because the compiler got confused by the earlier errors. – JaMiT Feb 21 '20 at 04:47
  • In this case it is certainly an error. – nsm Feb 21 '20 at 04:49
  • Yes, we are in agreement that it is an error, but you missed the point. Imagine you are responding to someone who does not understand the error messages. How would you justify ignoring the earlier errors? What would tell this person that the earlier errors can be ignored? – JaMiT Feb 21 '20 at 05:08
  • The first comment to the post already pointed out that non-ascii quotes were used. I supposed it was already clear and so just focused in other errors present. – nsm Feb 21 '20 at 05:11