Thread: Function problem - too few args

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    27

    Function problem - too few args

    Hi there, i havent coded for some time now, but ive got back into it and have been trying to code this small application that will store a persons age, name, location:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    struct DB {
           int age;
           char name[20];
           char location[20];
    };
    
    void inputDB(int a, char n[20], char l[20]);
    void viewDB(DB dbstats);
    
    int main(int argc, char *argv[])
    {
        int choice;
        cout << "Welcome\n";
        cout << "1. Input Data\n";
        cout << "2. View Data\n";
        cout << "3. Quit\n";
        cin >> choice;
        
        if(choice=1){
             inputDB();
        }
        else if(choice=2){
             viewDB();
        }   
        else if(choice=3){
             return EXIT_SUCCESS;
        }
        else{
             cout << "Not an option\n";
        }      
        
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    void viewDB(DB dbstats)
    {
         cout << "Age: " << dbstats.age << endl;
         cout << "Name: " << dbstats.name << endl;
         cout << "Location: " << dbstats.location << endl;
    }
    void inputDB(int a, char n[20], char l[20])
    {
         cout << "Enter Age: ";
         cin >> a;
         cout << "\nEnter Name: ";
         cin >> n;
         cout << "\nEnter Location: ";
         cin >> l;
         DB input = {a, n[20], l[20]};
    }
    Could someone please explain to me why the compiler tells me:

    Code:
    too few arguments to function `void inputDB(int, char*, char*)
    Im a little baffled, or even a link to something that could explain this for me, thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice:
    Code:
        if(choice=1){
             inputDB();
        }
    Clearly you call inputDB() with no arguments, but you declared it to take three arguments.

    Incidentally, choice=1 in that context is probably a mistake.
    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

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Ah yup, ive changed it a bit now:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    struct DB {
           int age;
           char name[20];
           char location[20];
    };
    
    void inputDB(int a, char n[20], char l[20]);
    void viewDB(DB dbstats);
    
    int main(int argc, char *argv[])
    {
        int age;
        char name[20];
        char location[20];
        
        int choice;
        cout << "Welcome\n";
        cout << "1. Input Data\n";
        cout << "2. View Data\n";
        cout << "3. Quit\n";
        cin >> choice;
        
        if(choice==1){
             inputDB(age, name, location);
        }
        else if(choice==2){
             viewDB(age, name, location);
        }   
        else if(choice=3){
             return EXIT_SUCCESS;
        }
        else{
             cout << "Not an option\n";
        }      
        
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    void viewDB(DB dbstats)
    {
         cout << "Age: " << dbstats.age << endl;
         cout << "Name: " << dbstats.name << endl;
         cout << "Location: " << dbstats.location << endl;
    }
    void inputDB(int a, char n[20], char l[20])
    {
         cout << "Enter Age: ";
         cin >> a;
         cout << "\nEnter Name: ";
         cin >> n;
         cout << "\nEnter Location: ";
         cin >> l;
         DB input = {a, n[20], l[20]};
    }
    but i get this error: `int' to non-scalar type `DB' requested
    Last edited by dynamethod; 03-09-2008 at 06:29 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    but i get this error: `int' to non-scalar type `DB' requested
    Compare:
    Code:
    void viewDB(DB dbstats);
    with its attempted use:
    Code:
    viewDB(age, name, location);
    By the way, this line accesses two arrays out of their bounds:
    Code:
    DB input = {a, n[20], l[20]};
    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

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Ok heres what i got now, but this bit has me really stumped :S

    Error: too few arguments to function `void viewDB(DB)'

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    struct DB {
           int age;
           char name[20];
           char location[20];
    };
    
    void inputDB(int a, char n[20], char l[20]);
    void viewDB(DB dbstats);
    
    int main(int argc, char *argv[])
    {
        int age;
        char name[20];
        char location[20];
        
        int choice;
        cout << "Welcome\n";
        cout << "1. Input Data\n";
        cout << "2. View Data\n";
        cout << "3. Quit\n";
        cin >> choice;
        
        if(choice==1){
             inputDB(age, name, location);
        }
        else if(choice==2){
             viewDB();
        }   
        else if(choice==3){
             return EXIT_SUCCESS;
        }
        else{
             cout << "Not an option\n";
        }      
        
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    void viewDB(DB dbstats)
    {
         cout << "Age: " << dbstats.age << endl;
         cout << "Name: " << dbstats.name << endl;
         cout << "Location: " << dbstats.location << endl;
    }
    void inputDB(int a, char n[20], char l[20])
    {
         cout << "Enter Age: ";
         cin >> a;
         cout << "\nEnter Name: ";
         cin >> n;
         cout << "\nEnter Location: ";
         cin >> l;
         DB dbstats = {a, n[20], l[20]};
    }
    Last edited by dynamethod; 03-09-2008 at 06:48 AM. Reason: cause im stupid

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ok heres what i got now, but this bit has me really stumped :S
    Now compare:
    Code:
    void viewDB(DB dbstats);
    with its attempted use:
    Code:
    viewDB();
    Error: too few arguments to function `void viewDB(DB)'
    Do you understand the error message?
    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

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Yes sorry for this, im overlooking some pretty simple errors, I'm really bad at coding on my own, cause usually if im with someone else they'll see something thats really obvious that i cant

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    struct DB {
           int age;
           char name[20];
           char location[20];
    };
    
    void inputDB(int a, char n[20], char l[20]);
    void viewDB(DB dbstats);
    
    int main(int argc, char *argv[])
    {
        int age;
        char name[20];
        char location[20];
        
        int choice;
        cout << "Welcome\n";
        cout << "1. Input Data\n";
        cout << "2. View Data\n";
        cout << "3. Quit\n";
        cin >> choice;
        
        if(choice==1){
             inputDB(age, name, location);
        }
        else if(choice==2){
             viewDB(DB dbstats);
        }   
        else if(choice==3){
             return EXIT_SUCCESS;
        }
        else{
             cout << "Not an option\n";
        }      
        
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    void viewDB(DB dbstats)
    {
         cout << "Age: " << dbstats.age << endl;
         cout << "Name: " << dbstats.name << endl;
         cout << "Location: " << dbstats.location << endl;
    }
    void inputDB(int a, char n[20], char l[20])
    {
         cout << "Enter Age: ";
         cin >> a;
         cout << "\nEnter Name: ";
         cin >> n;
         cout << "\nEnter Location: ";
         cin >> l;
         DB dbstats = {a, n[20], l[20]};
    }
    Thats the changes, cleared up the previous error but i get:

    Code:
    Line 32: expected primary-expression before "dbstats"
    for where i declare this:

    Code:
    void viewDB(DB dbstats);
    Last edited by dynamethod; 03-09-2008 at 07:01 AM. Reason: im getting stupidider

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes sorry for this, im overlooking some pretty simple errors
    The error message you quoted left out the number of the line at which the error was detected. Use that line number to help you figure out the error.
    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

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Line 32 is not where you declare the prototype. It's where you are calling the function.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob problem - function call
    By Ultraman in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2006, 02:28 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM