Thread: Entering Char problem

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    38

    Entering Char problem

    Hi guys, I'm new to this forms and this is my first post.
    I'm currently working on my assignment which consists of menu driven program. For one of the sections I'm required to create a ASCII to binary generator.Anyway what i would like to know is why in this section of my code it doesn't let me enter char.
    Code:
    void ascii_binary(void)
    {
         char ch[2] ={0};
        
        printf("Enter character: ");
        gets(ch);
        
        printf("You chave entered %s\n",ch);
        scanf("%s",ch);
        return;
    }
    (ps the code is not complete)
    If i run the code in a separate file and compile it, it works.
    Any help will be appreciated.
    Thanks
    SS

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I'm currently working on my assignment which consists of menu driven program. For one of the sections I'm required to create a ASCII to binary generator.Anyway what i would like to know is why in this section of my code it doesn't let me enter char.

    1) Don't use gets() - ever.
    2) Functions such as gets() and scanf() leave a newline in the input buffer. Read this.
    3) You can really just use getchar( ) if all you really want is a single character.
    4) Why are you fetching the input twice, anyway?
    5) Define "in this section of my code it doesn't let me enter char". Is it hanging, skipping, verbally refusing - what? Be specific, please.
    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;
    }

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    38
    Well after i run the code
    IT just prints "Enter character:" and goes straight into "You have entered:"

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Well after i run the code IT just prints "Enter character:" and goes straight into "You have entered:"

    Ok, well in that case the problem probably relates to #2 then.
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. char problem
    By ForlornOdium in forum C++ Programming
    Replies: 10
    Last Post: 10-29-2003, 02:39 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM