Thread: solution to the problem

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    46

    solution to the problem

    i want to replace the tab with \t , space with \s and back slash with
    \\, from input to output.
    i wrote the program like below. so tell me some one why it is wrong...

    Code:
    main()
    {
          int c;
          
          while((c = getchar()) != EOF) {
                   if(c == ' ') 
                         printf("\\s");
                   else if(c == '\t')
                          printf("\\t");
                    else if(c == '\')
                          printf("\\\\");
                   else
                          putchar(c);
            }
    }
    Last edited by vapanchamukhi; 09-08-2008 at 06:56 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You want to replace
    space with \s
    and your code does:
    Code:
                   if(c == ' ') 
                         printf("\\b");
    Does that seem right?

    so tell me some one why it is wrong
    It would of course help if you on your side explained "How it is different from what you expect", since we can only guess on what is not working....

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    no im sorry... but thats not what im asking. its giving errors that ' missing, } missing. so i want to know whether what i have written is write or wrong. if it is wrong then where i have done mistake....

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have a look at this line, then:
    Code:
    else if(c == '\')
    What does it actually produce?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    my intention of writing that line is if i give \ as input then that character is compared with \... isn't???
    Last edited by vapanchamukhi; 09-08-2008 at 07:13 AM.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Except that the backslash is the escape character, so you are actually escaping a single quote, which is obviously not what you want...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by vapanchamukhi View Post
    my intention of writing that line is if i give \ as input then that character is compared with \... isn't???
    mmm, yes. But the compiler will want to interpret things for you. It's uselessly clever like that It thinks you're trying to escape the second ' and hence you don't have a closing apostrophe. I think all you need is to compare c with '\\'.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    ya now 0 errors.. i compiled successfully. but now it is not giving any thing as output..

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vapanchamukhi View Post
    ya now 0 errors.. i compiled successfully. but now it is not giving any thing as output..
    That is very strange, as I would expect it to at least output back what you have input after you hit enter. In fact, it works just like I expect when I run it here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    post your code again, maybe another error crept in somewhere.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  11. #11
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    ya it works fine... i had done mistake while writing that program in vim editor... instead of assigning the value of getchar() i had used ==... thats because of that it was not showing any thing.... now it works fine...

  12. #12
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    That means you're not compiling with any of the warning flags. You should always include -Wall in your gcc commandline. That should have flagged up this problem as a warning.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  13. #13
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    ya actually i used to compile using cc not gcc. is there any such option in cc..

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vapanchamukhi View Post
    ya actually i used to compile using cc not gcc. is there any such option in cc..
    What OS are you using? If it's Linux, then cc == gcc. Otherwise, we'd need to know what compiler it is...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    ok thanks... i dint know that... i work in fedora linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Eight Queen Problem Solution!
    By chottachatri in forum C# Programming
    Replies: 2
    Last Post: 06-29-2009, 08:37 PM
  2. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  3. annoying problem, solution should be simple...
    By Captain Penguin in forum C++ Programming
    Replies: 0
    Last Post: 10-18-2002, 04:41 PM
  4. Replies: 4
    Last Post: 10-17-2002, 10:09 PM
  5. Major Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 01:06 PM