LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   smiley face escape sequence problem (http://www.linuxquestions.org/questions/programming-9/smiley-face-escape-sequence-problem-86399/)

mandrakeroot 08-27-2003 06:03 PM

STILL NEED HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
Im having some trouble with a program ive been trying to make.
I need to compile this:
Code:

using namespace std;
#include <iostream>
#include <iomanip>

int main()
{
  cout <<"    *****    " << endl;
  cout <<"  *    *  " << endl;
  cout <<"  * -  - *  " << endl;
  cout <<" *  o  o  * " << endl;
  cout <<"*    |    *" << endl;
  cout <<" *    +    * " << endl;
  cout <<"  * \___/ *  " << endl;
  cout <<"  *    *  " << endl;
  cout <<"    *****    " << endl;
  cout <<"  "happy"  " << endl;
  return 0;
}

but when trying to compile i get this:
Code:

tux@localhost tux $ g++ ex12.cpp
ex12.cpp:13:10: warning: unknown escape sequence '\_'
ex12.cpp: In function `int main()':
ex12.cpp:16: syntax error before string constant

Help would be appreciated!

SMB3Master 08-27-2003 06:13 PM

oh...

mandrakeroot 08-27-2003 06:18 PM

Code:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  cout <<"    *****    " << endl;
  cout <<"  *    *  " << endl;
  cout <<"  * -  - *  " << endl;
  cout <<" *  o  o  * " << endl;
  cout <<"*    |    *" << endl;
  cout <<" *    +    * " << endl;
  cout <<"  * \\___// *  " << endl;
  cout <<"  *    *  " << endl;
  cout <<"    *****    " << endl;
  cout <<"  "happy"  " << endl;
  return 0;
}

I still get compile erros:
Code:

tux@localhost tux $ g++ ex12.cpp
ex12.cpp: In function `int main()':
ex12.cpp:16: syntax error before string constant
tux@localhost tux $

I dont know what is wrong

kev82 08-27-2003 06:41 PM

try
Code:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  cout <<"    *****    " << endl;
  cout <<"  *    *  " << endl;
  cout <<"  * -  - *  " << endl;
  cout <<" *  o  o  * " << endl;
  cout <<"*    |    *" << endl;
  cout <<" *    +    * " << endl;
  cout <<"  * \\___/ *  " << endl;
  cout <<"  *    *  " << endl;
  cout <<"    *****    " << endl;
  cout <<"  \"happy\"  " << endl;
  return 0;
}


Proud 08-28-2003 07:23 PM

// sequence is a comment, no?

jinksys 08-29-2003 01:26 PM

I have your answer...
 
I tried to include my own version of your fixed source, but everytime i previewed my post...the code's formatting was lost and yes, i did try to include it as code. So when I refer to a line number, count down your code - with the first include being line #1.

The first fix was on line #14, the smile was confusing the compiler. You see, on the first try you entered in \__/ which translates to the escape sequence \_ (which doesnt exist) followed by underscores and finally a / . This is not what you wanted. So in order to include a \ character into a string statement you need to use the escape sequence \\ which translates into a single \ .

That takes care of the smile, next we tackle the "syntax error before string constant" error. That error is on line # 17. You had this: cout << " "happy" " << endl; what you wanted was the word happy with quotes, with white space on both sides. But when the compiler sees this, it thinks happy is a variable since it is outside of the two sets of quotes! It thinks you meant this: cout <<" " << happy << " " << endl ; But you just goofed up and didnt put that other <<operator in! What you needed were two \" escape sequences before and after the happy to print two quotes and not end your string.

I hope this helps!

:cool:

jinksys 08-29-2003 01:27 PM

oops
 
Seems like someone already answered the question!
Oh well.

Tarts 09-04-2003 10:37 PM

Never cared for the smiley face algorithm's i've seen, in book's nor in actual code, for me, despite the many book's, which, in fact, had little to do with smiley face's, never gave me a sense of tightness that i was looking for in smiley face code, oh we'll. :D

kev82 09-05-2003 06:11 AM

if by tightness you mean compactness then how about this
Code:

#include <iostream>
/*
  0x81006600423c0081 badly drawn smiley face
  0x383810fe10284482 man standing
*/
int main()
{
    for(int bit=63;bit>=0;bit--) {
        if((long long)1<<bit & 0x81006600423c0081) std::cout<<'#'; else std::cout<<' ';
        if(!(bit % 8)) std::cout << std::endl;
    }
    return 0;
}

ive included a man standing because he looks so much better than the crap smiley face i did(need a bigger datatype something with maybe 144 bits) the only reason i used c++ was so i could declare bit inside the for loop.

jinksys 09-06-2003 01:53 AM

I try to compile your code, kev82, and it gives me this.

Code:

smile.cpp: In function `int main()':
smile.cpp:9: error: integer constant is too large for "long" type


kev82 09-06-2003 05:30 AM

you must have quite a pedantic compiler, on line 9 put ll after the hex number, ie
Code:

if((long long)1<<bit & 0x81006600423c0081ll)
if that doesnt work i suggest you try a different gcc, there have been errors reported in gcc 3.3. ive compiled it on gcc 3.2.1 and sun's C++ compiler(wont tell me version) no problem but on gcc 2.95.2 the ll was needed.

jinksys 09-06-2003 05:47 AM

Yes, my compiler is gcc 3.3. I added ll and it works now.

Tarts 09-06-2003 07:11 PM

Quote:

Originally posted by kev82
if by tightness you mean compactness then how about this
Code:

#include <iostream>
/*
  0x81006600423c0081 badly drawn smiley face
  0x383810fe10284482 man standing
*/
int main()
{
    for(int bit=63;bit>=0;bit--) {
        if((long long)1<<bit & 0x81006600423c0081) std::cout<<'#'; else std::cout<<' ';
        if(!(bit % 8)) std::cout << std::endl;
    }
    return 0;
}

ive included a man standing because he looks so much better than the crap smiley face i did(need a bigger datatype something with maybe 144 bits) the only reason i used c++ was so i could declare bit inside the for loop.
Hi kev, that's quite complex, i like....but mabey something a little more 'C' shall we...
Code:

int main(int argc, argv*)
{
        if (argv[0] == '--help')
        printf("Please enter your mood, :( , :P , ;));
        else if (argv[1] == ':(' || argv[1] == ':P' || argv[1] == ';)')
        printf("\n\n\n:)\n\n\n");
        else
        printf("Bad Commandline Argument, please type '--help' for assistance");
        return 0;
}

I didn't compile this code, so excuse any mistake's, but hopefully you get the genreal idea. :D


All times are GMT -5. The time now is 06:00 AM.