multi character char?

Oct 13, 2010 at 4:32am
Hi everyone! Im new here to the forums so im gonna give this a try.
Im taking an intro class to c++ and although ive been doing pretty well im stuck on a small issue.
Right now I am working on "creating a calculator". Here is a small piece of my code:


#include <iostream>
#include <math.h>
using namespace std;

int main()
{
double a,b,c;
char i;
cout << "This program is intended to be used as a calculator\n"

<< "Please enter: \n (1) the value of the first number\n (2) the symbol of the desired operation \n (3) and finally the second number\n"

cin >> a;
cin >> i;

switch (i)
{
case '+':
cin >> b;
c = a+b;
cout << a<<" + "<<b<<" = "<<c<<endl;
swap(a, c);
break;

case 'cos':
cin >> b;
c = a+b;
cout << "cos("<<b<<") = "<<c<<endl;
break;

default:
break;
}



system("pause");
return 0;

}

As you probably already noticed, I am using the switch/case structure with i as the index. The problem is I am using it as a char so it only stores one character but I need it to store more than one e.g. 'cos'. I also tried declaring i as: char i[5]; but when i do this it says switch quantity is not an integer.
Can comeone explain (in simplest words) how i can make 'i' longer than one letter AND make it work as a switch index?
Oct 13, 2010 at 6:06am
What you are looking for are 'strings' ie; string i instead of char i. And a header #include <string>

But then you have a problem, because as far as I am aware you cannot use a string with switch statements as the 'index!' I think you need to reconfigure the switch statement so that you can work around your problem. For example you could ask the user to select the maths symbol first, then in the switch statement have them enter the values. There are actually multiple ways to do it.

I also see you need a semi-colon ; after the cout statement.

Also you have c = a+b in both '+' and 'cos' calculations. Not sure if thats intended??

Last edited on Oct 13, 2010 at 6:10am
Oct 13, 2010 at 6:59am
You can use static_cast on a switch statement to process the character,just check the ASCII table.Like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
switch(static_cast<int>(c))
         {

            case 65:

            cout<<"You entered an A"; // example
           
            break;
             
           .
           .  //process all the characters
           .
           .

           default:

           //display an error message if user entered a wrong value

         }




Cheers.
Oct 13, 2010 at 7:40am
@Mike
there is no cos in ASCII table.Am i right?

EDIT:Try too enumerate 'cos,sin or tan' before indexing.

enum enumeration_name {
value1,
value2,
value3,
.
.
} object_names;

example

enum trigo{cos,sin,tan}operator;// cos=0,sin=1,tan=2

I think will help
Oct 13, 2010 at 8:40am
hello Bacos001,

if you use std::string i like AlphaBravo said you cannot use switch(). Instead use if(...) else if(...) etc.

like
1
2
3
4
5
if("+" == i)
  { ... }
else if("cos" == i)
  { ... }
else if ...

nothing wrong with that


@Mazd

are you aware that you're declaring a variable by a construct like that
Mazd wrote:
enum enumeration_name {
value1,
value2,
value3,
.
.
} object_names;
Your're declaring a variable object_names of type enumeration_name

The following is completely wrong
Mazd wrote:
enum trigo{cos,sin,tan}operator;// cos=0,sin=1,tan=2
since that variable operator is a key word in C++
Last edited on Oct 13, 2010 at 8:41am
Oct 13, 2010 at 8:45am
To me it looks like Bacos 001 is just starting out in c++. In my opinion enums and static casts should probably left till later. A simple switch and or if statements are all thats needed for a calculator.
Oct 13, 2010 at 8:55am
Ow!God.Thanks guys, i was just wakeup. if()else is a good idea.
Oct 13, 2010 at 10:45am
Select on a string (scroll down to the second code snippet):
http://www.cplusplus.com/forum/general/11460/#msg54095

Always avoid multicharacter characters -- their value is implementation defined.

Hope this helps.
Oct 13, 2010 at 3:12pm
Thank you guys for your input. Yes, like alphabravo said, I am barely starting out with c++. And I guess i have no choice but to go back and rewrite my code using if and else if. *sigh* I hate those. I personally think switch and case are cleaner a easier but alas it seems those dont work.
Once again thank you all
-B
Oct 13, 2010 at 11:34pm
Yes, I agree that switch and case are cleaner -- which is why I gave you an example of how to switch on a string.
Topic archived. No new replies allowed.