Hello again. I need to add void functions to a program, and I'm having had a hard time understanding the void functions. I was wondering if anyone can give me some input to what else I need to add. I'm having a hard time wrapping my head around it.
#include <iostream>
#include <string>
usingnamespace std;
void testInteger(int integer);
void testPosNeg(int integer);
int main()
{
int integer;
int positive;
int negative;
int odd;
int even;
bool moreNums = true;
char ynAns;
//ask user for an integer
do {
cout << "Please enter an integer." << endl;
cin >> integer;
testInteger(int integer);
testPosNeg(int integer);
//if the user inputs the wrong data
if (!cin){
cin.clear();
cin.ignore(100, '\n');
cout << "Please enter an integer." << endl;
cin >> integer;
//and they do it again....
if (!cin){
cout << "Goodbye." << endl;
return 1;
}
}
cout << "The integer you entered is " << endl;
//ask user if they would like to continue
cout << "Would you like to enter more numbers? (y/n) ";
cin >> ynAns;
cin.ignore(100, '\n');
ynAns = toupper(ynAns);
if (ynAns == 'Y')
moreNums = true;
else {
moreNums = false;
cout << "Goodbye." << endl;
}
} return 0;
}
void testInteger(int integer){
//calculate if integer is negative, positive, or zero
if (integer < 0){
cout << "negative " << endl;
}
if (integer > 0){
cout << "positive " << endl;
}
if (integer == 0){
cout << "zero." << endl;
}
}
void testPosNeg(int integer){
int even;
//calculate if integer is even or odd
if ((integer % 2== 0) && (integer != 0)){
cout << "and even." << endl;
}
elseif ((integer !=0) && (integer != even)) {
cout << "and odd." << endl;
}
}
}
// Hello again. I need to add void functions to a program, and I'm having had
// a hard time understanding the void functions. I was wondering if anyone
// can give me some input to what else I need to add. I'm having a hard time
// wrapping my head around it.
#include <iostream>
#include <string>
usingnamespace std;
void testPosNeg(int integer);
void testEvenOdd(int integer);
int main()
{
// Ask user for an integer.
// If user doesn't insert an integer, it repeats the request two times.
bool more_nums = false;
do {
int integer = 0, num_of_attempts = 3;
// Ask for an integer three times
for(; num_of_attempts > 0; num_of_attempts--) {
cout << "\nPlease enter an integer: ";
cin >> integer;
if (!cin) { // If the user inputs the wrong data...
// ... clear the input buffer.
cin.clear();
cin.ignore(100, '\n');
} else { // Otherwise, exit this for-loop.
break;
}
}
// If user didn't fail three times in inserting an integer
if(num_of_attempts > 0) {
cout << "The integer you entered is ";
testPosNeg(integer);
testEvenOdd(integer);
}
// Ask user if they would like to continue.
cout << "\nWould you like to enter more numbers? (y/n) ";
char yn_ans;
cin >> yn_ans;
yn_ans = toupper(yn_ans);
if (yn_ans == 'Y') {
more_nums = true;
}
else {
more_nums = false;
cout << "Goodbye." << endl;
}
} while(more_nums);
return 0;
}
void testPosNeg(int integer)
{
//calculate if integer is negative, positive, or zero
if (integer < 0) {
cout << "negative ";
}
if (integer > 0) {
cout << "positive ";
}
if (integer == 0) {
cout << "zero." << endl;
}
}
void testEvenOdd(int integer)
{
// Calculate if integer is even or odd
// First: it can't be zero:
if(integer == 0) {
return; // void function: 'return' without a value just stops execution.
} // We go back to the caller.
// Second: is it even?
if (integer % 2== 0) {
cout << "and even." << endl;
}
// Third: ok, it can only be odd!
else {
cout << "and odd." << endl;
}
}