Problems with switch statement

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 32
Reputation: dontcare is an unknown quantity at this point 
Solved Threads: 0
dontcare dontcare is offline Offline
Light Poster

Problems with switch statement

 
0
  #1
Oct 14th, 2004
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);
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problems with switch statement

 
0
  #2
Oct 14th, 2004
>because I couldn't figure out how to use the switch
The switch is fine, it's the illegal nested function that's your problem. Functions cannot be defined within other functions.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 32
Reputation: dontcare is an unknown quantity at this point 
Solved Threads: 0
dontcare dontcare is offline Offline
Light Poster

Re: Problems with switch statement

 
0
  #3
Oct 14th, 2004
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);
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 12
Reputation: ohnbabygal is an unknown quantity at this point 
Solved Threads: 0
ohnbabygal ohnbabygal is offline Offline
Newbie Poster

Re: Problems with switch statement

 
0
  #4
Oct 14th, 2004
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')
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: Problems with switch statement

 
0
  #5
Oct 15th, 2004
a switch statement is a mechanism of flow control that can be translated to something like this (provided you return/break after each case),

  1. int cond;
  2.  
  3. // case 1:
  4. if(cond == 1 {
  5. // case 2:
  6. } else if (cond == 2) {
  7. // case 3:
  8. } else if (cond == 3) {
  9. // default:
  10. } else {
  11. }

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:

  1.  
  2. bool calc(int & n1, int & d1, int & n2, int & d2, char sign) {
  3. switch(sign) {
  4. case '+':
  5. AddFraction(n1, d1, n2, d2);
  6. break;
  7. case '-':
  8. SubFraction(n1, d1, n2, d2);
  9. break;
  10. case '*':
  11. MultFraction(n1, d1, n2, d2);
  12. break;
  13. case '/':
  14. DivFraction(n1, d1, n2, d2);
  15. break;
  16. default:
  17. return false;
  18. }
  19. return true;
  20. }

Or something to that effect
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: Problems with switch statement

 
0
  #6
Oct 15th, 2004
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')
He's passing references to integers (a type of pointer), not integers themselves.


  1.  
  2. void foo(int & n) {
  3. n++;
  4. }
  5.  
  6. int main() {
  7. int a = 0;
  8. cout << a << endl;
  9. foo(a);
  10. cout << a << endl;
  11. return 0;
  12. }

Will return,

0
1
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 32
Reputation: dontcare is an unknown quantity at this point 
Solved Threads: 0
dontcare dontcare is offline Offline
Light Poster

Re: Problems with switch statement

 
0
  #7
Oct 16th, 2004
  1. int gcd(int a, int b){
  2. assert(b != 0);
  3. int rem = a % b;
  4. while(rem !=0 ){
  5. a = b;
  6. b = rem;
  7. rem = a % b;
  8. }
  9. return b;
  10. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC