Stuck on creating and using functions

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 seanjames192  Icon User is offline

  • New D.I.C Head

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

Stuck on creating and using functions

Posted 18 July 2014 - 12:19 PM

Hello i am just doing a basic grading program that i would like modified to have a menuchoice to select one test score or multiple scores. From there it asks for the test score or scores, then calls for the average function to get the final score average from the inputted values. Then uses that final score to decide the final grade. i wanted to use an array to allow the user to enter as many test scores as they want without having to declare individual var or ints. i would like to take care of the errors first and then figure out a more simple logic for it. I feel i have overdone such a simple thing.


#include <iostream>

using namespace std;

void menuChoice(int, int);
void scoreAverage(int);
void singleTest(int);
void tests(int);

int main()

{
int choice, choice2, finalScore;


    cout << " Would you like to enter 1 test score or multiple test scores? " << endl;
    cout << " 1. Single Test Score /n 2. Multiple Test Scores " << endl;
    cin >> choice;
        menuChoice(choice, choice2);
        scoreAverage(finalScore);


return 0;
}

/* create function to allow choice for 1 test score or multiple / or look up how to enter multiple scores without needing
    pre define score1 score2 and so on <<< MAYBE LOOK UP ARRAYS ???>>>>      */

menuChoice(choice, choice2)
{
    int choice;

                singleTest(score);
            if (choice = 1)
            else if (choice = 2)
                tests();
            else
                cout << " please enter 1 or 2 for your selection " << endl;


return();
}
/* function to allow a single test score */
singleTest(score)
{
    int score;
        cout << " Please enter 1 test score " << endl;
        cin >> score;


return(score);
}
/* function to allow for multiple test scores */
tests(int)
{
    int scores[];
        cout << " Please Enter as many test scores as you would like " << endl;
        cin >> scores[];


return();
}
/* create function to get average of test scores and assign to final score */

    scoreAverage()
{
    int finalScore;

        if (finalScore > 90 )
            cout << " Congratulations! you got an A! " << endl;
        else if (finalScore < 90 )
            cout << " Congratulations! you got a B! " << endl;
        else if (finalScore <= 79 )
            cout << " You passed! you got a C! " << endl;
        else if (finalScore < 70 )
            cout << " Whoops! you got a D! " << endl;
        else (finalScore < 65 );
            cout << " You failed. You received an F " << endl;


return(finalScore);
}





Is This A Good Question/Topic? 0
  • +

Replies To: Stuck on creating and using functions

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,858
  • Joined: 25-December 09

Re: Stuck on creating and using functions

Posted 18 July 2014 - 12:38 PM

The first thing I notice is the use of default return types in your functions. That is not allowed in a C++ program you must explicitly state the return type for your functions. And remember the function implementation must exactly match the function declaration (prototype).

You may want to study the function tutorials contained in my signature. And in future if your code generates warnings or errors be sure to post the messages exactly as they appear in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 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 18 July 2014 - 01:07 PM

I will be reading the link in your signature. Here is the full code with debug errors. i will post after i read the links and try to figure a little more out.

#include <iostream>

using namespace std;

void menuChoice(int, int);
void scoreAverage(int);
void singleTest(int);
void tests(int);

int main()

{
int choice, choice2, finalScore;


    cout << " Would you like to enter 1 test score or multiple test scores? " << endl;
    cout << " 1. Single Test Score /n 2. Multiple Test Scores " << endl;
    cin >> choice;
        menuChoice(choice, choice2);
        scoreAverage(finalScore);


return 0;
}

/* create function to allow choice for 1 test score or multiple / or look up how to enter multiple scores without needing
    pre define score1 score2 and so on <<< MAYBE LOOK UP ARRAYS ???>>>>      */

menuChoice(choice, choice2)
{
    int choice;

                singleTest(score);
            if (choice = 1)
            else if (choice = 2)
                tests();
            else
                cout << " please enter 1 or 2 for your selection " << endl;


return();
}
/* function to allow a single test score */
singleTest(score)
{
    int score;
        cout << " Please enter 1 test score " << endl;
        cin >> score;


return(score);
}
/* function to allow for multiple test scores */
tests(int)
{
    int scores[];
        cout << " Please Enter as many test scores as you would like " << endl;
        cin >> scores[];


return();
}
/* create function to get average of test scores and assign to final score */

    scoreAverage()
{
    int finalScore;

        if (finalScore > 90 )
            cout << " Congratulations! you got an A! " << endl;
        else if (finalScore < 90 )
            cout << " Congratulations! you got a B! " << endl;
        else if (finalScore <= 79 )
            cout << " You passed! you got a C! " << endl;
        else if (finalScore < 70 )
            cout << " Whoops! you got a D! " << endl;
        else (finalScore < 65 );
            cout << " You failed. You received an F " << endl;


return(finalScore);
}









||=== Build: Debug in Grades (compiler: GNU GCC Compiler) ===|
main.cpp|29|error: expected constructor, destructor, or type conversion before '(' token|
main.cpp|44|error: expected constructor, destructor, or type conversion before '(' token|
main.cpp|54|error: ISO C++ forbids declaration of 'tests' with no type [-fpermissive]|
main.cpp||In function 'int tests(int)':|
main.cpp|54|error: new declaration 'int tests(int)'|
main.cpp|8|error: ambiguates old declaration 'void tests(int)'|
main.cpp|56|error: storage size of 'scores' isn't known|
main.cpp|58|error: expected primary-expression before ']' token|
main.cpp|61|error: expected primary-expression before ')' token|
main.cpp|65|error: ISO C++ forbids declaration of 'scoreAverage' with no type [-fpermissive]|
main.cpp||In function 'int tests(int)':|
main.cpp|62|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 9 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|


This post has been edited by seanjames192: 18 July 2014 - 01:10 PM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,858
  • Joined: 25-December 09

Re: Stuck on creating and using functions

Posted 18 July 2014 - 01:21 PM

The tutorials should help you figure out these errors, and read the first paragraph of post #2. The problems is with the missing return types.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 dotdot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-July 14

Re: Stuck on creating and using functions

Posted 18 July 2014 - 10:41 PM

Yeah, the issues with your function returns and such were already pointed out, but you're also going to have problems with some variables later on, such as "finalScore" in your scoreAverage function. You don't pass anything in, and finalScore is a local variable to the function which you don't initialize, yet you start running your if statements to check its value.
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1705
  • View blog
  • Posts: 3,265
  • Joined: 30-May 10

Re: Stuck on creating and using functions

Posted 18 July 2014 - 10:55 PM

Start small and compile often.
http://cboard.cprogr...nt-process.html

Writing nearly 100 lines of code, then wondering what all those error messages mean isn't the way to go.
Was This Post Helpful? 0
  • +
  • -

#7 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 19 July 2014 - 11:50 AM

Hello all here is the newly written code compiled successfully through single score function. I am setting up the multiple test score function and am having problems with setting up an array to hold an unknown number of scores and then pass those scores to another function that will average the scores out into one final score.

i just want to know if it's possible to declare an array with an unknown size and then pass the contents of that array to another function to be averaged. Thank you all in advance for your input.

Here is the full code with debug errors:

#include <iostream>

using namespace std;

void choiceMenu();
void singleScore();
void multipleScores();
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;

            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)
                    cout << " Please enter a score  value of 0 through 150 " << endl;
                    cin >> score;


}

void multipleScores()
{
        int scoreArray[];

            cout << " You have chosen to enter multiple test scores " << endl;
            cout << " Please enter as many test scores as you want " << endl;
            cin >> scoreArray[];



}





||=== Build: Debug in grades2Revised (compiler: GNU GCC Compiler) ===|
main.cpp||In function 'void choiceMenu()':|
main.cpp|28|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
main.cpp|30|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
main.cpp||In function 'void multipleScores()':|
main.cpp|56|error: storage size of 'scoreArray' isn't known|
main.cpp|60|error: expected primary-expression before ']' token|
||=== Build failed: 2 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|



This post has been edited by seanjames192: 19 July 2014 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: Stuck on creating and using functions

Posted 19 July 2014 - 01:00 PM

Yes, it is possible.

There are two approaches. The correct C++ approach which is to use the std::vector class and then the "C with classes" approach to C++ which is to use new to dynamically allocate an array.
Was This Post Helpful? 0
  • +
  • -

#9 dotdot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-July 14

Re: Stuck on creating and using functions

Posted 19 July 2014 - 01:06 PM

Yeah, it's not really possible to create an array of unknown size. You could always resize it as you go, but that's pretty inefficient. Are you familiar with vectors or possibly linked lists? Those would be a much better fit than using an array. If I were you I'd read up a bit on vectors and use one in your program. They're pretty simple so it shouldn't take you long. You'll need a loop to enter in the grades and a way to break the loop (like a special number) when you're done entering grades.
Was This Post Helpful? 0
  • +
  • -

#10 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 19 July 2014 - 08:59 PM

Hello so ive looked up vectors and created the vector scores. I am having a problem with there being no match for an operator on line 70.

Any tips would be appreciated. And is it necessary to create a function prototype. if not when is it necessary?

here's the full code with debug error messages:::

#include <iostream>
#include <vector>
using namespace std;

void choiceMenu();
void singleScore();
void multipleScores();
int scoreAverage(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;

            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)
                    cout << " Please enter a score value of 0 through 150 " << endl;
                    cin >> score;
                else
                    scoreAverage(score);


}

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

            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++)
                {

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

                        scores.push_back(numScores);

                }





}







||=== Build: Debug in grades2Revised (compiler: GNU GCC Compiler) ===|
main.cpp||In function 'void choiceMenu()':|
main.cpp|30|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
main.cpp||In function 'void singleScore()':|
main.cpp|52|error: 'else' without a previous 'if'|
main.cpp||In function 'void multipleScores()':|
main.cpp|70|error: no match for 'operator>>' in 'std::cin >> scores'|

||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 1 second(s)) ===|



Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: Stuck on creating and using functions

Posted 19 July 2014 - 09:05 PM

In C and C++, you'll want to fix your errors in the order reported by the compiler because of the way some errors cascade. Only if you really know what you are doing should you skip around fixing errors.

Anyway, with that particular error, it is because there is no out of the box way to extract input from a stream directly into a vector. You can extract stream input into a vector element, but not into a whole vector. The typical solution is to extract into a variable, and push that variable value into the vector.
Was This Post Helpful? 0
  • +
  • -

#12 dotdot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-July 14

Re: Stuck on creating and using functions

Posted 19 July 2014 - 09:54 PM

Hmmm you are getting closer but there are still a few pretty simple errors if you know what you are looking for. Like Skydiver said, you'll want to store the stream input in a variable then push that variable onto the vector.

You're using an assignment operator (=) on line 30 instead of an equality operator (==) like you want to be. Also you'll need squiggly braces for the two lines after your if statement on line 49. There's some other stuff with your for loop where you take in the multiple grades and store them in the vector, but try to get the smaller errors out for now.
Was This Post Helpful? 0
  • +
  • -

#13 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 19 July 2014 - 11:05 PM

I'm still having problems with these two errors i just don't understand the concept of vectors yet. i have my book right here, but its difficult to understand. Can anyone explain in an easier way please.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: Stuck on creating and using functions

Posted 19 July 2014 - 11:41 PM

For the first error, look at your line 52 that has the else keyword. Where is the corresponding if for that else. (Hint: C++ is not like Python where indents define blocks of code. Look through your book to refresh your memory on what does define blocks of code in C++.)

As for the second error, let's say you defined a class:
class Foo
{
public:
    Foo()
        : m_value(0)
    {
    }

    void setValue(int value)
    { 
        m_value = value;
    }

    int getValue() const
    {
        return m_value;
    }

private:
    int m_value;
};



You cannot do the following:
Foo foo;
cin >> foo;
cout << foo;



This is because the class Foo didn't define an operator override for >> (or <<) that will allow you to input (or output) that object type directly. Similarly, the std::vector class doesn't define such operators as well.

So how do you set the value of an instance of Foo? Without doing the operator override, you would use a variable to hold the value that you want to set:
Foo foo;
int temp;
cin >> temp;
foo.setValue(temp);
cout << foo.getValue();


Was This Post Helpful? 0
  • +
  • -

#15 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 - 04:40 PM

Well little by little its coming along but still quite frustrating. here is the updated code and debug errors. My main question now is im given several warnings can someone explain if those should be fixed or is it because they are waiting for user input. And im getting 2 undefined reference errors. Any help is appreciated.

#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, numScores);
                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 totalScore, finalScore, score, scores, numScores;

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


    return(finalScore);



}





||=== Build: Debug in grades2Revised (compiler: GNU GCC Compiler) ===|
main.cpp||In function 'int scoreAverage()':|
main.cpp|86|warning: unused variable 'score' [-Wunused-variable]|
main.cpp||In function 'void singleScore()':|
main.cpp|50|warning: 'numScores' may be used uninitialized in this function [-Wuninitialized]|
main.cpp||In function 'int scoreAverage()':|
main.cpp|88|warning: 'scores' is used uninitialized in this function [-Wuninitialized]|
main.cpp|88|warning: 'numScores' is used uninitialized in this function [-Wuninitialized]|
main.cpp||In function 'void multipleScores()':|
main.cpp|68|warning: 'numScores' may be used uninitialized in this function [-Wuninitialized]|
main.cpp|78|warning: 'score' may be used uninitialized in this function [-Wuninitialized]|
obj\Debug\main.o||In function `Z11singleScorev':|
main.cpp|50|undefined reference to `scoreAverage(int, int)'|
obj\Debug\main.o||In function `Z14multipleScoresv':|
main.cpp|78|undefined reference to `scoreAverage(int, int)'|
||=== Build failed: 2 error(s), 6 warning(s) (0 minute(s), 1 second(s)) ===|



This post has been edited by seanjames192: 20 July 2014 - 04:41 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2