Not understanding functions

Hello, how would i be able to turn this into a function?

im gonna keep the question simple because i get downvoted on every other forum i post, nobody wants to ever answer my questions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
      int option;

do
    {
        cout << "1) Open sales person file. "<< endl;
        cout << "2) Display sales person information. "<< endl;
        cout << "3) Update sales. " << endl;
        cout << "4) Get best sales person. " << endl;
        cout << "5) Exit. " << endl;

        cout << "Please enter a number 1-5 to select an option." <<endl;
        cin >> option;
        if(option == 1)
        {

        }

        else if(option == 2)
        {

        }
        else if(option == 3)
        {

        }
        else if(option == 4)
        {

        }
        else if(option == 5)
        {
            cout << "closing program."<<endl;
        }
        else
        {
            cout<<"invalid option entered. try again." << endl;
        }
    }
    while(option != 5);
It has been my experience that people here are pretty helpful as long as you don't try to get them to do your homework. :)

But you did not ask a question. What do you not understand specifically about functions?
im trying but i just dont understand why it wont work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
void optionsFunc(int);

int main()
{

cout << optionsFunc << endl;

}

void optionsFunc(int options)
{
        int option;

do
    {
        cout << "1) Open sales person file. "<< endl;
        cout << "2) Display sales person information. "<< endl;
        cout << "3) Update sales. " << endl;
        cout << "4) Get best sales person. " << endl;
        cout << "5) Exit. " << endl;

        cout << "Please enter a number 1-5 to select an option." <<endl;
        cin >> option;
        if(option == 1)
        {

        }

        else if(option == 2)
        {

        }
        else if(option == 3)
        {

        }
        else if(option == 4)
        {

        }
        else if(option == 5)
        {
            cout << "closing program."<<endl;
        }
        else
        {
            cout<<"invalid option entered. try again." << endl;
        }
    }
    while(option != 5);



    return 0;
}
@MrGoat well i dont understand what kind of function i should make it. i just put void because i saw it on a youtube video. i also dont understand how im supposed to call it in my main function. am i supposed to use cout or just put "optionsFunc" to call it?

look i really dont need you to do my homework for me, i just need a little bit more understanding where youtube videos wont help.
The reason why it won't work is because you should just be calling it as "optionsFunc();" instead of "cout << optionsFunc << endl;"

Also you do not need to pass an integer into your function because you are also declaring the same variable once you enter the function:

void optionsFunc(int options <----delete this)
{
//since you have created the "options" variable here you do not need to pass anything in
//declaring it twice is redundant and inefficient, so try to avoid those types of errors
int option;

Also, not sure if you have learned about switch statements yet, but if you have then it would be better practice to use a switch statement instead of an if-elseif-else.
Two problems:

1: line 6: You've specified the name (which resolves to the address) of the function. You're not making a function call. A function call in a cout statement looks like this, however see problem #2.
 
cout << optionsFunc() << endl;


2: Since you've declared optionsFunc() to return nothing (type void), that cout statement won't output anything (except what is output inside optionsFunc). If you want the cout statement at line 6 to output the option the user selected, optionsFunc() needs to return that value.

If you change optionsFunc() to type int, then inside optionsFunc() return the value of option, the cout at line 6 will output the value selected.
Hello, how would i be able to turn this into a function?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

void menuFunction();
using namespace std;

int main() {
    menuFunction ();
    return 0;

}

void menuFunction() {
    int option;
    do {
        cout << "1) Open sales person file. " << endl;
        cout << "2) Display sales person information. " << endl;
        cout << "3) Update sales. " << endl;
        cout << "4) Get best sales person. " << endl;
        cout << "5) Exit. " << endl;
        cout << "Please enter a number 1-5 to select an option." << endl;
        cin >> option;
        if (option == 1) {

        }
        else if (option == 2) {

        }
        else if (option == 3) {

        }
        else if (option == 4) {

        }
        else if (option == 5) {
            cout << "closing program." << endl;
        }
        else {
            cout << "invalid option entered. try again." << endl;
        }
    }
    while (option != 5);
}
well this is what ive advanced since i posted this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
//variables--------------------------------------------------------
string salesPersonName;
double saleAmount = 0.00;
double lineCount = 0.00;
//prototypes-------------------------------------------------------
int optionsFunc(int&);
int getIndexLargest(double[], int);
void displaySalesPeople(string[], double[], int);


int main()
{
    ifstream readSales;


    int options;
    optionsFunc(options);
    if(options == 1)
    {
        readSales.open("salespeople.txt");
        cout << "File read." << endl;
    }

    else if(options == 2)
    {

    }
    else if(options == 3)
    {

    }
    else if(options == 4)
    {

    }
    else if(options == 5)
    {
        cout << "closing program."<<endl;
    }
    else
    {
        cout<<"invalid option entered. try again." << endl;
    }

    return 0;

}


//functions----------------------------------------------------
int optionsFunc(int &options)
{


    do
    {
        cout << "1) Open sales person file. "<< endl;
        cout << "2) Display sales person information. "<< endl;
        cout << "3) Update sales. " << endl;
        cout << "4) Get best sales person. " << endl;
        cout << "5) Exit. " << endl;

        cout << "Please enter a number 1-5 to select an option." <<endl;
        cin >> options;

    }
    while(options != 5);


}

void displaySalesPeople(string names[], double sales[], int arrayLength)
{
    ifstream readSales;
 while(!readSales.eof())
        {
        lineCount++;
        readSales >> salesPersonName;
        readSales >> saleAmount;

        }
}


int getIndexLargest(double totalSales[], int arrayLength)
{




}


i hired a tutor for today which i hope will steer me in the right direction.

but thank you all, at least i was able to learn a couple things from this.
A few comments.

Lines 7-8: I suspect you want to make these arrays, not simple variables.

Lines 7-9: It's best practice to avoid global variables where possible. General rule is to make variables as local as possible.

Line 9: linecount is an index and should be an int, not a double.

Line 22: You've passed options by reference. That's fine, however, you've also made optionsFunc() an int function. When you're returning a value from a function, you should do it one way or the other. Generally, not a good idea to do it both ways. For simple values, I prefer using the return value.
1
2
 
options = optionsFunc ();


Lines 23-41: You should consider the use of a switch statement here.
http://www.cplusplus.com/doc/tutorial/control/

Lines 45-48: optionsFunc() should not return a value that is other than 1-5. Therefore, the else clause is unnecessary.

Line 72: The while clause is going to allow you out of the loop when any value other than 5 is chosen. This is not what you want. You want optionsFunc() to only return a valid value. Change your while clause to:
 
while (options < 1 || options > 5);


Line 73: Since optionsFunc() is declared an int function, it should return a value:
 
  return options;


Line 77: You never call displaySalesPeople(). You also never declare names or sales arrays to pass to this function.

Line 62,79: Your menu says "1. open sales file", but you're opening it here on line 79. Did you mean to open the sales file and read it into the respective arrays if 1 was selected? If so, then lines 79-86 should iterate through the arrays and you need a different function to read from the file into the arrays.

Line 82: If displaySalesPeople() is called more than once, linecount is going to go out of bounds.







Topic archived. No new replies allowed.