-1

My code is (using Atom):

Kindly note all the errors have been removed, but the output is wrong. At the end of the question, the output is listed.

#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. How can I get the solution for these errors?

Output:

51
1982955789
859839168
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 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
  • 2
    @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
  • For the error messages: It is 342 200 234 (octal) → 0xE2 0x80 0x9C (hexadecimal) → UTF-8 sequence for Unicode code point U+201C ([LEFT DOUBLE QUOTATION MARK](https://www.charset.org/utf-8/9)). And 342 200 235 (octal) → 0xE2 0x80 0x9D (hexadecimal) → UTF-8 sequence for Unicode code point U+201D ([RIGHT DOUBLE QUOTATION MARK](https://www.charset.org/utf-8/9)). They can be searched for (and replaced) using the regular expression `\x{201C}|\x{201D}`. – Peter Mortensen Apr 26 '23 at 22:53
  • This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 26 '23 at 22:55
  • What is "notepad" in the title? Windows' [Notepad](https://en.wikipedia.org/wiki/Windows_Notepad)? [Notepad++](https://en.wikipedia.org/wiki/Notepad%2B%2B)? Why is Atom then mentioned in the body and not Windows' Notepad? We [may never know](https://www.explainxkcd.com/wiki/index.php/979:_Wisdom_of_the_Ancients) as the OP has left the building (*"Last seen more than 3 years ago"*). – Peter Mortensen Apr 26 '23 at 22:59
  • Note: The notation is different in Visual Studio Code (and probably others): `\u201C|\u201D` – Peter Mortensen Apr 27 '23 at 13:09

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