| | |
Problems with switch statement
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 32
Reputation:
Solved Threads: 0
a program that will allow the user to input two fractions and one of the four operations of +, - , *, and / . Once this information has been entered, the program should compute the operation on the two fractions and then output the answer appropriate labeled. Reducing the fractions using the gcd algorithm. The program should then ask the user if another number is desired (y or n) and continue requesting pairs of fractions until the user answers no (n). My problem is encorporating the switch statement for the operations and also the gcd algorithm. Here is what I have, I only used addition and sub. because I couldn't figure out how to use the switch.
--------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
void EnterOperator(char &opchar)
{
cout << "Enter an operation to perform { + - / * } ";
cin >> opchar;
}
void ReadFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function will allow the user to enter two fraction. */
{
cout << "Enter the numerator and denominator for the first fraction; include a space: ";
cin >> Num >> Denom;
cout << endl;
cout << "Enter the numerator and denominator for the second fraction; include a space: ";
cin >> Num2 >> Denom2;
cout << endl;
}
//-------------------------------------------------------------------
void AddFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function is called after Reduce. This function adds the two
fractions Reduce() reduced
Pre: Two Fractions
Post: One reduced fraction */
{
if (Denom != Denom2)
{
Num = Num * Denom2;
Num2 = Num2 * Denom;
Denom = Denom * Denom2;
Denom2 = Denom2 * Denom;
Num = Num + Num2;
}
else
{
Num = Num + Num2;
}
}
void SubFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function is called after Reduce. This function adds the two
fractions Reduce() reduced
Pre: Two Fractions
Post: One reduced fraction */
{
if (Denom != Denom2)
{
Num = Num * Denom2;
Num2 = Num2 * Denom;
Denom = Denom * Denom2;
Denom2 = Denom2 * Denom;
Num = Num - Num2;
}
else
{
Num = Num - Num2;
}
}
void DisplayFraction(int &Num, int &Denom)
/* This function displays the reduced and added fraction. This
function is called after AddFraction()
Post: Prints fraction */
{
cout << "The fraction value is: " << Num << "/" << Denom << endl;
}
int main()
{
char an;
int op;
do
{
int Num, Denom, Num2, Denom2 = 0;
char opchar;
EnterOperator(opchar);
ReadFraction(Num, Denom,Num2,Denom2);
AddFraction(Num, Denom, Num2, Denom2);
SubFraction(Num, Denom, Num2, Denom2);
DisplayFraction(Num, Denom);
cout << endl;
switch (opchar) {
case '+':
op = ;
break;
case '-':
op = ;
break;
default: // If operator is illegal shut program down
cout << "Invalid operator." << endl;
int gcd(int Num, int Denom ) {
assert(Denom != 0);
int rem = Num % Denom;
while(rem !=0 ){
Num = Denom;
Denom = rem;
rem = Num % Denom;
}
return Denom;
}
}
cout <<"Would you like to do another fraction? ";
cin >> an;
cout << endl;
} while ((an == 'y') || (an == 'Y'));
return(0);
}
--------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
void EnterOperator(char &opchar)
{
cout << "Enter an operation to perform { + - / * } ";
cin >> opchar;
}
void ReadFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function will allow the user to enter two fraction. */
{
cout << "Enter the numerator and denominator for the first fraction; include a space: ";
cin >> Num >> Denom;
cout << endl;
cout << "Enter the numerator and denominator for the second fraction; include a space: ";
cin >> Num2 >> Denom2;
cout << endl;
}
//-------------------------------------------------------------------
void AddFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function is called after Reduce. This function adds the two
fractions Reduce() reduced
Pre: Two Fractions
Post: One reduced fraction */
{
if (Denom != Denom2)
{
Num = Num * Denom2;
Num2 = Num2 * Denom;
Denom = Denom * Denom2;
Denom2 = Denom2 * Denom;
Num = Num + Num2;
}
else
{
Num = Num + Num2;
}
}
void SubFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function is called after Reduce. This function adds the two
fractions Reduce() reduced
Pre: Two Fractions
Post: One reduced fraction */
{
if (Denom != Denom2)
{
Num = Num * Denom2;
Num2 = Num2 * Denom;
Denom = Denom * Denom2;
Denom2 = Denom2 * Denom;
Num = Num - Num2;
}
else
{
Num = Num - Num2;
}
}
void DisplayFraction(int &Num, int &Denom)
/* This function displays the reduced and added fraction. This
function is called after AddFraction()
Post: Prints fraction */
{
cout << "The fraction value is: " << Num << "/" << Denom << endl;
}
int main()
{
char an;
int op;
do
{
int Num, Denom, Num2, Denom2 = 0;
char opchar;
EnterOperator(opchar);
ReadFraction(Num, Denom,Num2,Denom2);
AddFraction(Num, Denom, Num2, Denom2);
SubFraction(Num, Denom, Num2, Denom2);
DisplayFraction(Num, Denom);
cout << endl;
switch (opchar) {
case '+':
op = ;
break;
case '-':
op = ;
break;
default: // If operator is illegal shut program down
cout << "Invalid operator." << endl;
int gcd(int Num, int Denom ) {
assert(Denom != 0);
int rem = Num % Denom;
while(rem !=0 ){
Num = Denom;
Denom = rem;
rem = Num % Denom;
}
return Denom;
}
}
cout <<"Would you like to do another fraction? ";
cin >> an;
cout << endl;
} while ((an == 'y') || (an == 'Y'));
return(0);
}
•
•
Join Date: Oct 2004
Posts: 32
Reputation:
Solved Threads: 0
I omitted the gcd algorithum, if someone could explain to me how i can call the functions to the switch statement I would appreciate it. Thanks
#include <iostream>
using namespace std;
void EnterOperator(char &opchar)
{
cout << "Enter an operation to perform { + - / * } ";
cin >> opchar;
}
void ReadFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function will allow the user to enter two fraction. */
{
cout << "Enter the numerator and denominator for the first fraction; include a space: ";
cin >> Num >> Denom;
cout << endl;
cout << "Enter the numerator and denominator for the second fraction; include a space: ";
cin >> Num2 >> Denom2;
cout << endl;
}
//-------------------------------------------------------------------
void AddFraction(int &Num, int &Denom, int &Num2, int &Denom2)
{
if (Denom != Denom2)
{
Num = Num * Denom2;
Num2 = Num2 * Denom;
Denom = Denom * Denom2;
Denom2 = Denom2 * Denom;
Num = Num + Num2;
}
else
{
Num = Num + Num2;
}
}
void SubFraction(int &Num, int &Denom, int &Num2, int &Denom2)
{
if (Denom != Denom2)
{
Num = Num * Denom2;
Num2 = Num2 * Denom;
Denom = Denom * Denom2;
Denom2 = Denom2 * Denom;
Num = Num - Num2;
}
else
{
Num = Num - Num2;
}
}
void MultFraction(int &Num, int &Denom, int &Num2, int &Denom2)
{
if (Denom != Denom2)
{
Num = Num * Num2;
Denom = Denom * Denom2;
}
else
{
Num = Num * 0;
}
}
void DivFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function is called after Reduce. This function adds the two
fractions Reduce() reduced
Pre: Two Fractions
Post: One reduced fraction */
{
if (Denom != Denom2)
{
Num = Num * Denom2;
Denom = Denom * Num2;
}
else
if (Denom < 0)
{
Num *= -1;
Denom *= -1;
}
}
void DisplayFraction(int &Num, int &Denom)
/* This function displays the reduced and added fraction. This
function is called after AddFraction()
Post: Prints fraction */
{
cout << "The fraction value is: " << Num << "/" << Denom << endl;
}
int main()
{
char an;
int op;
do
{
int Num, Denom, Num2, Denom2 = 0;
char (opchar);
EnterOperator(opchar);
ReadFraction(Num, Denom,Num2,Denom2);
AddFraction(Num, Denom, Num2, Denom2);
SubFraction(Num, Denom, Num2, Denom2);
MultFraction(Num, Denom, Num2, Denom2);
DivFraction(Num, Denom, Num2, Denom2);
DisplayFraction(Num, Denom);
cout << endl;
switch (opchar) {
case '+':
op = ; //Call Function
break;
case '-':
op = ; //Call Function
break;
case '*':
op = ; //Call Function
break;
case '/':
op = ;
break; //Call Function
default: // If operator is illegal shut program down
cout << "Invalid operator." << endl;
}
cout <<"Would you like to do another fraction? ";
cin >> an;
cout << endl;
} while ((an == 'y') || (an == 'Y'));
return(0);
}
#include <iostream>
using namespace std;
void EnterOperator(char &opchar)
{
cout << "Enter an operation to perform { + - / * } ";
cin >> opchar;
}
void ReadFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function will allow the user to enter two fraction. */
{
cout << "Enter the numerator and denominator for the first fraction; include a space: ";
cin >> Num >> Denom;
cout << endl;
cout << "Enter the numerator and denominator for the second fraction; include a space: ";
cin >> Num2 >> Denom2;
cout << endl;
}
//-------------------------------------------------------------------
void AddFraction(int &Num, int &Denom, int &Num2, int &Denom2)
{
if (Denom != Denom2)
{
Num = Num * Denom2;
Num2 = Num2 * Denom;
Denom = Denom * Denom2;
Denom2 = Denom2 * Denom;
Num = Num + Num2;
}
else
{
Num = Num + Num2;
}
}
void SubFraction(int &Num, int &Denom, int &Num2, int &Denom2)
{
if (Denom != Denom2)
{
Num = Num * Denom2;
Num2 = Num2 * Denom;
Denom = Denom * Denom2;
Denom2 = Denom2 * Denom;
Num = Num - Num2;
}
else
{
Num = Num - Num2;
}
}
void MultFraction(int &Num, int &Denom, int &Num2, int &Denom2)
{
if (Denom != Denom2)
{
Num = Num * Num2;
Denom = Denom * Denom2;
}
else
{
Num = Num * 0;
}
}
void DivFraction(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function is called after Reduce. This function adds the two
fractions Reduce() reduced
Pre: Two Fractions
Post: One reduced fraction */
{
if (Denom != Denom2)
{
Num = Num * Denom2;
Denom = Denom * Num2;
}
else
if (Denom < 0)
{
Num *= -1;
Denom *= -1;
}
}
void DisplayFraction(int &Num, int &Denom)
/* This function displays the reduced and added fraction. This
function is called after AddFraction()
Post: Prints fraction */
{
cout << "The fraction value is: " << Num << "/" << Denom << endl;
}
int main()
{
char an;
int op;
do
{
int Num, Denom, Num2, Denom2 = 0;
char (opchar);
EnterOperator(opchar);
ReadFraction(Num, Denom,Num2,Denom2);
AddFraction(Num, Denom, Num2, Denom2);
SubFraction(Num, Denom, Num2, Denom2);
MultFraction(Num, Denom, Num2, Denom2);
DivFraction(Num, Denom, Num2, Denom2);
DisplayFraction(Num, Denom);
cout << endl;
switch (opchar) {
case '+':
op = ; //Call Function
break;
case '-':
op = ; //Call Function
break;
case '*':
op = ; //Call Function
break;
case '/':
op = ;
break; //Call Function
default: // If operator is illegal shut program down
cout << "Invalid operator." << endl;
}
cout <<"Would you like to do another fraction? ";
cin >> an;
cout << endl;
} while ((an == 'y') || (an == 'Y'));
return(0);
}
a switch statement is a mechanism of flow control that can be translated to something like this (provided you return/break after each case),
Two important distinctions, however, must be made; both implementation details. The first is that a switch statement only handles int/char cases (conditions) and the other is that you may fall through the case (condition) quite easily (ie: as mentioned early if you don't return/break after a case you fall through to the next case).
So, anyway, for your purpose you might have this function:
Or something to that effect
C++ Syntax (Toggle Plain Text)
int cond; // case 1: if(cond == 1 { // case 2: } else if (cond == 2) { // case 3: } else if (cond == 3) { // default: } else { }
Two important distinctions, however, must be made; both implementation details. The first is that a switch statement only handles int/char cases (conditions) and the other is that you may fall through the case (condition) quite easily (ie: as mentioned early if you don't return/break after a case you fall through to the next case).
So, anyway, for your purpose you might have this function:
C++ Syntax (Toggle Plain Text)
bool calc(int & n1, int & d1, int & n2, int & d2, char sign) { switch(sign) { case '+': AddFraction(n1, d1, n2, d2); break; case '-': SubFraction(n1, d1, n2, d2); break; case '*': MultFraction(n1, d1, n2, d2); break; case '/': DivFraction(n1, d1, n2, d2); break; default: return false; } return true; }
Or something to that effect
•
•
•
•
Originally Posted by ohnbabygal
as far as i can see, u wanna call the functions to an int (op), but ur functions are not returning anything. try to return the final value to an integer at the end of each function. that mite work.
(eg. for subfraction function u mite wanna return 'num')
C++ Syntax (Toggle Plain Text)
void foo(int & n) { n++; } int main() { int a = 0; cout << a << endl; foo(a); cout << a << endl; return 0; }
Will return,
0
1
•
•
Join Date: Oct 2004
Posts: 32
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
int gcd(int a, int b){ assert(b != 0); int rem = a % b; while(rem !=0 ){ a = b; b = rem; rem = a % b; } return b; }
![]() |
Similar Threads
- JavaScript Switch Statement Problems (JavaScript / DHTML / AJAX)
- Using a switch statement with nested if/else statements (C++)
- Problems with switch statement (C++)
Other Threads in the C++ Forum
- Previous Thread: Still need info on calling a function from bool!
- Next Thread: need help
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






