Thread: A little help please

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Hazel Park, Michigan, United States
    Posts
    15

    A little help please

    Ok so I been following this book "How to teach Yourself C"

    and its pretty old but its working and helping me learn but now im stuck on loops

    I use Cygwin to compile CodeBlocks to Write


    Code:
    #include <stdio.h>
    
    main()
    {
    	char smplChr, again;
    
    
    	again = 'A';
    	while(again == 'A')
    		{
    		printf("What char do you need the ASCII value of? > ");
    			scanf("\n%c",smplChr);
    		printf("\n The ASCII VALUE OF %c is %d\n ",smplChr,smplChr);		
    		printf("\n\nEnter "A" To Look up Another Value " );
    		printf("\nOr anyother letter if your Finsihed. ");
    			scanf("\n%c",&again);
    		}
    		
    		
    }

    Error from Compiler:

    Code:
    MrNerd@MrNerd-PC ~/HelloWorld/anscii$ gcc main.c -o main
    main.c: In function ‘main’:
    main.c:9:8: warning: missing terminating " character
    main.c:9:2: error: missing terminating " character
    main.c:10:3: error: expected expression before ‘{’ token
    main.c:19:1: error: expected expression before ‘}’ token

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > printf("\n\nEnter "A" To Look up Another Value " );

    You need to escape quotes to print them
    Code:
        printf("\n\nEnter \"A\" To Look up Another Value " );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Location
    Hazel Park, Michigan, United States
    Posts
    15
    Quote Originally Posted by Salem View Post
    > printf("\n\nEnter "A" To Look up Another Value " );

    You need to escape quotes to print them
    Code:
        printf("\n\nEnter \"A\" To Look up Another Value " );
    ok so i took them out same error...could it be my compiler?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Dunno - your error messages don't line up with your code.

    This is what I get with your code.
    $ gcc foo.c
    foo.c: In function ‘main’:
    foo.c:12:13: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
    foo.c:14:28: error: expected ‘)’ before ‘A’

    This is what I get with my fix.
    $ gcc foo.c
    foo.c: In function ‘main’:
    foo.c:12:13: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    The last remaining fix being
    scanf("\n%c",&smplChr);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Location
    Hazel Park, Michigan, United States
    Posts
    15
    Quote Originally Posted by Salem View Post
    Dunno - your error messages don't line up with your code.

    This is what I get with your code.
    $ gcc foo.c
    foo.c: In function ‘main’:
    foo.c:12:13: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
    foo.c:14:28: error: expected ‘)’ before ‘A’

    This is what I get with my fix.
    $ gcc foo.c
    foo.c: In function ‘main’:
    foo.c:12:13: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    The last remaining fix being
    scanf("\n%c",&smplChr);
    must be my compiler then what do you recomend i use?? linux or windows and what compiler with what editor
    ?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    At the command prompt, type this

    $ cat main.c
    $ gcc main.c -o main
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Location
    Hazel Park, Michigan, United States
    Posts
    15
    OUTPUT:

    Code:
            while("again == 'A')
    LOL thanks for that cat i see better on black and white!


    i might just nano from now on. thanks alot. it worked when i removed that "



    BUT when i try to run i get

    Code:
    MrNerd@MrNerd-PC ~/helloworld/anscii
    $ ./main
    What char do you need the ASCII value of? > y
    Segmentation fault (core dumped)
    
    
    MrNerd@MrNerd-PC ~/helloworld/anscii
    Last edited by MrNerd; 10-16-2012 at 02:49 AM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    replace these two lines:
    scanf("\n%c",smplChr); //&again is OK, but:

    with:
    scanf(" %c",&smplChr); //note the space before the %, no \n, and the & (ampersand), for the first scanf() line of code.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > What char do you need the ASCII value of? > y
    > Segmentation fault (core dumped)
    I take it you didn't catch the "last remaining fix" in my earlier post then.

    > LOL thanks for that cat i see better on black and white!
    Which begs the question, why wasn't your code in post #1 a COPY/PASTE from your IDE editor?

    I mean, if you've copy/pasted, the stray " would have been posted on the forum as well, and the mistake would have been blindingly obvious to us.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Location
    Hazel Park, Michigan, United States
    Posts
    15
    Quote Originally Posted by Salem View Post
    > What char do you need the ASCII value of? > y
    > Segmentation fault (core dumped)
    I take it you didn't catch the "last remaining fix" in my earlier post then.

    > LOL thanks for that cat i see better on black and white!
    Which begs the question, why wasn't your code in post #1 a COPY/PASTE from your IDE editor?

    I mean, if you've copy/pasted, the stray " would have been posted on the forum as well, and the mistake would have been blindingly obvious to us.
    i did fix it it turned out to just be the compiler crashing or something i just reinstalled codeblocks with the GNU GCC Compiler and its working fine thanks alot.

Popular pages Recent additions subscribe to a feed