What am I doing wrong?

This is a discussion on What am I doing wrong? within the C Programming forums, part of the General Programming Boards category; I'm teaching myself C by reading "Practical C Programming" by Steve Oualline and doing its exercises, such as: Code: #include ...

  1. #1
    Registered User adobephile's Avatar
    Join Date
    Dec 2002
    Posts
    8

    What am I doing wrong?

    I'm teaching myself C by reading "Practical C Programming" by Steve Oualline and doing its exercises, such as:

    Code:
    #include <stdio.h>
    char	line[100];	/* line of data for input */
    int	total;		/* Runniing total of all numbers so far */
    int	item;		/* next item to add to the list */
    
    int main()
    {
        total = 0;
        while (1) {
            printf("Enter # to add \n");
            printf("  or 0 to stop:");
            
            fgets(line, sizeof(line), stdin);
            sscanf(line, "%d", &item);
            
            if (item == 0)
                break;
                
            total += item;
            printf("Total: %d\n", total);
        }
        printf("Final total %d\n", total);
        return (0);
    }
    When I run this with Mac OS X's Project Builder compiler I get this:

    No prompts. Only a blank console. When I enter "3" (return) and then "4" (return) and "0" it then lists:

    3
    4
    0
    Enter # to add
    or 0 to stop:Total: 3
    Enter # to add
    or 0 to stop:Total: 7
    Enter # to add
    or 0 to stop:Final total 7

    How do I get the "Enter # to add" prompts to appear one by one waiting for input and not at the end as above?
    Last edited by adobephile; 12-29-2002 at 05:49 AM.

  2. #2
    Registered User adobephile's Avatar
    Join Date
    Dec 2002
    Posts
    8

    That did it!

    Code:
    printf("Enter # to add \n");
            printf("  or 0 to stop:");
            fflush(stdout);
    That gave me:

    Enter # to add
    or 0 to stop:

    for each prompt.

    Enter # to add
    or 0 to stop:3
    Total: 3
    Enter # to add
    or 0 to stop:4
    Total: 7
    Enter # to add
    or 0 to stop:0
    Final total 7

    Thanks! Care to explain? How do I look up this function to learn how it works and when to use it?

    If you're not using the Project Builder compiler, what system are you on which might be similar?
    Last edited by adobephile; 12-29-2002 at 09:07 AM.

  3. #3
    Registered User adobephile's Avatar
    Join Date
    Dec 2002
    Posts
    8
    Thanks for the man pages link to freeBSD. I found my man pages and that I can access such references though such commands as: "man fflush" in my terminal app.

    Apple is calling its system Darwin. This PDF file gives a basic description.

    Once I get a grasp of C and UNIX I'm going on to Cocoa, which is a superset of C they're calling Objective C.

    There's a lot to learn, but it's all very interesting. I'm going to be creating and publishing educational software.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Objective C is not what "they're" calling it. It is what the language is called by everyone. Objective C has been around for awhile. Since Apple bought inherited all of NeXT, they got all of NeXT's Objective C OS code. I heard Apple is pushing to make Objective C able to use the C++ libraries, which should be cool assuming it hasn't happened yet.

  5. #5
    Registered User adobephile's Avatar
    Join Date
    Dec 2002
    Posts
    8
    OK. Thanks for the clarification.

    Could you state briefly the essential differences between C and C++?

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    No, but a quick google search revealed
    http://216.239.51.100/search?q=cache...hl=en&ie=UTF-8

  7. #7
    Registered User adobephile's Avatar
    Join Date
    Dec 2002
    Posts
    8
    Thanks again. Though I don't have much judgement on the relative merits of various languages, it looks like I could have done far worse than C, C++, Objective C, and Cocoa! I'd say I have a fascinating journey ahead!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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