New to C - need help with a simple prog.

This is a discussion on New to C - need help with a simple prog. within the C Programming forums, part of the General Programming Boards category; I've read a few guides, copied and pasted example codes to test them, and even edited them to see how ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    New to C - need help with a simple prog.

    I've read a few guides, copied and pasted example codes to test them, and even edited them to see how much I can change it without messing it up, but tonight I tried writing my own program from scratch. It compiles fine, but there's some problem in the execution. It's just a little program to impress my mother and ask if she wants a Dr Pepper float

    Code:
    #include <stdio.h>
    char Choice;
    char n;
    char y;
    int main()
    {
    printf("Would you like a Dr. Pepper float? (y/n)\n");
    scanf("%c\n", &Choice);
    if (Choice==y)
    {
    printf("Coming right up!\n");
    }
    else if (Choice==n)
    {
    printf("Ok. Whenever you want one, just call.\n");
    }
    return(0);
    }
    The output:
    Code:
    [milan@localhost ~]$ gcc drp.c -o drp
    [milan@localhost ~]$ ./drp
    Would you like a Dr. Pepper float?
    y
    y
    [milan@localhost ~]$ ./drp
    Would you like a Dr. Pepper float?
    n
    n
    [milan@localhost ~]$
    What I intended is for her to type y/n and see "Coming right up"/"Ok...".
    Instead, I have to type y or n twice and the program then terminates. I imagine this is some absurdly simple thing I've done wrong...

    Oh, another problem I've been having: I tried to use
    gcc prog.c -o prog.exe
    to compile a program to an executable so my mother could run it on her Windows machine, but all she gets is a DOS prompt that sits there and does nothing. Am I not able to compile to an exe on a Linux box?

  2. #2
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    Code:
    scanf("%c\n", &Choice);
    that is probably your problem, you are trying to write 2 bytes into the address of Choice which is only 1 byte big (1 character = 1 byte). remove the \n in that statement. I am surprised gcc even accepted that. otherwise your code looks fine.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, that's not the problem. What they have will try to take 1 character and a newline as a pattern, storing the character. That's fine, assuming you're actually only entering 1 character and hitting enter. The problem is that the variable y and the variable n are not the character 'y' and 'n'. What they really should have is something like:
    Code:
    if( Choice == 'y' )
    Which compares the value stored in the variable Choice with the character y, not the variable y. Two very different things.


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

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    That takes care of one problem - I only have to type y or n once now. But the program still terminates without outputting either of the strings I specified. Take away one of the Ys or Ns in the original output I posted and that's what I got.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    Sorry, quzah, I was typing up a reply before yours got posted. That helped a lot - it runs perfectly now. I should have thought of that in the first place, especially since someone had already mentioned that to me...
    Thanks for the help.

  6. #6
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    hah, surprised I didn't notice that. the variables y and n have no values .

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. Need help in a fairly simple C prog
    By CodeSlow in forum C Programming
    Replies: 4
    Last Post: 01-04-2008, 01:49 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Help on simple text console prog...
    By Azh321 in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2003, 06:03 AM
  5. unable to get simple program working
    By toom in forum Linux Programming
    Replies: 1
    Last Post: 10-11-2003, 05:05 AM

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