Stuck on creating and using functions

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1240 Views - Last Post: 05 August 2014 - 06:50 PM Rate Topic: -----

#16 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: Stuck on creating and using functions

Posted 20 July 2014 - 04:50 PM

For the two errors you are getting, compare the prototype on line 8 versus the actual definition on line 84. Do you see the difference?

As for warnings, you should always try to resolve warnings. Often the warnings are correct what they are warning you about.
Was This Post Helpful? 0
  • +
  • -

#17 seanjames192  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 14

Re: Stuck on creating and using functions

Posted 20 July 2014 - 05:06 PM

ok so now i only have the warnings i took on line 84 int scoreAverage() and added int scoreAverage(int, int, same as line 8. it has helped but can you explain why? i dont remember anything from reading about it in my book. what is it under arguments or parameters? if i have to ask should i just re-read?
Was This Post Helpful? 0
  • +
  • -

#18 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: Stuck on creating and using functions

Posted 20 July 2014 - 08:09 PM

Re-read the section in your book about functions, function parameters, and function prototypes.
Was This Post Helpful? 0
  • +
  • -

#19 dotdot  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 50
  • Joined: 04-July 14

Re: Stuck on creating and using functions

Posted 20 July 2014 - 09:05 PM

Well, the compiler is getting mad at you for a few reasons according to your warnings/errors. First, you have several variables that are never given a value at all (aka "uninitialized" variables). These warnings are pretty simple because the program lists the line number and the name of the uninitialized variable.

The last two errors are because you are calling your scoreAverage function which returns an int, but you are not assigning any variable to hold the returning int. You need something like:

int returnIntValue;
returnIntValue = scoreAverage(num1, num2);



As for vectors, think of them like arrays. Just a long chain of objects that come in a package. You can add more things to the end of the vector by using vectorName.push_back(int foo) and you can remove the object at the end of the vector by saying vectorName.pop_back(). Other than that they function pretty much like an array does. You can say for instance vectorName[0] and get access to the first element in the vector, much like you would an array.
Was This Post Helpful? 0
  • +
  • -

#20 seanjames192  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 14

Re: Stuck on creating and using functions

Posted 26 July 2014 - 12:12 PM

its been a little while sorry guys, work and just been tired afterwards, Im still working on it and ill repost the newly updated code later tonight or tomarrow. Thanks for all the help guys.
Was This Post Helpful? 0
  • +
  • -

#21 seanjames192  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 14

Re: Stuck on creating and using functions

Posted 03 August 2014 - 11:48 AM

Grrrrr.... I cant find out how to fix this problem. I have no whereto look for an answer. The problem is when i select my menuchoice and enter my score/scores and press enter it doesnt take me to the next function its so frustrating. Any help is greatly appreciated and are we allowed to take screenshots and post them to help with the problems?

if you are able to run the program you will see the problem . Im just looking for the reason why it wont send me to the next function after menuchoice.
#include <iostream>
#include <vector>
using namespace std;

void choiceMenu();
void singleScore();
void multipleScores();
int scoreAverage(int, int);

int main()
{
        cout << " Welcome to a simple grading program " << endl;
        cout << " This program will take single or multiple test \n scores and then average them out and display the letter \n grade associated with that score \n " << endl;
            choiceMenu();

    return 0;
}

 void choiceMenu()
{
    int choice;

        cout << " Would you like to enter single or multiple test scores ? \n " << endl;
        cout << " 1. Single Test Score \n 2. Multiple Test Scores \n " << endl;
        cout << " Please enter 1 or 2 for you answer " << endl;
        cin >> choice;



            if (choice == 1)
                singleScore();
            else if (choice == 2)
                multipleScores();
            else (choice < 1 || choice > 2);
                cout << " Please enter a correct number choice " << endl;
                cin >> choice;


}

void singleScore()
{
        int score, numScores;

            cout << " You have chosen to enter a single test score " << endl;
            cout << " Please enter one test score " << endl;
            cin >> score;

            /* verify score is an acceptable number. allow for extra credit up to a 150 point score */
                if (score >= 0 || score <= 150)
                    scoreAverage(score);
                else
                    cout << " Please enter a score value of 0 through 150 " << endl;
                    cin >> score;




}

void multipleScores()
{
        vector<int> scores;
        int index, score, numScores;

            cout << " You have chosen to enter multiple test scores " << endl;
            cout << " Please enter as many test scores as you want " << endl;

                for(index = 0; index < numScores; index++)
                {
                    int score;

                    cout << " Score for Test # " << index + 1 << " : " << endl;
                    cin >> score;

                        scores.push_back(score);

                }
                scoreAverage(score,numScores);



}

int scoreAverage(int, int)
{
        int totalScore, finalScore, scores, score, numScores;

            totalScore = scores * numScores;
            finalScore = totalScore / numScores ;


    return(finalScore);



}


||=== Build: Debug in grades2Revised (compiler: GNU GCC Compiler) ===|
main.cpp||In function 'void singleScore()':|
main.cpp|51|error: too few arguments to function 'int scoreAverage(int, int)'|
main.cpp|8|note: declared here|
main.cpp|43|warning: unused variable 'numScores' [-Wunused-variable]|
main.cpp||In function 'int scoreAverage(int, int)':|
main.cpp|87|warning: unused variable 'score' [-Wunused-variable]|
||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 1 second(s)) ===|



Was This Post Helpful? 0
  • +
  • -

#22 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Stuck on creating and using functions

Posted 03 August 2014 - 02:40 PM

Hi, the error says that on line 51 in the main.cpp file there are too few arguments to the scoreAverage function :

main.cpp|51|error: too few arguments to function 'int scoreAverage(int, int)'|



line 51 is

51    scoreAverage(score);



The function scoreAverage is defined with two arguments :

int scoreAverage(int, int);



I take that you are passing a total and the number of scores. Since it is in singleScore you can pass 1 as the number of scores.

51    scoreAverage(score, 1);




In choiceMenu you forget an if keyword, have added an unwanted semicolon (because the if was missing the compiler likely told you a semicolon was missing). Brackets are needed to group the block of statements in the if else statement.

  else if (choice < 1 || choice > 2) //;
  {
    cout << " Please enter a correct number choice " << endl;
    cin >> choice;
  }


Was This Post Helpful? 0
  • +
  • -

#23 seanjames192  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 14

Re: Stuck on creating and using functions

Posted 03 August 2014 - 03:33 PM

I will go over these errors and post later thank you for the help
Was This Post Helpful? 0
  • +
  • -

#24 dotdot  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 50
  • Joined: 04-July 14

Re: Stuck on creating and using functions

Posted 05 August 2014 - 06:50 PM

If I were you, I'd read up on functions and passing parameters. That seems to be what you're struggling with now.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2