Thread: Beginner help

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

    Beginner help

    I am on the section in the tutorials about an indeterminate amount of function arguments. I have tried to change the example to allow user input and have ran into a problem with it. It is just a simple average calculator that is supposed to get user input for numbers to find the average of. It is probably an easy fix but i'm still learning and can't figure out what is wrong with the code. If i leave it as is there are no errors from the compiler. If I enter one number it returns the value "inf" if more than one the program crashes. Any help would be greatly appreciated.

    Code:
    #include <cstdarg>
    #include <iostream>
    
    using namespace std;
    
    char numbers[150];
    
    double average ( int num, ... )
    {
        va_list numbers[2];
        double sum = 0;
    
        va_start (numbers[2], num);
        for (int x=0;x<num;x++)
            sum += va_arg (numbers[2], double);
        va_end (numbers[2]);
    
        return sum / num;
    }
    int main()
    {
        cout<<"Please enter numbers to find the average): "<<endl;
        cin.getline ( numbers, 150);
        cout<<average (numbers[2])<<endl;
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_O

    Can you give me a link to the tutorial you are using?

    Soma

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are only passing one argument to average. In my opinion, variable argument lists are not appropriate here: you effectively changed the problem into one for which the solution is no longer applicable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    variable argument lists are not appropriate here
    I'm not entirely sure that they are ever appropriate.

    Soma

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    It is the tutorial on cprogramming.com. It was the first tutorial I found so I decided to go through it. Cprogramming.com Tutorial: Variable Argument Lists for Functions that is where i'm at on it. I am just using what the tutorial was using.
    If I understand correctly the string only counts as one argument. If so that would mean that I didn't even accomplish what I was going for. Well thank you for the help anyway. I will just move on with the tutorial then.
    Last edited by flyguy10269; 08-28-2010 at 12:52 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    numbers is a char array, and the first argument to average is an int. How can you NOT get a compile error? Something is not right.
    Also, you should use std::getline instead of std::cin.getline.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by phantomotap View Post
    I'm not entirely sure that they are ever appropriate.

    Soma
    In C++ maybe not, but in C have a look at printf.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But C is an ancient language stuck in 1950, and this is the C++ forum, so C is not applicable here.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ADO.NET in VS2008 or gdi+ (beginner question)
    By robgar in forum C# Programming
    Replies: 3
    Last Post: 10-04-2009, 02:40 AM
  2. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  3. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM