Bool Function Switch Validation

Hi guys, I'm working on a project and I've hit a roadblock. In specific, I'm trying to validate user input VIA a Boolean function. There's a myriad of ways this can be done, but my professor's instructions are pretty specific.

I need guidance pertaining to part 4 - and I'm open to suggestions regarding any segments of my code.

Directions here:


1. This program should meet ALL of the requirements of project 4. If you did not complete project4, or your project 4 did not meet all the requirements on the grading rubric, review it and complete it. If it is easier for you to start from scratch on this project you may, as long as all of the project 4 requirements are met.

2. Create a void function called get_package(). This function has the following requirements: a. This function gets the package from the user and assigns it to a global variable. b. It should do NOTHING other than that

3. Create a void function called get_comics(). This function has the following requirements: a. This function gets the number of comics from the user and assigns it to a global variable. b. It should do NOTHING other than that

4. Create a bool function called validate_input(). This function has the following requirements: a. This function should call get_package() and get_comics() b. It should employ a switch. c. If the package and number of comics are valid, the function should return true, otherwise, it should return false

My code here:


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
  
#include <iostream>
using namespace std;
                            
                            /* Function declarations */

  void get_package(char& a); // gets the package from user
  void get_comics(int& quantity); // gets the number of comics 
  bool validate_input(); // checks input of packages and quantity
  double calculate_package_cost(int a, char b);
  void display_cost();
  double compare_packages();
  void display_alternatives();
   
   
                           /* Global variables */
 char packageChosen;
 int numOfComics;
 const double PACKAGEA = 9.95;
 const double PACKAGEAFEES = 2.00; // this constant declared represents the 2.00 cost of each comic after initial purchase..
 const double PACKAGEB = 19.95;
 const double PACKAGEBFEES = 1.00; // this constant declared r epresents the 1.00 cost of each comic after initial purchase..
 const double PACKAGEC = 39.95;
                // no fees for package c..  get_comics(numOfComics);
   
      
      
 int main(void){
     

                
   get_package(packageChosen);
   get_comics(numOfComics);
   display_cost();
               
   return 0;
      }
   
   
   
void get_package(char& a){ 
     cout << "Which package are you shopping for? (enter \"A\", \"B\", \"C\", or \"Q\" to quit)" <<endl;
     cin >> a;
     }
     
void get_comics(int& quantity){
     cout << "How many comics would you like to purchase?" << endl;
     cin >> quantity;
     }
   
   
/*bool validate_input(){
       while(get_package(packageChosen) != 'a' && get_package(packageChosen) != 'A' && get_package(packageChosen) != 'b' && get_package(packageChosen) != 'B' && get_package(packageChosen) != 'c' && get_package(packageChosen) != 'C' && get_package(packageChosen) != 'q' && get_package(packageChosen) != 'Q')
          {
           cout<<"Invalid choice. Try again."<<endl;
           cout << "What package are you shopping for?" <<endl;
           cin >> package; // asking for user input to store in package..
           }
      }  */
      
double calculate_package_cost(int comicsNum, char packageType){

double total;
   
 switch(packageType){
   case 'a':
   case 'A':
        if(comicsNum <= 10){
          total = PACKAGEA;
           return total;
          }
            else{
             total = PACKAGEAFEES * (comicsNum - 10) + PACKAGEA;
              return total;
             }
         break;
         
   case 'b':
   case 'B':
        if(comicsNum <= 10){
          total = PACKAGEB;
           return total;
          }
            else{
             total = PACKAGEBFEES * (comicsNum - 10) + PACKAGEB;
              return total;
             }
          break;
          
   case 'c':
   case 'C':
           total = PACKAGEC;
            return total;
          break;

}

}

void display_cost(){
cout << calculate_package_cost(numOfComics, packageChosen);
return;
}

double compare_packages(){


}





I originally intended on calling my functions get_comics and get_package in the main function to initialize my global variables - which worked fine until I read that the same functions need to be called in the bool validation function in step 4. If I were to take this approach, my main would come out to be:

1
2
3
4
5
6
7
8
9
10
11
int main void(){

get_package(parameters);

get_comics(parameters);

validate_input(parameters);

return;

}


If I were to continue on this route, my functions get_package and get_comics would prompt for the user input, then validate_input would redundantly do the same if I called the functions inside the bool function. To circumvent this issue would I only call validate_input() and use that solely to initialize my variables, rather than calling all 3 in main?

In short, I'm confused about the procedural calling of the function
Last edited on
Topic archived. No new replies allowed.