|
-
May 8th, 2011, 04:16 PM
#1
switch state strings!
Here's my code
Code:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <string.h>
using namespace std;
int regular ();
int complex ();
int main ()
{
double dNum1;
double dNum2;
char cSymbol;
char cSetting;
char cOption;
char cComplex[3];
cout << "Type R for Regular or C for Complex" << endl << endl;
cin >> cSetting;
switch (cSetting)
{
case 'r':
regular();
break;
case 'R':
regular();
break;
case 'c':
complex();
break;
case 'C':
complex();
break;
}
}
int regular ()
{
double dNum1;
double dNum2;
char cSymbol;
char cSetting;
char cOption;
char cComplex[3];
system("CLS");
cout << "Type in your problem." << endl;
cout << " '1h1' for more information " << endl;
cin >> dNum1 >> cSymbol >> dNum2;
switch (cSymbol)
{
case '+':
cout << (dNum1 + dNum2) << endl;
break;
case '-':
cout << (dNum1 - dNum2) << endl;
break;
case '*':
cout << (dNum1 * dNum2) << endl;
break;
case 'x':
cout << (dNum1 * dNum2) << endl;
break;
case 'X':
cout << (dNum1 * dNum2) << endl;
break;
case '/':
cout << (dNum1 / dNum2) << endl;
break;
case 'h':
cout << "Action: Example Command: Result:" << endl;
cout << "Addition 2+2 4"<< endl;
cout << "Subtraction 2-2 0" << endl;
cout << "Multiplication 2*2 4" << endl;
cout << "Division 2/2 1" << endl;
cout << endl;
cout << "Press R to return or X to exit" << endl;
cin >> cOption;
switch (cOption)
{
case 'r':
regular();
break;
case 'R':
regular();
break;
default:
return 0;
break;
}
}
system("PAUSE");
return 0;
}
int complex()
{
double dNum1;
double dNum2;
char cSymbol;
char cSetting;
char cOption;
char cComplex[3];
system("CLS");
cout << "Type in your problem." << endl;
cout << " 'h1 1' for more information " << endl;
cin >> cComplex[3] >> dNum1 >> dNum2;
switch (cComplex[3])
{
case 'r':
cout << sqrt(dNum1) << endl;
break;
case 'pow':
cout << pow(dNum1,dNum2) << endl;
break;
case 'cos':
cout << cos(dNum1) << endl;
break;
case 'sin':
cout << sin(dNum1) << endl;
break;
case 't':
cout << tan(dNum1) << endl;
break;
case 'h':
cout << "Action: Example Command: Result:" << endl;
cout << "Squareroot r4 0 2"<< endl;
cout << "Power p2 5 32" << endl;
cout << "Cos c8 0 -0.1455" << endl;
cout << "Sin s8 0 0.989358" << endl;
cout << "Tan t8 0 -6.79971" << endl;
cout << endl;
cout << "Press R to return or X to exit" << endl;
cin >> cOption;
switch (cOption)
{
case 'r':
complex();
break;
case 'R':
complex();
break;
default:
return 0;
break;
}
}
system("PAUSE");
return 0;
}
the part where it says
Code:
int complex()
{
double dNum1;
double dNum2;
char cSymbol;
char cSetting;
char cOption;
char cComplex[3];
system("CLS");
cout << "Type in your problem." << endl;
cout << " 'h1 1' for more information " << endl;
cin >> cComplex[3] >> dNum1 >> dNum2;
switch (cComplex[3])
{
case 'r':
cout << sqrt(dNum1) << endl;
break;
case 'pow':
cout << pow(dNum1,dNum2) << endl;
break;
case 'cos':
cout << cos(dNum1) << endl;
break;
case 'sin':
cout << sin(dNum1) << endl;
break;
case 't':
cout << tan(dNum1) << endl;
break;
Is a very big problem.
I want my code to do the following action when you type in 'pow'
Help?
-
May 8th, 2011, 05:42 PM
#2
Re: switch state strings!
You can't do it with a switch, but an if statement would work.
One thing you can do is combine your cases such as
Code:
case 'r':
case 'R':
regular();
break;
-
May 8th, 2011, 06:39 PM
#3
Re: switch state strings!
Could you please give me an example of an if then statement?
-
May 9th, 2011, 07:06 AM
#4
Re: switch state strings!
 Originally Posted by Dgameman1
Could you please give me an example of an if then statement?
I really don't mean to sound rude, but an if statement is really, really basic and covered in any C++ text. If you're not familiar with it, you should probably work through a good tutorial before attempting any serious programming. As I'm sure your compiler is pointing out, you have quite a few other syntactical errors. C++ doesn't lend itself to guessing very well.
-
May 9th, 2011, 09:32 AM
#5
Re: switch state strings!
 Originally Posted by GCDEF
I really don't mean to sound rude, but an if statement is really, really basic and covered in any C++ text. If you're not familiar with it, you should probably work through a good tutorial before attempting any serious programming. As I'm sure your compiler is pointing out, you have quite a few other syntactical errors. C++ doesn't lend itself to guessing very well.
Yeah, I understand.
I meant an if statement that would fit in that context, but I got it all figured out =P
-
May 10th, 2011, 12:02 AM
#6
Re: switch state strings!
If a String switch expression cannot be proven by static analysis to be interned, then the compiler will generate a call to the String's intern() method.
Example 1
String state variety = condition ? "fish" : "fowl";
switch (variety) {
case "fish":
return 1;
case "fowl":
return 2;
}
return 3;
In Example 1, it's clear from path analysis that variety can evaluate only to one of two String constants, and since String constants are always interned, the compiler doesn't generate code to intern the switch expression's String value.
U want more questions Click this link http://www.coolinterview.com
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|