-1

(a) Define the class Book with all the basic attributes such as title, author, publisher, price, etc. Define the default constructor, member functions display_data() for displaying the Book details. Use appropriate access control specifiers in this program.

Output:

Line 16: error: stray '\342' in program
compilation terminated due to -Wfatal-errors.

Source:

#include <iostream>
#include <string>

using namespace std;

Class Book
{
    Int ISBNNo;
    Char Title[30];
    Char Author[30];
    Char pub[50];
    Float price;

    Public: Book()
    {
        ISBNNO = 1001;
        Strcpy(Title, ”C++ Programming”);
        Strcpy(Author, “E.Balagurusamy”);
        Strcpy(pub, “Tata Press”);
        Price = 300;
    }

    Void display_data()
    {
        Cout << ”Book ISBNNO” << ISBNNO << endl;
        Cout << ”Book Title” << Title << endl;
        Cout << ”Book Author Name” << Author << endl;
        Cout << ”Book Publisher” << pub << endl;
        Cout << ”Book Price” << price << endl;
    }
};

Main()
{
    Book B;
    B.display_data();
    getch();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zakk
  • 29
  • 1
  • @SingerOfTheFall That is a bit hard to read ;) – dyp Sep 18 '13 at 07:36
  • @Dyp, well, yea :S Also, `main` starts with a small `m`, and returns `int`. – SingerOfTheFall Sep 18 '13 at 07:36
  • 1
    what is interesting is that in some places " is used to open and close, and in other places “ is used to open. It's like someone is neurotic or is really trying to obfuscate the code. By the way, that code has more serious problems than just “... – thang Sep 18 '13 at 07:37
  • 1
    C++ is case sensitive. So what's `Void` `Main` `Int` `Char` etc? – Yu Hao Sep 18 '13 at 07:38
  • 2
    It looks to me like someone copied some code from a website, pasted it into Word (or another word processor), then copied it from there and pasted it into an IDE. – thang Sep 18 '13 at 07:39
  • 2
    Don't you mean... “someone”? – thang Sep 18 '13 at 07:41
  • That's the worst code snippet I've ever read. And also it sounds like homework! – adripanico Sep 18 '13 at 07:51
  • 1
    @adripanico The worst code snippets I've seen are much worse and were written by "professionals". OP is obviously a beginner and the question is IMHO legit, I fail to see why it is downvoted. Everyone once started and I can easily imagine that something like the above happens to a lot of people. Let's help them to overcome this hurdle instead of bashing them for trying to learn something new. – Daniel Frey Sep 18 '13 at 08:17
  • @DanielFrey thank you daniel, everyone failed to see that this is my FIRST C++ program, only you understood thank you! as for the rest, i am sorry that you guys found my question useless, i hoped for clarification, and what i got was disappointment – zakk Sep 18 '13 at 08:24
  • 1
    The close reason is incorrect. This is a duplicate, asked many times over ("Error: stray ‘\342’" makes it quite clear what the problem is (is entirely reproducible and is ***not*** caused by a typo)). The canonical is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Mar 05 '21 at 07:51
  • Didn't you leave out some of the stray errors? I would expect it to contain stray '\342', '\200', and '\235'. 342 200 235 (octal) → 0xE2 0x80 0x9D (hexadecimal) → UTF-8 sequence for Unicode code point U+201D ([RIGHT DOUBLE QUOTATION MARK](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)). – Peter Mortensen Mar 06 '21 at 21:21
  • Does this answer your question? [Compilation error: stray ‘\302’ in program, etc](https://stackoverflow.com/questions/19198332/compilation-error-stray-302-in-program-etc) – Karl Knechtel Jan 16 '23 at 14:32

1 Answers1

3

You need to use a proper editor. You are using and not ". The former is a character which is not allowed in C++ source code, the latter is a regular double-quote.

If I see your code, it seems you are using a word processor suitable for writing regular text (like letters, articles, etc.). Note that source code is different and you need to use an editor. I, for example, use emacs, other alternatives are available. A (source code) editor would not try to replace the character you typed by something more appealing or capitalize words (which is I think happened for Void, Int, etc. - these need to be void, int, ...)

The C++ standard defines the allowed characters in §2.3:

2.3 Character sets [lex.charset]

1 The basic source character set consists of 96 characters: the space character, the control characters repre- senting horizontal tab, vertical tab, form feed, and new-line, plus the following 91 graphical characters:14

a b c d e f g h i j k l m n o p q r s t u v w x y z

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

0 1 2 3 4 5 6 7 8 9

_ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ∼ ! = , \ " ’

14) The glyphs for the members of the basic source character set are intended to identify characters from the subset of ISO/IEC 10646 which corresponds to the ASCII character set. However, because the mapping from source file characters to the source character set (described in translation phase 1) is specified as implementation-defined, an implementation is required to document how the basic source characters are represented in source files.

Community
  • 1
  • 1
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • *Pedantic:* The latter is *not* a Unicode character? And why isn't it allowed in C++ source code? (I imagine an obscure implementation allowing `hrglt”` as an identifier..) – dyp Sep 18 '13 at 07:38
  • @DyP The allowed characters are defined in §2.3 and any implementation that would allow an identifier like the one you created would lead to non-portable source code. – Daniel Frey Sep 18 '13 at 07:41
  • [lex.name] (or 2.11) describes the valid characters for an *identifier*, and it lists "other implementation-defined characters". But yes, this would lead to non-portable source code. However, the encoding of the source file isn't specified either. – dyp Sep 18 '13 at 07:42
  • @thang but not legible ;) (and probably not portable, if my eyes don't deceive me) – dyp Sep 18 '13 at 07:44
  • 1
    2.3 only defines the lex character set. It doesn't say that you necessarily have to generate an error if you use something outside of that char set. – thang Sep 18 '13 at 07:51
  • @thang Yes, but I think it's best to stick to these characters. Except maybe for the *content* of a string, but definitly not for the delimiters of a string :) (Also, given that OP seems to be a beginner, let's not throw currently useless details at him - better solve his problem, the rest will come (much) later) – Daniel Frey Sep 18 '13 at 07:55
  • 1
    @thang The issue is not trivial. The mapping of the source character codes to internal characters is implementation defined. After the mapping, the _only_ character that the standard allows as a delimiter of a string literal is Uniocde \u0022, but an implementation could certainly map \u201C and \u201D to \0022 on input. Similarly, an implementation could reject all characters except those explicitly listed in §2.3 as input, and require `wchar_t quote = "\u201C";` for the left quote character. In practice, using any characters other than those above exposes you to portability problems. – James Kanze Sep 18 '13 at 08:37
  • Ok, but my whole point is that "The former is a character which is not allowed in C++ source code" isn't strictly correct. What is allowed is very different from what is good practice. You are confounding those two items, and you certainly shouldn't quote the standard to make a statement about what is good practice. The standard is not a guideline for writing good code. – thang Sep 18 '13 at 08:48
  • @thang Yes, I know, but please think of the target audience of this question. Someone who ends up here needs help with very basic things and limiting them in the way I did allows them to continue learning the basics. It doesn't matter if it is strictly correct. When you start learning mathematics in school, you don't start teaching the kids [formal logic](http://en.wikipedia.org/wiki/Mathematical_formalism#Formal_logical_systems) first, right? – Daniel Frey Sep 18 '13 at 08:54
  • When I started to learning to play guitar, I learned a few simple chords the easy, but incorrect, ways. 2 years later, it's much more difficult to unlearn the simple chords and learn the correct ones than to start with the correct ones to begin with. Simple vs complex is not the same as right vs wrong. You can simplify things and still make it correct... and I will stop here :p – thang Sep 18 '13 at 08:59
  • A more likely source (than a word processor) is copied code from a web page, a PDF document, email, or through chat (e.g., Skype Chat). – Peter Mortensen Jul 23 '21 at 01:50