0

Was coding on linux(centos4.8 Server edition) and constantly getting error Inspite of the right code. The code displayed below is not copied and pasted from anywhere coz I am typing it hence no chance of entering junk characters. The Code is as follows: -

#include <iostream>                               // L:1
using namespace std;                              // L:2

int main()                                        // L:3
{
    int i;                                        // L:4
    cout << "Enter the no: - ";                   // L:5
        cin >> i;                                 // L:6
        cout << "The number entered is: - " << i; // L:7
        return 0;                                 // L:8
}

It gives me error as follows: - 
line 5: error:  stray '\194' in program
line 5: error:  stray '\168' in program
line 5: error:  `Enter' was not declared in this scope
line 5: error:  expected `;' before "the"
line 5: error:  stray '\' in program
line 5: error:  stray '\194' in program
line 5: error:  stray '\168' in program
line 7: error:  `The' was not declared in this scope
line 7: error:  expected `;' before "number"
line 7: error:  stray '\194' in program
line 7: error:  stray '\168' in program

KINDLY NOTE: - I HAD TO TYPE "s (Double Quotes) Button twice to get it printed on screen everytime while enclosing string characters.. I understand that error has got to do with parsing miscalculation hence its giving the stray character and ending it in wrong token, so i viewed the HEXCODE format of the same Source Code and deleted on the junk character C2(hexadecimal) and after removing the same all the errors regarding: -

error:  stray '\194' in program 

vanished but rest were still remaining.

My GCC compiler version is:

g++ -v(command)
... skipping the option details
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-11)
sehe
  • 374,641
  • 47
  • 450
  • 633
dev roy
  • 7
  • 3
  • 3
    First, your `g++` compiler is awfully old and probably useless. Use a recent one (e.g. GCC 4.8); then use a good editor, able to show you the strange characters in your file. – Basile Starynkevitch Oct 17 '13 at 11:55
  • 1
    You spelled namespace wrong, at least. – ta.speot.is Oct 17 '13 at 11:56
  • Thanks ta.speot.is :-) but it is just misspelled on this! – dev roy Oct 17 '13 at 12:06
  • Disable dead-key composing in your terminal's host OS keyboard settings. Not that it should lead to _this_ but at least it will make typing code less painful – sehe Oct 17 '13 at 12:16
  • Thanks Basile for answering :-) But how the compiler problem related to double typing of the "s(Double quotes) Button? Are you saying that may be compiler is ignoring the key pressing event of the (Double Qoutes) Button if that so then how come its entering C2(Hex) value for parsing? Plzz suggest? In meantime will work on your solution.. – dev roy Oct 17 '13 at 12:16
  • 1
    Compilers don't deal with key presses. – R. Martinho Fernandes Oct 17 '13 at 12:17
  • Thanks Martinho for clarifying I knew the same, still in parallel will try soln of Basile, can suggest me in meantime another soln coz i have trying to find out the reason of getting the "s(Double Quotes) Button twice due to which the junk characters are generating and still dnt know why remaining errors are existing since after removing C2(Hex junk character) the Language(token) generated is right as per the C++ standard hence it should parse right and compute right but unfortunately its not happening! plz help – dev roy Oct 17 '13 at 12:30
  • UTF-8 is not handled by your old GCC 3.4; at that time (2005) UTF-8 was barely supported by Linux distributions. Your compiler is probably much older than your machine. – Basile Starynkevitch Oct 17 '13 at 12:55
  • Thanks Basile! :-) working on your solution will update if works. – dev roy Oct 17 '13 at 13:23
  • 3
    C2 is not junk. C2 (194) is part of the sequence C2A8, (A8 is 168), and removing it makes it worse because it breaks that sequence. Now, the sequence C2A8 is the UTF-8 sequence for A8, the diaresis character: `¨`. This is not a double quotes character. It's a diacritic, and thus occurs together with a letter, not alone. Like this: `ä`. It is sometimes entered with dead keys, meaning you press the diaresis key once, nothing shows, and then you press the `a` key and `ä` shows up. That's probably why pressing it once does nothing. – R. Martinho Fernandes Oct 17 '13 at 13:56
  • I suggest: 1) check you are pressing the right key 2) check your system keyboard layout. – R. Martinho Fernandes Oct 17 '13 at 13:56
  • Thank you! so so so so verrrrry much Martinho! I owe u mann, the problem is solved now can complete my whole code. @Basile also implementing your solution too avoid future problems thank u. Thank you all for ur time and contribution and the one who gave me -2 atleast be bold enough to say why so.., so that i can improve since i am not so much into "reputation stuff", I asked since it was my genuine doubt & so was stuck in my work. Thank you all once again. c ya. Me Back to Code..!!! – dev roy Oct 17 '13 at 15:06
  • @dev roy: I did downvote your question (after giving some constructive comments) because I feel you did not dig enough the useful compiler diagnostics. (Googling for `gcc stray in program` gives immediately relevant answers). – Basile Starynkevitch Oct 17 '13 at 18:47
  • possible duplicate of [Why my .c coding can't be compile in GCC?](http://stackoverflow.com/questions/10168659/why-my-c-coding-cant-be-compile-in-gcc) – Basile Starynkevitch Oct 17 '13 at 18:51
  • Thanks Basile! for ur soln! But it couldn't had solved the problem since the compiler parsing technique(like LALR.. etc) for grammar were already fine. I do agree with u that upgrade to compiler(GCC 4.8 as suggested by u) will support modern style C++ programming construct better making it fast, but problem was not old compiler, problem still will remain same with new compiler since "diaresis character"(as said by Marthino) was the problem bcoz keyboard was inserting it due to which it was been parsed by the compiler leading to the problem. – dev roy Oct 18 '13 at 07:50
  • @Basile:- Proof of it is that in-spite of old compiler(GCC 3.4.6) now the problem is solved. Also not many google results referred to "error:\194 and error:\168". Still genuinely thanks again coz of u i am upgrading ur compiler which will help in near & far future... to execute code faster :-) c ya! – dev roy Oct 18 '13 at 07:50
  • Handling UTF-8 is not a parsing issue, but a lexical tokenization issue, in the GCC compiler. And 3.4.6 should not be expected to know about UTF-8; you should give it only ASCII characters! (with a code less than 0x7f). – Basile Starynkevitch Oct 18 '13 at 07:52
  • @BasileStarynkevitch:- Thanks! I am implementing new compiler and then will re-test on same problem, will update if it works! :-) c ya! – dev roy Oct 18 '13 at 07:59
  • You are *installing* a new version of the *existing* GCC compiler; Implementing a new compiler is incredibly difficult (recent GCC is more than ten millions lines of source code). Your lifetime won't be enough to implement, as a single person, a competitive compiler to GCC.... And even the latest GCC won't accept strange characters (outside of comments and literal strings) in your source code, because the language standards forbid them. – Basile Starynkevitch Oct 18 '13 at 08:02

1 Answers1

3

Your file has a bad encoding.

Most likely, you pasted "fancy quotes" (like ʺ ˝ ˮ ˵ ˶ ̈ ̋ ̎ ̏) from a web-page.

Save it as latin1 or UTF-8 and make sure you pasted no funny characters.

Fixing the typo in namespace, you'd be good to go:

#include<iostream>
using namespace std;

int main()
{
    int i;
    cout << "Enter the no: - ";
    cin >> i;
    cout << "The number entered is: - " << i;

    return 0;
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks sehe! for Answering! I am actually typing code in VI thru terminal but dnt know how to do change utf-8 in it. But later I opened it in gedit and found it to be utf-8 only hence no changes required. And thus the problem remains the same. Request youto give any other solution if any..? – dev roy Oct 17 '13 at 12:10
  • Yeah. Just fix the quotes. Shouldn't be too hard (delete them and type them fresh from the keyboard). Also, in vim you can `:se fenc=latin1` – sehe Oct 17 '13 at 12:11
  • Thanks Sehe! But I have done this retyping multiple times but its the same and thats the problem every time the "s(Double Quote) Button has to be pressed twice due to which the C2 hexadecimal character is generated and even after removing that the problem is not solved! Can you plz suggest something else coz I have thousands lines to be coded and its not working at the basic step. – dev roy Oct 17 '13 at 12:21
  • Buy a better editor, copy files remotely, reinstall your PC. If you can't edit text, your problem is no longer a programming problem and you need to go [SU] or some place else. – sehe Oct 17 '13 at 13:49
  • Thanks Sehe! I am the super user and problem is with that login only. – dev roy Oct 17 '13 at 14:30