Question:
C++help with void function and its use?
Kimberly Martar
2013-04-16 14:12:38 UTC
I have been working with the book and I still cannot figure out how to make this work.

This program will ask the user to enter two numbers then ask
if they want to multiply or add the two numbers and then
the program will execute and display the results using a
void function.*/

#include
#include
#include

using namespace std;

// function prototype
void product(double num1, double num2)

{ cout << product;
}
void sum(int num1, int num2)
{
cout << sum;
}
int main()
{
double num1, num2, product;
int num1, num2, sum;
char choice, again;

//Asks the user if they want to add or multiply and recieves input.
do{
cout << "What would i like to do add or multiply?\n";
cout << "Please press 'a' for add and 'm' for multiply and press enter.\n";
cin >> choice;
if ((choice == 'a') || (choice == 'A'))
// Add
{cout << "Enter the two numbers you would like to add\n";
cout << "and I will tell you the answer: ";
cin >> num1 >> num2;
sum = num1 + num2;
sum;
}
// Multiply
else ((choice == 'm') || (choice == 'M'));
cout << "Enter the two numbers you would like to multiply\n";
cout << "and I will tell you the snwer; ";
cin >> num1 >> num2;
product = num1 * num2;
product;

// Asks the user if they would like to try again.
cout << "Would you like to try again? Type 'y' yes and 'n' for no. ";
cin >> again;
}while ((again == 'y') || (again == 'Y'));
cout << endl;
system("pause");
return 0;
}
Three answers:
cja
2013-04-17 05:14:13 UTC
The first step is to pay attention to what the compiler is telling you, and fix those errors:



arith.cpp: In function `void product(double, double)':

arith.cpp:10: warning: the address of `void product(double, double)', will always evaluate as `true'

arith.cpp: In function `void sum(int, int)':

arith.cpp:14: warning: the address of `void sum(int, int)', will always evaluate as `true'

arith.cpp: In function `int main()':

arith.cpp:19: error: conflicting declaration 'int num1'

arith.cpp:18: error: 'num1' has a previous declaration as `double num1'

arith.cpp:19: error: declaration of `int num1'

arith.cpp:18: error: conflicts with previous declaration `double num1'

arith.cpp:19: error: conflicting declaration 'int num2'

arith.cpp:18: error: 'num2' has a previous declaration as `double num2'

arith.cpp:19: error: declaration of `int num2'

arith.cpp:18: error: conflicts with previous declaration `double num2'

arith.cpp:32: warning: converting to `int' from `double'

arith.cpp:33: warning: statement has no effect

arith.cpp:36: warning: statement has no effect

arith.cpp:41: warning: statement has no effect



The compiler can be very helpful to you in eliminating the nonsense from your code.



Here are some fixes I suggest you consider trying:



#include

#include

#include



using namespace std;



float product(float, float);

float sum(float, float);

void getFlt2(const string &s, char delim, float *, float *);

enum { Add = 'a', Mult = 'm', Yes = 'y' };

const string prompt("\nEnter two numbers: a, b: ");

const char delim(',');

const string opStr[] = { " + ", " * " };



int main(int argc, char *argv[]) {

    float num1, num2;

    string in, op;

 

    do {

        do {

            cout << "Enter 'a' to add, or 'm' to multiply: ";

            getline(cin, op);

            op[0] = tolower(op[0]);

        } while ((op[0] != Add) && (op[0] != Mult));



        getFlt2(prompt, delim, &num1, &num2);

        cout << num1 << opStr[op[0] == Mult] << num2 << " = ";

        if (op[0] == Add) {

            cout << sum(num1, num2);

        } else {

            cout << product(num1, num2);

        }

        cout << endl << endl << "Would you like to try again (y/n)?: ";

        getline(cin, in);

        in[0] = tolower(in[0]);

    } while (in[0] == Yes);

    return 0;

}



float product(float a, float b) {

  return a * b;

}



float sum(float a, float b) {

  return a + b;

}



void getFlt2(const string &s, char delim, float *a, float *b) {

    string in;

    stringstream ss;

    char c;

   

    do {

        cout << s;

        getline(cin,in);

        ss.clear(); ss.str(in);

    } while (!(ss >> *a >> c >> *b) || (c != delim) || ss.good());

}



#if 0



Sample run:



Enter 'a' to add, or 'm' to multiply: m



Enter two numbers: a, b: 3, 2

3 * 2 = 6



Would you like to try again (y/n)?: y

Enter 'a' to add, or 'm' to multiply: a



Enter two numbers: a, b: 4, 8

4 + 8 = 12



Would you like to try again (y/n)?: n



#endif
cs421621
2013-04-16 14:22:37 UTC
You should move the calculations to your sum and product functions. Then call those functions in main().



Example:



void subtract(int num1, int num2)

{

int difference = num1 - num2;

cout << difference;

}



call difference function in main:



subtract(num1, num2);



Hope that helps! Best of luck.
?
2016-08-09 08:22:14 UTC
When calling services in JAVA and C++, you simplest need to name it utilizing this manner: functionName(arguments); So in your application, with the intention to call that perform all you have got to do is to write down: square_func(measurement); The void square_func(int dimension) is most effective written once for declaring your operate along with its parameters (w/c in this case is int measurement) and the definition of the operate or the function block. I am hoping that make experience. Just right good fortune! =)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...