Void Function

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.
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
#include <iostream>
#include <string>

using namespace 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;
        }   
    else if ((integer !=0) && (integer != even)) {
        cout << "and odd." << endl;    
              }     
        }

}


Tips and pointers welcome.
Last edited on
If you haven't solved your issues yet:
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
// 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>

using namespace 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;
   }
}
Topic archived. No new replies allowed.