-4

I just started reading this book and copy pasted this code from the PDF file:

// Read name and age (2nd version)
int main()
{
    cout << "Please enter your first name and age\n";
    string first_name = "???"; // string variable
    // ("???” means “don’t know the name”)
    int age = –1; // integer variable (–1 means “don’t know the age”)
    cin >> first_name >> age; // read a string followed by an integer
    cout << "Hello, " << first_name << " (age " << age << ")\n";
}

The book says it should output,

Hello, 22 (age –1)

if I input

22 Carlos

but I get this error:

D:\C++\Part I The Basics\Programs\3.Read name and age (2nd).cpp|9|error: stray '\226' in program|

And the program isn't compiling.

I realized that in the line below the "minus 1" isn't actually a "minus" "-" sign. .. " – " " - " It is bigger than the minus, see?

int age = –1

So I changed that sign with a minus sign and typed 22 Carlos and it outputs Hello 22, 0 instead of Hello 22, -1.

Why is the program not working when I simply copy-paste it from the PDF file?

Why is it not working even after I change the – sign with a minus "-" sign?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
n0thing15
  • 1
  • 1
  • 2
  • 1
    Read the comment on the line where you're reading the input. It says *// read a string followed by an integer*, but you're feeding it *an integer followed by a string*. What value do you expect *Carlos* as an integer to produce? It doesn't do any good to just blindly type the code from the book and run it, because you don't learn anything other than how to type better. You need to actually *read and understand* the code as well. – Ken White Oct 17 '15 at 22:09
  • 4
    Typographically, the longer minus sign is supposed to be used for negative numbers as opposed to subtraction. I bet the PDF generator automatically swapped for that longer "better looking" minus sign. – Taco de Wolff Oct 17 '15 at 22:09
  • As an aside, be careful when using multiple `?` chars in a row. A compiler that is not yet `C++17` compliant will recognize `??` as the start of a trigraph and depending on the next character will interpret the three chars as one and you'll get unexpected results. Trigraphs are removed in C++17. – Casey Oct 17 '15 at 23:30
  • @KenWhite Hi there, thanks for trying to help me. I do understand that "Carlos" or "Bravos" or any-string-123 has no value as an integer. Tho the book said that if i type the sequnce wrong (integer,string instead of string,integer) it should show the value "-1" but it doesn't even run it. with that long "minus" http://i.imgur.com/UVOGAgj.png – n0thing15 Oct 18 '15 at 00:48
  • @TacodeWolff Tho, why the program is not even running as it states in http://i.imgur.com/UVOGAgj.png – n0thing15 Oct 18 '15 at 00:53
  • @Casey, That's far too complex for me to understand, i just copy paste the code and go line by line to fully understand what it does. http://i.imgur.com/UVOGAgj.png – n0thing15 Oct 18 '15 at 00:54
  • *I just copy paste the code* is the exact problem I warned you against. *Do not just copy/paste the code*. **Read and understand it before you try to run it.** Learning to copy/paste is not learning how to program, and the sooner you learn *that* the sooner you'll start to actually gain knowledge. – Ken White Oct 18 '15 at 01:25
  • 226 (in this case, the stray value is reported in *decimal* (octal, 342, is most common)) is a ***signature*** for the start of a UTF-8 sequence (so stray errors 128 and 147 (decimal) have probably been left out in the question here): 226 128 147 (decimal) → 0xE2 0x80 0x93 (hexadecimal) → UTF-8 sequence for Unicode code point U+2013 ([EN DASH](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)). – Peter Mortensen May 27 '23 at 00:24
  • There are also other stray characters in the code, but they are all in comments and thus likely accepted. – Peter Mortensen May 27 '23 at 00:27
  • U+2013 can be searched (and replaced) for by using the regular expression `\x{2013}` in any modern text editor or IDE. Note: The notation is different in Visual Studio Code (and probably others): `\u2013` (instead of `\x{2013}`). – Peter Mortensen May 27 '23 at 00:28
  • 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)*. In this case, it was revealed the code was copied from a PDF file. – Peter Mortensen May 27 '23 at 00:29

1 Answers1

0

The character in the PDF file looks like it is a different character than the - symbol which causes to compiler to not recognize the symbol. This program does exactly what it is designed to do.

As Ken White said, you are assigning a string to an int, and this is causing your input stream to fail which you never check. You could do something like this:

cin >> first_name >> age;
if(cin.fail())
    age = -1;
// Do stuff

cin.fail() will check to see if the correct value is assigned to the variable from the input stream.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RitterS
  • 44
  • 5
  • Hi there and thanks for the fix. It worked, and now it does exactly what i wanted to do. Like i told KenWhite, i know that strings like "carlos" "andrew" "any-string-example" does not have any value at all as a n integer. I simply did not know why the program didn't swapped the "0 aka no value" with "-1" as the book said it will http://i.imgur.com/UVOGAgj.png . Btw thanks again , now i learn how i can use cin.fail() – n0thing15 Oct 18 '15 at 00:58
  • btw one question. http://i.imgur.com/vciWTp8.png attributing the value "???" to the string "first_name" has any use at all? because after one line i have to type the new value of the string "first_name" – n0thing15 Oct 18 '15 at 01:08
  • You are correct. `string first_name = "???";` in your case is sort of a place holder. You could have `string first_name;` and it will work. – RitterS Oct 18 '15 at 01:43
  • I actually tried that right before i made my last comment :D. Obviously it worked. Btw, if you are looking forward to help dummyes like me i would be happy if u add me on skype : id: demonminecraft (age : 17), really really old skype id xD – n0thing15 Oct 18 '15 at 02:27