Case Statements with functions inside.

Hello all! I'm new to the forum. I have a question that has had me pulling my hair out for the past six hours.
I have a simple menu code with case statements and I want my two sets of codes to run if the right case statement is true. - basically how do you integrate the two separate codes into the menu. All code works when not together.

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
#include<stdio.h>
#include<time.h>
#include <stdlib.h>
#include<iostream>
#include <string>

using namespace std;

int main()
{
    char a;
    char b;
    char menu;
    string name;
    cout << "What is your name? \n";
    cin >> name;

    system("cls");

    do

    {



    cout <<"Welcome  " << name << "\n" ;
    cout <<"This program  will allow you to create a multiplication table for any number by pressing a OR Multiply a number to itself a set number of times you requested by pressing b." "\n" ;

    cin >> menu;








        switch (menu)
        {

        case 'a':
            system("cls");
            cout << "--program 1-- \n";
            break;



        case 'b':
            system("cls");
            cout << "--program 2-- \n";
            break;


        default:
            system("cls");
            cout << "Invalid command \n";
            break;

        }



    }

    while (menu != a || menu != b);




    return 0;
}


// program 1

#include<stdio.h>
#include<time.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
int delTime;
void delay(unsigned int mseconds)// delay is set for mill-seconds
{
     clock_t goal = mseconds + clock();
     while (goal > clock());
}

int main()
{
int p;
int x;
int value;
cout << "Please enter a number for the multiplication table. \n";
cin >> x;
cout << "How large do you want the table to be? INCLUDES ZERO MULTIPLE! \n";
cin >> p;
system("cls");
    {
    int i;
    int counter=0;
    for(i=0;i<p;i++) // delay for loop to run 10 times
        {
            value= x*counter;
            delay(1000); // calls the delay function and provides time in ms
            cout << "This is the value" << x << "*"   << counter << "=" << value <<"\n";
            counter = counter++;

        }

    }
return 0;
}

//program 2

#include<stdio.h>
#include <stdlib.h>
#include<time.h>
#include<iostream>
#include <math.h>
using namespace std;
int delTime;
void delay(unsigned int mseconds)// delay is set for mill-seconds
{
     clock_t goal = mseconds + clock();
     while (goal > clock());
}

int main()
{
int p;
int x;
int value;
cout << "Please enter a base. \n";
cin >> x;
cout << "Please enter an exponent. \n";
cin >> p;
system("cls");
    {
    int i;
    int counter=x;
    for(i=0;i<p;i++) // delay for loop to run 10 times
        {
            value= x*(counter);
            delay(1000); // calls the delay function and provides time in ms
            cout << "This is the value" << x << "*"   << counter << "=" << value <<"\n";
            counter = value;

        }

    }
return 0;
}
Why can't you use functions inside the switch statements instead of running other programs?
Is this what you mean?


#include<stdio.h>
#include<time.h>
#include <stdlib.h>
#include<iostream>
#include <string>

using namespace std;

int main()
{
char a;
char b;
char menu;
string name;
cout << "What is your name? \n";
cin >> name;

system("cls");

do

{



cout <<"Welcome " << name << "\n" ;
cout <<"This program will allow you to create a multiplication table for any number by pressing a OR Multiply a number to itself a set number of times you requested by pressing b." "\n" ;

cin >> menu;








switch (menu)
{

case 'a':
system("cls");

int delTime;



void delay(unsigned int mseconds)// delay is set for mill-seconds
{
clock_t goal = mseconds + clock();
while (goal > clock());
}


{
int p;
int x;
int value;
cout << "Please enter a number for the multiplication table. \n";
cin >> x;
cout << "How large do you want the table to be? INCLUDES ZERO MULTIPLE! \n";
cin >> p;
system("cls");
{
int i;
int counter=0;
for(i=0;i<p;i++) // delay for loop to run 10 times
{
value= x*counter;
delay(1000); // calls the delay function and provides time in ms
cout << "This is the value" << x << "*" << counter << "=" << value <<"\n";
counter = counter++;

}

}


}
break;



case 'b':
system("cls");
cout << "--program 2-- \n";
break;


default:
system("cls");
cout << "Invalid command \n";
break;

}



}

while (menu != a || menu != b);




return 0;
}

You could do this but there are two problems with this. Number one, you can't declare a function inside of another function, number two, I can't see a reason for the delTime variable. I also think it would be easier to manage and to read if you wrote the functions as you did in the first example (but call them something else of course) and just call them inside the switch statement. For example
1
2
3
4
void multiplication()
{
    //your code here
}


and in the switch statement...
1
2
3
4
case 'a':
    multiplication();
    break;
    //etc 
Last edited on
Like this? Now I get "multi was not declared in this scope"

#include<stdio.h>
#include<time.h>
#include <stdlib.h>
#include<iostream>
#include <string>

using namespace std;

int main()
{

char a;
char b;
char menu;
string name;
cout << "What is your name? \n";
cin >> name;

system("cls");

do

{



cout <<"Welcome " << name << "\n" ;
cout <<"This program will allow you to create a multiplication table for any number by pressing a OR Multiply a number to itself a set number of times you requested by pressing b." "\n" ;

cin >> menu;








switch (menu)
{

case 'a':
system("cls");
multi();
break;



case 'b':
system("cls");
cout << "--program 2-- \n";
break;


default:
system("cls");
cout << "Invalid command \n";
break;

}



}

while (menu != a || menu != b);




return 0;

}

int delTime;
void delay(unsigned int mseconds)// delay is set for mill-seconds
{
clock_t goal = mseconds + clock();
while (goal > clock());
}


void multi()
{
int p;
int x;
int value;
cout << "Please enter a number for the multiplication table. \n";
cin >> x;
cout << "How large do you want the table to be? INCLUDES ZERO MULTIPLE! \n";
cin >> p;
system("cls");
{
int i;
int counter=0;
for(i=0;i<p;i++) // delay for loop to run 10 times
{
value= x*counter;
delay(1000); // calls the delay function and provides time in ms
cout << "This is the value" << x << "*" << counter << "=" << value <<"\n";
counter = counter++;

}

}
return ;
}


Before any function is called, the function calling it must know about it. You can do one of two things to solve this problem. You can either define the function before main, or you can put a function prototype before main. In order to do this, all you need to do is put the return type which in your case is void, the name, and the parameters followed by a semicolon. In your case, it would look like this. void multi(); . This would go at the top of your source code and has to be before any call to that particular function. You would have to do this to the delay function too.
Topic archived. No new replies allowed.