A noob need help

This is a discussion on A noob need help within the C Programming forums, part of the General Programming Boards category; Im a noob in C and I got this simple code working.. Code: #include <stdio.h> #include <conio.h> #include <string.h> int ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    A noob need help

    Im a noob in C and I got this simple code working..

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    int main ()
    {
        char str1[20]="burke",str2[20];
        
        printf("Dog's name?:");
        scanf("%s",str2);
        
        if (strcasecmp(str1, str2)==0){
        printf("Gratz you got it",str2);
        }
        else{
        printf("Wrong Answer!!!");     
        }
        
        getch();
    }
    My problem is what if instead of "burke" its "burke jr." in str1[20] coz somehow i can get it working. >.< been studying C for almost a week now I hope you guys can help me. and give me some tip,hint and advice

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    int main ()
    {
        char str1[20]="burke jr",str2[20];
        
        printf("Dog's name?:");
        scanf("%s",str2);
        
        if (strcasecmp(str1, str2)==0){
        printf("Gratz you got it",str2);
        }
        else{
        printf("Wrong Answer!!!");     
        }
        
        getch();
    }
    im getting Wrong Answer.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Read the docs for scanf()... it will stop assigning characters whenever it encounters a space...

    Look at the documentation for other ways of reading characters from the keyboard to find one that will read until carriage return... fgets() gets() etc.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    1. To use scanf, you need to put an ampersand in front of the second argument.
    2. Use fgets(str2, sizeof(str2), stdin) to get strings from the user instead of scanf. fgets with stdin stops reading input when you enter a newline, scanf stops when you enter a space.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Babkockdood View Post
    1. To use scanf, you need to put an ampersand in front of the second argument.
    2. Use fgets(str2, sizeof(str2), stdin) to get strings from the user instead of scanf. fgets with stdin stops reading input when you enter a newline, scanf stops when you enter a space.
    He doesn't want the ampersand (address of) for a string input... the array name already is a pointer.

    But, yes, fgets is probably his best bet.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    thanks guys. fgets and a simple format specifier for scanf works too as I did some research..

    Code:
    scanf("%[^\n]]s",str2)

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The difference between using scanf and fgets is that I can blow past the end of your array with my input when you use scanf.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 09:28 PM
  2. Noob Q: How to read a value of a byte in binary?
    By Hitsuin in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 03:46 PM
  3. my noob program doesnt work
    By Scarvenger in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2005, 12:40 PM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 09:49 AM
  5. noob: compiling error... plz help!
    By chosii in forum C Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21