Thread: New to C, small compiler question

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    New to C, small compiler question

    Hi, I'm wondering why this little program I wrote works on my freeware Pacific C MS-DOS compiler, but it doesn't quite work right on my Dev-C++ for win32 by Bloodshed, also freeware.

    Code:
    #include <stdio.h>
    
    
    int main() {
                      char string[10];      
                      printf("Enter a string:");
                      scanf("%s", string); 
                                                      
        for (counter = 0; counter < 10; counter++) {
                                      if (string[counter] == 'k') {
                                         printf("You typed a k!");
                                       }
                                       }
                                       getchar();
    return 0;
    }
    Everything works fine except the getchar(); when I compile it under Dev-C++. I even tried including conio.h instead of just the regular stdio.h....in the other compiler it compiles just fine with just stdio, I even looked in it, and getchar is in there. Now here's the really weird part, I tried moving the getchar command up to right above printf, just to see if it would work in another part of the program, and it did....what on earth is going on here???

    Thanks in advance, Ash

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Oh, one other small question, if anyone should know, (yes, I googled it to death first, not much luck), anyone know of a compiler that has support for some command to change the color of text? I read that C by itself doesn't support this...

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The getchar is eating the newline (enter key) you have entered when you type your string. You type a string, and hit enter. scanf takes everything before enter (assuming that's your only whitespace), and puts it in your little buffer. The newline is left in the input stream, and getchar snags it.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    thanks

    Thanks much for the reply, I went ahead and added another getchar, and you were right, now it works fine. I guess I'll just have to keep in mind however that it's not neccessary to add it if I compile using the other compiler.

    Thanks again,

    Ash

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    I'll just have to keep in mind however that it's not neccessary to add it if I compile using the other compiler.
    If you plan to write portable programs, you should stop being compiler specific in your approach. The problem with your program is that the stdin buffer(input buffer) is not being cleared after the scanf() call. If you want to take in strings from the user, you should use fgets(). If you want to use scanf(), then please check the faq about information to clear the input buffer. A simple line after the scanf call like
    Code:
    while((c=getchar())!='\n');
    will do the trick.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oh, one other small question, if anyone should know, (yes, I googled it to death first, not much luck), anyone know of a compiler that has support for some command to change the color of text? I read that C by itself doesn't support this...
    Obviously you didn't search the FAQ at all: http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    Code:
    while((c=getchar())!='\n');
    ->
    Code:
    int c;
    while((c = getchar()) != '\n' && c != EOF);
    For using scanf():
    How do I... (Level 2) - Flush the input buffer
    How do I... (Level 1) - How do I get a number from the user (C)
    How do I... (Level 2) - Validate user input
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    scanf("%s", string);
    There are better ways to get a string from the user. fgets() is the best (gets() is the worst!). Here's an fgets() sample:
    Code:
    #include <string.h>
    char *p;
    fgets(string, sizeof(string), stdin);
    if(p = strrchr(string, '\n')) *p = 0;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Thanks, I'll start using fgets.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    thanks

    [QUOTE=dwks]Obviously you didn't search the FAQ at all: http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    Thanks for the scanf info. Actually, I did read that, and I tried it, still didn't work.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Actually, I did read that, and I tried it, still didn't work.
    So post your attempt and state what "didn't work". Then perhaps either your mistake or the FAQ will get fixed.
    Sticking to one compiler - say dev-c++ - would be a help initially.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Quote Originally Posted by Salem
    > Actually, I did read that, and I tried it, still didn't work.
    So post your attempt and state what "didn't work". Then perhaps either your mistake or the FAQ will get fixed.
    Sticking to one compiler - say dev-c++ - would be a help initially.

    Ok, sure, the first example, the 'textcolor' example, well, I tried it with both my compilers, and neither support it. I searched on their web sites and googled to try and find if they had a comparable function, but no luck, also not in the manual that came with either compiler/editor.

    The second example does work, and I've been using it for now, which is great, but I'm sure it won't work on "true" dos programs. So instead, to make colors for "true" dos, I decided on ANSI, which you can supposedly make work on WinXP by adding "device=C:\ansi.sys" (or whatever path) to your config.nt file in windows\system32. I found that out by googling, however, even after a reboot, it still doesn't display the colored text.

    Also, if I do that whole printf("\0233") thing (I don't remember exactly what it was to type, but I tried it before from many different guides, you can tell ansi.sys isn't loaded because it spits out a bunch of weird characters.

    I hope all the above made sense, even if you can't help, you rock if you read this far :-)

  12. #12
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("\033[31mHello.\033[0m\n");
        return 0;
    }
    If your terminal supports ANSI, the above program will output Hello. in red.

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Holy $h!t, that worked! I could have sworn I tried it before and it didn't...ok now if you can please just tell me what I need to change to make different colors?

    I just realized what I did differently, I compiled it with the DOS compiler, and it worked fine. Before I used Dev-C++, and when I ran that exact same code, it showed weird symbols.

  14. #14
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869

  15. #15
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Great, thanks much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about a compiler warning
    By sashaKap in forum C Programming
    Replies: 4
    Last Post: 03-17-2009, 09:47 AM
  2. Small question , can you help me ??
    By Roo7 in forum C++ Programming
    Replies: 7
    Last Post: 12-19-2008, 12:23 PM
  3. compiler question
    By xddxogm3 in forum Tech Board
    Replies: 1
    Last Post: 03-14-2004, 11:47 PM
  4. C/C++ Compiler Question
    By Unregistered in forum C++ Programming
    Replies: 11
    Last Post: 07-09-2002, 02:09 AM
  5. Question: Which Compiler????
    By MaBo in forum C Programming
    Replies: 5
    Last Post: 06-04-2002, 11:57 AM