GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Oct-2004, 08:24
dontcare dontcare is offline
New Member
 
Join Date: Oct 2004
Posts: 12
dontcare is on a distinguished road

Switch Statement Problem


Problems with switch statement
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.

--------------------------------------------------------------------------------------------------
CPP / C++ / C Code:
#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);
}
Last edited by JdS : 14-Oct-2004 at 08:43. Reason: Please insert your example C/C++ codes between [c] and [/c] tags
  #2  
Old 14-Oct-2004, 16:10
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi dontcare.

I don't see that you have any problems with the switch statement whatsoever (beyond your indentation ). It appears that the program just needs to be completed.

CPP / C++ / C Code:
switch (opchar) {
  case '+':
    op = ;  //Call function
    break;
  case '-':
    op = ;  //Call function
    break;
  case '*':
    op = ; //Call Function.
    break;
  case '/':
    op =  //Call function.
    break;

  default: // If operator is illegal shut program down
    cout << "Invalid operator." << endl;

That looks right to me. As for the other part of the question, it looks suspiciously like a homework assignment. I don't speak for everyone, but I don't get any credit for doing other peoples homework. I don't have any problem helping if you have more specific problems or questions.

Good luck!
  #3  
Old 14-Oct-2004, 19:54
dontcare dontcare is offline
New Member
 
Join Date: Oct 2004
Posts: 12
dontcare is on a distinguished road
Your right for know I will omit the gcd algo. but could explain to me how you call functions I would appreciate it. Thanks
  #4  
Old 14-Oct-2004, 22:23
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,377
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dontcare
Your right for know I will omit the gcd algo. but could explain to me how you call functions I would appreciate it. Thanks
CPP / C++ / C Code:
{
    int aVal, bVal;

...

    value = AddNumbers(aVal, bVal);    // add a + b
...
}

// The value aVal above will enter the function as val1, bVal as val2
int  AddNumber(int val1, val2)    // receives two ints, returns an int
{
    int rtnvalue;    // the answer...

    rtnvalue = val1 + val2;    // calculate the sum
    return  rtnvalue;            // return the sum
}

Also, note the indentation. Check the tutorials section for the Formatting Code tutorial
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Command Line Arguments, Part 1 WaltP C Programming Language 0 10-Jul-2004 23:34
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53
Help a C++ Idiot: I am trying to fill an array based on a switch statement. Psycop C Programming Language 2 14-Apr-2004 03:12
switch statement freedomJoe C Programming Language 0 27-Oct-2003 08:45

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:44.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.