I'm a C++ noob. I've been learning functions and ran into something that I would like my program to do but do not really see how. I'm sure it can be done, just perhaps not in the way that I have in mind.
This is a program that lets the user add or subtract integers (or end the program). As you will note from the code, I provide an error message, but the error message appears in every sub-block of code -- which seems redundant to me and therefore ripe for a function. What I am not seeing is how to put the error message in a function but then to get the program back on track once the user input is acceptable.
Please note also, like I said, I am noob, so any general helpful comments on my coding style is also appreciated.
What i would do is enclose everything after your first integer definitions in a while loop. Loop that until the user enters 0. Then use switch states for the add/subtract. Actualy, let me just show you.
// Program gives the user an option to
// add or subtract two integers
// Program runs until user enters "0"
#include <iostream>
usingnamespace std;
int nAddition (int nNum1, int nNum2);
int nSubtraction (int nNum1, int nNum2);
int main()
{
int nValue1, nValue2;
int nOption = 1;
while(nOption != 0)
{
// initial user choice interface
cout << endl;
cout << "Press 1 to add two integers or \n";
cout << "Press 2 to subtract two integers: \n";
cout << "<Press 0 to end program> \n";
cin >> nOption;
cout << " \n";
if(nOption < 0 || nOption > 3) // error message
{
cout << "<error> \n";
cout << "You did not enter a valid choice";
continue; //Bring the program back to the
//top of the while loop
}
switch(nOption)
{
case 1: // Add
cout << "You have chosen to add two integers. \n";
cout << " \n";
cout << "Enter first integer: ";
cin >> nValue1;
cout << " \n";
cout << "Enter second integer: ";
cin >> nValue2;
cout << "\n";
cout << "The sum of " << nValue1 << " and ";
cout << nValue2 << " is: ";
cout << nAddition(nValue1, nValue2) << "\n";
cout << " \n";
break;
case 2: // Subtract
cout << "You have chosen to subtract two integer. \n";
cout << "\n";
cout << "Enter first integer: ";
cin >> nValue1;
cout << " \n";
cout << "Enter second integer: ";
cin >> nValue2;
cout << " \n";
cout << "The difference of " << nValue1 << " and \n";
cout << nValue2 << " is: " << nSubtraction(nValue1, nValue2);
cout << " \n";
break;
case 0:
cout << "Program now exiting...";
nOption = 0;
}
}
system("PAUSE");
return 0;
}
// addition function
int nAddition(int nNum1, int nNum2)
{
int nValue;
nValue = nNum1 + nNum2;
return(nValue);
}
// subtraction function
int nSubtraction(int nNum1, int nNum2)
{
int nValue;
nValue = nNum1 - nNum2;
return(nValue);
}
Thank you. I was not familiar with "continue". Using "switch" also seems to me to better capture the logic of the program, as well as make the program more concise.
I initalize nOption to 1 because of while(nOption != 0)
If i dont initilize nOption to anything then the while loop doesnt work and i get errors. And if i put it to zero then the while loop will never loop. I guess i could have initialized it to zero and made it a Do While loop but i didn't think about that.
I dont know if its considered bad form, but usually on something really basic like adding or subtracting and the operation is obvious i eliminate the extra copy to the variable like so.
1 2 3 4
int nAddition(int nNum1, int nNum2)
{
return (nNum1 + nNum2);
}
acorn is correct, his is more efficient, though perhaps the compiler will optimise out the redundant variables anyhow...
If we're really talking about efficiency (and indeed just concise code), then there is no real point having addition and subtraction functions. It is easier, more efficient and more versatile to simply write the operator.
However, I guess you were just using these as examples to practise with functions. Just remember that functions that simply wrap existing operators are not worthwhile in full programs if they don't add any new functionality.