Thread: Help with design of program

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    2

    Post Help with design of program

    Im working on a program and I am having sort of a design problem with it and was wondering if I could get some ideas on how to solve this problem.

    Basicly I want to be able to type two numbers on the screen using scanf and have a add function add the two numbers together and give me the result on the screen. The problem is I don't know how to go about this. Here's what i have so far:

    Code:
    #include <stdio.h>
    
    int add(int addx, int add_y);
    int main()
    {
    	int i;
            i = add(10,5);
    
    	printf("The answer is %d\n", i);
    	return 0;
    }
    
    int add(int add_x, int add_y)
    {
    	return (add_x + add_y);
    }
    Each time you want another number, you have to manually change the number in the add function in main and re-compile it. Thats not what I want.

  2. #2
    Registered User KurtSurge's Avatar
    Join Date
    Aug 2003
    Posts
    25
    You can use cin to accomplish that. For example.

    int First;

    cout << "Please enter the first number:";
    cin >> First;
    It's too bad that stupidity isn't painful
    --Anton Szandor LaVey

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You can use cin to accomplish that.
    But in C we tend to sway in favor of scanf:
    Code:
    printf("Enter two numbers: ");
    fflush(stdout);
    
    if (scanf("%d %d", &a, &b) == 2)
        printf("%d\n", add(a, b));
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    2 questions

    Hey, Prelude, you said fflush(stdout);
    What that means/does ?

    Also in your if statement, the return value of scanf is 2?

    Thx - Polor

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    9
    Code:
    int add(int addx, int add_y);
    I believe you want that first variable to be add_x.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by AsmLover
    Code:
    int add(int addx, int add_y);
    I believe you want that first variable to be add_x.
    It doesn't matter in the prototype. Another legal proto is
    Code:
    int add(int, int);
    Specifying the variable name is unnecessary.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Prelude
    >You can use cin to accomplish that.
    But in C we tend to sway in favor of scanf...
    Not those of us who hate the problems scanf() causes

    I prefer fgets() followed by atoi() or sscanf()
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Actually I prefer writing my own keyboard interrupt handler, albeit not in console mode.



    ::ducks::

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    2
    Code:
    printf("Enter two numbers: ");
    fflush(stdout);
    
    if (scanf("%d %d", &a, &b) == 2)
        printf("%d\n", add(a, b));
    This works if I want to only add two numbers, but what if i want to add 10? Also thanks for the tip on the function prototype.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005

    Re: 2 questions

    >you said fflush(stdout);
    >What that means/does ?

    It encourages the host to actually display what the previous call to printf placed in the stdout.
    >Also in your if statement, the return value of scanf is 2?

    When you ask scanf to attempt to convert 2 values, and it does so correctly, its return value -- the number of items assigned -- is 2.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Bubba is next Bill gates.
    Saravanan.T.S.
    Beginner.

  13. #13
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    if you wanted to add 10 numbers together, you could write one to handle 10, or infinite (in theory). look at this
    http://www.cprogramming.com/tutorial/lesson17.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program design question
    By Chaplin27 in forum C++ Programming
    Replies: 1
    Last Post: 06-23-2005, 07:18 PM
  2. user input and program design
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2005, 08:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Unicode & Good Program Design :: C++
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-05-2002, 04:09 PM