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.
#include<stdio.h>
#include<time.h>
#include <stdlib.h>
#include<iostream>
#include <string>
usingnamespace 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>
usingnamespace std;
int delTime;
void delay(unsignedint 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>
usingnamespace std;
int delTime;
void delay(unsignedint 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;
}
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;
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
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;
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++;
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.