A VERY simple program is confusing me..HELP PLEASE!

This is a discussion on A VERY simple program is confusing me..HELP PLEASE! within the C++ Programming forums, part of the General Programming Boards category; I am now in my first C++ class, and I am learning it fairly well, but it seems like the ...

  1. #1
    BBain80
    Guest

    A VERY simple program is confusing me..HELP PLEASE!

    I am now in my first C++ class, and I am learning it fairly well, but it seems like the simplest programs are giving me the most trouble.
    This program uses the function FindSum() , and tells the user to input positive integers (negative integer to quit), and then adds all of the positive integers together as one positive output sum. I know some of you guys are probably laughing at me right now, I know it is a simple program, but I just can't get it to output the correct numbers.

    Here is what I have so far, please feel free to run it yourself and let me know what I am doing wrong. Thanks! Brent

    #include <iostream>
    using namespace std;
    int FindSum(int, int);
    int main ()
    {
    int a, b;
    cout << "Enter a positive value (negative value to stop): ";
    cin >> a;
    b=FindSum(a,b);
    cout << "Your numbers added is: "<<b<< endl;
    return 0;
    }

    FindSum(int x, int y)
    { while (x > 0){
    cin>>x;
    y=x+y;}
    return y ;
    }

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    1
    I don't know if this is the problem but why are you passing b to FindSum? It hasn't been initialized, it's just junk...

    Also, the while loop is messed up. It will ask for a number then add it to y before checking if it's > 0. Put cin >> x; after y=x+y;. This code should work:

    #include <iostream>
    using namespace std;
    int FindSum(int);
    int main ()
    {
    int a, b;
    cout << "Enter a positive value (negative value to stop): ";
    cin >> a;
    b=FindSum(a);
    cout << "Your numbers added is: "<<b<< endl;
    return 0;
    }

    FindSum(int x)
    {
    int y = 0;
    while (x > 0){
    y=x+y;
    cin>>x;
    }
    return y ;
    }
    Last edited by Dominyo; 02-27-2002 at 04:04 PM.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    I'm guessin you want to prompt for the second varible "b" to get the sum of both. In this case b has no value when its given to the other function, so only garbage (leftover in memory location) will be given.

    EDIT: Doh, beat me, I knew I shouldn't have gone upstairs for that coke =P

    "I once saw a photograph of a large
    herd of wild elephants in Central
    Africa seeing an airplane for the first
    time, and all in a state of wild
    collective terror... As, however, there
    were no journalists among them, the
    terror died down when the airplane
    was out of sight. "


    - Bertrand Russell


  4. #4
    Unregistered
    Guest
    You need to use a return type before the name of the function in the function definition.

    Most people don't view 0 as a negative number though, so I might change the while conditional to:

    while(x >= 0);

    Also if additional user input beyond the initial input is to be given in FindSum() then we should be kind enough to ask for it:

    Here's what I would do assuming b is initialized to zero in main():

    int FindSum(int x, int y)
    {
    while (x >= 0){
    y += x;
    cout << "enter another non-negative number to continue or negative number to stop."
    cin>>x;
    }
    return y ;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 03:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  4. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  5. Help with small simple program
    By Sonor in forum C Programming
    Replies: 5
    Last Post: 04-02-2005, 07:40 AM

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