1

I have written this C++ file.

I tested my C++ file in Visual Studio, and I tried to compile it in Cygwin too for practice.

As you can see in the first image, in Visual Studio, it was successful. But in Cygwin, the same file couldn't be compiled. This is what I tried to compile:

g++ Coursera.cpp

And these are the errors as a result:

Error1

Error2

Including a bunch of warnings, there are a lot of messages. The main errors are:

Coursera.cpp:1:1: error: stray ‘\377’ in program
 ▒▒
 ^

Coursera.cpp:1:2: error: stray ‘\376’ in program
 ▒▒
  ^

Coursera.cpp:17:4: error: invalid preprocessing directive #i; did you mean #if?
    c i n   > >   N ;
    ^
    if

Coursera.cpp:19:2: error: ‘u’ does not name a type
    i n t   n u m b 1   =   0 ,   n u m b 2   =   0 ;
  ^

Coursera.cpp:25:2: error: ‘i’ does not name a type
      {
  ^

How can I fix these errors? What is the reason for it?

I also tried compiling the same C++ file converted into UTF-8:

cd ~
g++ Coursera_UTF.cpp

And I got these errors:

In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/exception_ptr.h:38:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/exception:142,
                 from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/new:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ext/new_allocator.h:33,
                 from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++allocator.h:33,
                 from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/allocator.h:46,
                 from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/vector:61,
                 from Coursera_UTF.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/cxxabi_init_exception.h:38:10: fatal error: stddef.h: No such file or directory
 #include <stddef.h>
          ^~~~~~~~~~
compilation terminated.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Antonio SEO
  • 457
  • 1
  • 9
  • 23
  • 2
    Looks like the file is UTF-16 (Windows Unicode) encoded. Use a text editor and save it as UTF-8. – Richard Critten May 27 '18 at 11:58
  • 1
    Possible duplicate of [\377\376 Appended to file (Windows -> Unix)](https://stackoverflow.com/questions/12462340/377-376-appended-to-file-windows-unix) – DavidPostill May 27 '18 at 12:00
  • I did both of them... :( Failed... I will show u – Antonio SEO May 27 '18 at 12:15
  • @RichardCritten Hi, Thank you for your help. I Changed it but there appeared another error... Plz See the post – Antonio SEO May 27 '18 at 12:19
  • @DavidPostill Thank your David. I'm trying trying to use that solution but I don't think it works... – Antonio SEO May 27 '18 at 12:21
  • That's a different error altogether. File **stddef.h** appears to be missing from your cygwin installation. – Paul Sanders May 27 '18 at 12:52
  • @RichardCritten Visual Studio can save files as UTF-8 but it's a bit tucked away, see [here](https://msdn.microsoft.com/en-us/library/dxfdkfke.aspx?f=255&MSPPError=-2147217396). It probably saved the file as Unicode originally because it contained a non-ASCII character. It doesn't normally do that. That's still hostile behaviour though, I don't suppose anybody likes it. – Paul Sanders May 27 '18 at 12:53
  • @PaulSanders Thank you Paul I do understand what you mean. But the thing is I can't find that file on my cygwin intstallation packages(on Select Packages) Do you know what it is? – Antonio SEO May 27 '18 at 12:58
  • Not much in it actually, see http://pubs.opengroup.org/onlinepubs/7908799/xsh/stddef.h.html. Someone else had this problem recently but I can't track down the post. Maybe cygwin is slightly broken at the moment. – Paul Sanders May 27 '18 at 13:35
  • Sorry, that's a very old link, this one is better: http://www.cplusplus.com/reference/cstddef/. Also, there's a likely looking post here: https://stackoverflow.com/questions/31600600/compilation-error-stddef-h-no-such-file-or-directory. Google is your friend. – Paul Sanders May 27 '18 at 13:42
  • `stddef.h` belongs to `gcc-core` try reinstalling the 7.3.0-2 tests version and be sure to have it properly installed with `cygcheck -c gcc-core gcc-g++` – matzeri May 28 '18 at 06:00
  • Related: *[Should we edit a question to transcribe code from an image to text?](https://meta.stackoverflow.com/questions/415040)* – Peter Mortensen May 06 '23 at 19:03
  • Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and [do the right thing](https://stackoverflow.com/posts/50551861/edit) (it covers answers as well). Thanks in advance. – Peter Mortensen May 06 '23 at 19:04

1 Answers1

2

In the menu, choose Save As. In the save dialog will be a Save button with down arrow. Select Save with encoding and choose ASCII.

Visual Studio by default uses wide characters when files are created.

Not all other compilers can compile files saved with this encoding.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
  • Thank you Robert ! I chose Encoding option as US-ASCII- Codepage 20127 And I got the same error... 10: fatal error: stddef.h: No such file or directory #include ^~~~~~~~~~ compilation terminated. this one is the thing I posted. Do you know What I should install additionally ? – Antonio SEO May 27 '18 at 13:11
  • Try this : https://stackoverflow.com/questions/31600600/compilation-error-stddef-h-no-such-file-or-directory#32410193 – Robert Andrzejuk May 27 '18 at 13:44
  • And this : https://stackoverflow.com/questions/34762556/cygwin-g-compilation-fails – Robert Andrzejuk May 27 '18 at 13:45