Multiple functions with case selector.

Hello, this is my most difficult assignment yet. Quick Disclaimer: I am a student taking intro to programming. I have some experience in Python because that's what I'll be using/have used in the Geographic Information Systems industry. I recently went back to school to make my knowledge in GIS official.

I've made plenty of headway, but I've run into several problems. I've read several threads in this forum that were related and they could only provide so much guidance. I also read through the chapters, got help from a friend who writes code for a living (unfortunately not in C++) and I've gone over the tutorials on this web site.

The solution eludes me, but I know I'm close.

Below is my code thus far. I put in comments to help show you where I need more guidance.

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Headers and Other Technical Items

#include <iostream>
using namespace std;
#include "C:\\Users\\Alan\\Cpp_Software_Download\\Dev-Cpp\\user_library\\udst_monitor.h"



// Variables

char        user_choice;
//int         user_integer;     //This is for a future function not yet written.
int         x;

// Function prototypes

void counting_loop(){         //void is the only type that didn't return an
                              //error              
    
    cout << ".counting_loop"; //This is a test to see if my call functions work.
    }                         //When I inserted my real counting loop here it
                              //didn't work.
    
void impossible(void){
    
    cout << ".impossible";    //I haven't tried to get the rest of these to
                              //work yet.
    }
    
void missing_item(void){
    
    cout << ".missing_item";
    }
    
void odd_even(void){
    
    cout << ".odd_even";
    }
    
void poem(void){
    
    cout << ".poem";
    }



//******************************************************
// main
//******************************************************

int main(void)    //Not sure if I should define my functions below or above
                  //this point.
{
//******************************************************
// do while
//******************************************************
//clear_m();  If I clear here it is ineffective.                                          
do
//clear_m();  If I clear here I get an error.
  {   
  //clear_m(); If I clear here the screen clears my answer before I can 
              //read it.
  cout << "Welcome to the Fun Program.";
  cout << "\nSelect from the menu.";
  cout << "\n      ";
  cout << "\nA gets Counting Loop.";
  cout << "\nB gets Impossible.";
  cout << "\nC gets Missing Item.";
  cout << "\nD gets Odd or Even.";
  cout << "\nE gets Poem.";
  cout << "\n      ";
  cout << "\nQ quits the program.";
  cout << "\n      ";
  cout << "\nEnter the letter of your choice.";
  cout << "\nThen hit the enter key.";
  cout << "\n      ";
  cout << "\nYour choice: -----> ";
  
  cin >> user_choice;
  clear_m(); //If I clear here I get the answer and the Welcome message again.
             //Best outcome so far, but not what I want.
  //******************************************************
  // case
  //******************************************************
 
  
  switch (user_choice)
     {
     case 'A': counting_loop();
          cout << "\n\n";
          break;
          
          pause_m ();
          return 0;
          
     case 'B': impossible();
          cout << "\n\n";
          break;
          
          pause_m ();
          return 0;
     
     case 'C': missing_item();
          cout << "\n\n";
          break;
          
          pause_m ();
          return 0;
     
     case 'D': odd_even();
          cout << "\n\n";
          break;
          
          pause_m ();
          return 0;
     
     case 'E': poem();
          cout << "\n\n";
          break;
          
          pause_m ();
          return 0;
     
     }                                      
                                               
  //******************************************************
  // counting loop: This is where my functions were originally.
  //******************************************************

      //counting_looop();
         //for (x = 0; x < 5; x++)
         //{
         //cout << "\nAre we having fun?";
         //}
      //pause_m();
      //return 0;
  
  //******************************************************
  // impossible loop: This has yet to work at all. 
  // I am tempted to use a nested if function.
  //****************************************************** 
     //do
      //{
      //cout << "\nThe repeat until looop is impossible in C++.";
      //}
      //while (user_choice == 'B');
       
         
      //pause_m();
      
      //return 0;
  }
while (user_choice != 'Q');  

  pause_m();
  
  return 0;
}

//******************************************************
// End of Program
//******************************************************


If you run this, you can delete comment slashes to see how I've played with this. I've structured this the way I have to follow the pseudocode I was given. If I stray from that too far, I'll lose points.

I will submit the pseudocode if it helps you understand why I wrote this code the way I did.

Here is a basic outline:

Function main
Pass in nothing
Do
Call clear screen
All the display
User input
Case of user choice A-E
Endcase
While Q not equal to Q
Pass out zero
Endfunction

Then the five functions are listed separately below. I won't list them here because they seem easy/I've done them before. They use the three variables listed.

So to summarize my questions:
How should I clear the screen to follow the pseudocode? (lines 59 & 80)
It seems functions in C++ must be written in a certain order or there will be errors. Why do my test couts work, but when I insert the actual functions they get errors?
Should any of my defined functions include parameters? I simply declared them and call them in my switch case function.
I know this was a long post, but I wanted to be thorough. Thank you for reading.
Last edited on
About clearing the screen - there is no cross-platform and standard way but some libraries on some systems provide some extended functions for it. If it is not critical - you'd better simply print out several empty lines to visually separate new input/output from previous.

About order of functions - in C/C++ function should be declared before (i.e. textually above) the place where it is called. If this give you trouble, you can declare all functions above and then define them below, like this:

1
2
3
4
5
6
7
8
9
int func(int a);

int main() {
    printf("%d", func(5));
}

int func(int a) {
    return a*a;
}


About parameters - it is not necessary that function use any parameters, however if function uses some values passed from outside, it is better to do via parameters rather than via global shared variable. I think it is almost the same in Python.
Last edited on
Hey there,

Rodiongork answered most of your questions already but I'll give you my input as well.. you never know, different wording may make it click.

So, there's several ways you can clear the screen depending on the system you're using. system("cls") is a windows-friendly command, system("ls") is a linux-friendly command (I think I also say system("clr") or something like that.. don't quote me on that, though). You can also use system("pause"), but that's also windows based. So it just depends on who you want your audience to be there. I suppose, maybe... you could put both commands in there and maybe somehow figure out a way to do an if statement... like see if there's a way to find out if the command is invalid or something. I'm not sure about all the "advanced" capabilities of c++, but I've made my way around a problem or two.

The next thing is that functions may be DEFINED before main, which would rid your use of prototypes. BUT it's considered good programming practice to do your function prototypes before the main and then define the functions after the main, like so:

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
#include <iostream>
using namespace std;

int sum(int, int&); //Refer to #1 below the code.

int main()
{
     int value = 0,
          equals = 0,
          number = 0;

     cout << "In main, the variables are : \n";
     cout << "value : " << value << endl;
     cout << "equals : " << equals << endl;; 
     cout << "number : " << number << endl;

     equals = sum(value, number);

     cout << "Now the values are : \n";
     cout << "value : " << value << endl;
     cout << "equals : " << equals << endl;; 
     cout << "number : " << number << endl;

     return 0;
}

int sum(int pval, int &pnum) {

     pnum = 5;
     pval = 2;
     int total = 0;
    
     total = pnum + pval;

     return total;
}


Okay, here goes :
#1 //This is the function prototype, you only have to define the data types in the parameter list but you can put the names of the parameters also. Also notice the ampersand, this indicates the parameter is passed by reference (the original variable can be changed ) The prototype looks just like the definition header, only it has a ";" at the end of the statement.


if a function has no parameters or doesn't return a value, you can just type you can just type your function calls like this :

sum(); //If you have a calculation that takes no parameters
int sum () //Function header for above

function();
void function() //The function header for a function that does not return a value and has no parameters.


I'm sure I over explained it, but there you go.
Last edited on
I made some changes but I still need help!

Thank you both for your help.

On the topic of clear screen. I understand now it's dependent upon the machine you're using. I'm running the compiler (Dev C++) on Windows 7 and I'm calling it from a header file saved on my C: drive.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
#include "C:\\Users\\Alan\\Cpp_Software_Download\\Dev-Cpp\\user_library\\udst_monitor.h"

//some lines of code

do
clear_m();
//more code
while


I think where I have it now may work. What concerns me the most is defining this (see below) as a function and calling it in a case control structure so that in the print screen, the 'Welcome' display shows first, then the user inputs A-E then the correct function shows with the display again (do while loop) until the user selects Q.

This code works in my first test, but I cannot figure out where to put it in my case control structure. It has a variable so it should go below main, but the case control structure is the apparatus that calls the function... This is where my head spins. The user should see "Are we having fun?" five times when they select 'A'.

1
2
3
4
5
6
7
8
9
 if (user_choice == 'A')
         for (x = 0; x < 5; x++)
         {
         cout << "\nAre we having fun?";
         }
      pause_m();
      
      return 0;
  


I know now I cannot put this in the Function Prototypes, but every other place I put it kicks out an error. The pseudocode clearly states that the case control structure calls a particular function based on the letter the user selects. The other four functions will need similar treatment as this one.

Example for selection 'A'
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
//headers
// Variables
// Function prototypes

void counting_loop(){        
    
    cout << ".counting_loop";   //test statement, it works
    }                       

//main
int main(void)

do
{
cout << "\nYour choice: -----> ";   //user selects 'A'
  
  cin >> user_choice;
  clear_m();

//case control structure 

switch (user_choice)
     {
     case 'A': counting_loop();   //where I need to call the function
          cout << "\n\n";
          break;
          
          pause_m (); //call pause from header
          return 0;
       }
}
while(user_choice != 'Q');
pause_m();   //call pause from header
return 0;


The selection by the user calls the proper function. But I just wanted to test it. The real function doesn't run in this way. So I've made progress but I've hit a wall here.

Good news is, your posts have given me a better understanding of functions. Also, my case control structure can call functions onscreen. I'm learning a lot from this headache - which I think is the point! Can you help me find a way to make the function work within the case control structure? I can get the other four functions and deal with the clear screen. I know I need to do most of this myself. Thank you again.
Last edited on
Here is what I was given to work with if it helps with understanding my problem. I still don't know where to put the actual functions. I know where to define them and call them. I can't put in the 'guts' without getting an error.

Function main
Pass In: nothing
Do
Call: clear_m
Display ........
Display "Your choice: --> ".
Get the response for user_choice from the keyboard
Case of user_choice
'A' Call: counting_loop
'B' Call: impossible
'C' Call: missing_item
'D' Call: odd_even
'E' Call: poem
Endcase
While user_choice not equal to 'Q'
Pass Out: zero to the OS
Endfunction

********************

Function counting_loop
Pass In: nothing
For x starts at 0, x < 5, increment x
Display "Are we having fun?"
Endfor
Call: pause_m
Pass Out: nothing
Endfuction

********************

Function impossible
Pass In: nothing
Display "The repeat until loop is impossible in C++."
Call: pause_m
Pass Out: nothing
Endfuction

********************

Function missing_item
Pass In: nothing
Display "This program is missing only the while loop."
Call: pause_m
Pass Out: nothing
Endfuction

********************

Function odd_even
Pass In: nothing
Display a message asking user for an integer value
Get the user_integer from the keyboard
If user_integer modulus 2
Display "Your number was odd."
Else
Display "Your number was even."
Endif
Call: pause_m
Pass Out: nothing
Endfuction

********************

Function poem
Pass In: nothing
Display a 4 line poem
Call: pause_m
Pass Out: nothing
Endfuction

********************

clear_m
and
pause_m
are functions defined in the udst_monitor.h

******************************************************

Potential Variables

Data Type Identifier Name
********* ***************
character user_choice
integer user_integer
integer x

******************************************************
Programming Hints

1. Don't try writing the program all at once. You can
eat an elephant but only one bite at a time. Suggestion:

a. Complete the do while loop but don't do the case (switch).
Compile and test it.
b. Add the case with only one item (item A) and build the
function for item A.
compile and test it.
c. Build each additional menu item one at a time.
Compile and test it before doing the next menu item.

2. Review as needed:

a. Program Control functions
b. User Defined Specific Task functions
c. The chapters within the Structured Programming Group.
******************************************************
End of file
As I suspected I should have followed the pseudocode in the order listed. So for just the 'A' selection the code should read:

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
// Headers and Other Technical Items

#include <iostream>
using namespace std;
#include "C:\\Users\\Alan\\Cpp_Software_Download\\Dev-Cpp\\user_library\\udst_monitor.h"



// Variables

char        user_choice;
int         user_integer;
int         x;

// Function prototypes

void counting_loop(void);                                                       
//void impossible(void);       //For brevity, I didn't include these functions.
//void missing_item(void);
//void odd_even(void);
//void poem(void);

//******************************************************
// main
//******************************************************

int main(void)
{
//******************************************************
// do while
//******************************************************
do
  {   
  clear_m(); 
  cout << "\nWelcome to the Fun Program.";
  cout << "\nSelect from the menu.";
  cout << "\n      ";
  cout << "\nA gets Counting Loop.";
  cout << "\nB gets Impossible.";
  cout << "\nC gets Missing Item.";
  cout << "\nD gets Odd or Even.";
  cout << "\nE gets Poem.";
  cout << "\n      ";
  cout << "\nQ quits the program.";
  cout << "\n      ";
  cout << "\nEnter the letter of your choice.";
  cout << "\nThen hit the enter key.";
  cout << "\n      ";
  cout << "\nYour choice: -----> ";
  
  cin >> user_choice;

  //******************************************************
  // case
  //******************************************************
 
  
  switch (user_choice)
     {
     case 'A': counting_loop();
               break;
     }                                      
  }
while (user_choice != 'Q');
return 0;
}                 
  //******************************************************
  // counting loop: 
  //******************************************************
     void counting_loop(void)
     {
     for (x = 0; x < 5; x++)
       {
       cout << "\nAre we having fun?";
       }
                    
     pause_m ();
     return ;
     }

//******************************************************
// End of Program
//****************************************************** 


As far as the clear function I called, I believe the first time it didn't work is that everything was out of order and I didn't have the braces properly closed. I also didn't have braces before and after the cout functions.

So this is the actual solution to this code. I'm surprised my professor gave me 8/10 on this one ;)
Topic archived. No new replies allowed.