Functions :'(

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//i was trying the use the functions for this particual program but i keep //getting erros in it even after reading the textbook can you please help me //thank you.

#include <iostream>

using namespace std;
//prototyping all the functions
void PoundstoNewtons();
void FeettoMeters();
void MilestoKilometers();
void MenuAndSelection(char choice);
void doSelection(char selection);


//calling the functions
int main()
{
    cout<<MenuAndSelection();





    return 0;
}

 //Givint the user a choice for a selection in a function
        void MenuAndSelection(char choice){
cout<<"P  Pounds to Newtons"<<endl<<"F  Feet to Meters"<<endl<<"M  miles to Kilometers"<<endl;
cin>>choice;
}



      //using a switch to take the characters from the choice and directiong it to the functions
         void doSelection(char selection){
switch (selection){

case 'P':
              PoundstoNewtons();
                           break;
case 'F':
              FeettoMeters();
                           break;
case 'M':
              MilestoKilometers();
                           break;
default:
            cout<<"There is no such choice"<<endl;

break;
                  }
                              }






//function fot pounds to newtons
 void PoundstoNewtons(){

     const double NewtonPP = 4.9;
     double Pounds = 0.0;
     double Newtons = 0.0;

                    cout<<"Plese enter a value for pound:";
                    cin>>Pounds;

                    Newtons=Pounds*NewtonPP;

                    cout<<"The valuse in pounds which you entered is:"<<Pounds<<endl;
                    cout<<"The value in Newtons is:"<<Newtons<<endl;
                    }


//function for feet to meters
void FeettoMeters(){

     const double FeetPP = 3.28;
     double Feet = 0.0;
     double Meters = 0.0;

                    cout<<"Plese enter a value for Meters:";
                    cin>>Meters;

                    Feet=Meters*FeetPP;

                    cout<<"The valuse in Meters which you entered is:"<<Meters<<endl;
                    cout<<"The value in Feet is:"<<Feet<<endl;
                    }


   
             //function for miles to kilometers
void MilestoKilometers(){

     const double Milespp = 1.61;
     double Miles = 0.0;
     double Kilometers = 0.0;

                    cout<<"Plese enter a value for Kilometers:";
                    cin>>Kilometers;

                    Miles=Kilometers*Milespp;

                    cout<<"The valuse in Kilometers which you entered is:"<<Kilometers<<endl;
                    cout<<"The value in Miles is:"<<Miles<<endl;
                    }



just compile it and read the error message

line 17 MenuAndSelection: function doesnt accept 0 arguments


if you have a look at line 10
void MenuAndSelection(char choice);


that means you need to call the function with something like this:

1
2
3
4
int main()
{
   MenuAndSelection('P');
}
Last edited on
Ok i got that but how do i link everything together like as if i wasnt using functions ?
What do you mean with "as lf i wasnt using functions ?" Maybe you can try instead of prototyping the functuions, declare them above the int main () ?
@alex
he is declaring them under the main(). this also works ;)
Add a char to the main and pass by reference to MenuAndSelection();

Then pass this value to the other functions.
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>

using namespace std;
//prototyping all the functions
void PoundstoNewtons();
void FeettoMeters();
void MilestoKilometers();
void MenuAndSelection(char choice);
void doSelection(char selection);


//calling the functions
int main()
{
    MenuAndSelection('M');
         doSelection('M'); //i want it to run without me enter a char variable
                                     //i want it to get the value from MenuandSelection
                                      //and use it in doSelection instead of 'M'




    return 0;
}

 //Givint the user a choice for a selection in a function
        void MenuAndSelection(char choice){
cout<<"P  Pounds to Newtons"<<endl<<"F  Feet to Meters"<<endl<<"M  miles to Kilometers"<<endl<<"Please enter your choice from the menu above:";
cin>>choice;
}



      //using a switch to take the characters from the choice and directiong it to the functions
         void doSelection(char selection){
switch (selection){

case 'P':
              PoundstoNewtons();
                           break;
case 'F':
              FeettoMeters();
                           break;
case 'M':
              MilestoKilometers();
                           break;
default:
            cout<<"There is no such choice"<<endl;

break;
                  }
                              }






//function fot pounds to newtons
 void PoundstoNewtons(){

     const double NewtonPP = 4.9;
     double Pounds = 0.0;
     double Newtons = 0.0;

                    cout<<"Plese enter a value for pound:";
                    cin>>Pounds;

                    Newtons=Pounds*NewtonPP;

                    cout<<"The valuse in pounds which you entered is:"<<Pounds<<endl;
                    cout<<"The value in Newtons is:"<<Newtons<<endl;
                    }


//function for feet to meters
void FeettoMeters(){

     const double FeetPP = 3.28;
     double Feet = 0.0;
     double Meters = 0.0;

                    cout<<"Plese enter a value for Meters:";
                    cin>>Meters;

                    Feet=Meters*FeetPP;

                    cout<<"The valuse in Meters which you entered is:"<<Meters<<endl;
                    cout<<"The value in Feet is:"<<Feet<<endl;
                    }



             //function for miles to kilometers
void MilestoKilometers(){

     const double Milespp = 1.61;
     double Miles = 0.0;
     double Kilometers = 0.0;

                    cout<<"Plese enter a value for Kilometers:";
                    cin>>Kilometers;

                    Miles=Kilometers*Milespp;

                    cout<<"The valuse in Kilometers which you entered is:"<<Kilometers<<endl;
                    cout<<"The value in Miles is:"<<Miles<<endl;
                    }















I understand.

1
2
3
4
5
6
7
8
//calling the functions
int main()
{
   char Menu = 0;                  //the char that you will pass to the functions
    MenuAndSelection(Menu);
    doSelection(Menu); 
    return 0;
};


Then you will need to modify your functions to take a reference to a char:

1
2
void MenuAndSelection(char& choice);
void doSelection(char& selection);

Last edited on
I see Thank you very much :)
Topic archived. No new replies allowed.