storing a variable which is an integer multiplied by a string

Hi i want to store 5x in a variable a(a=5x), where 5 is an integer and x is a single character string constant.what data type does a belong to?.below is the code i wrote for the problem

main()
int b;
b=5;
char a,y;
y='x';
a=b*y;
printf("a = ???\n",a);
}
when i give printf("a = %d%c\n",b,y) iam getting the ouput as 5x.i know that 5x is not stored in the variable a. any help is highly appreciated.

hope i made my question clear

Regards
vinoth
Do you know what typecasting is? Where you have ??? use %d (as you've used it already I assume you know what it is)
As for typecasting, try compiling this inside a program:

1
2
3
printf("Enter a character: ");
char myChar = getchar();
printf("%d", int(myChar);

Type in a number. Add 48 (ASCII value of 0 is 48), that's what the program will print.

That should be enough to get you going. If not, ask back here.
Have fun.
hi what you are saying is not clear to me.i want to store 5x in an variable(a).what data type does that variable belong to.similarly if i want to add, say b = a+a, the value of b should be 10x. what data type b should belong to.
What you are trying to do is more than just a simple one or two lines of code.

For 'a' to have the value "5x", 'a' cannot be an integer type, since "5x" is not an integer (indeed, as I've written it, it is a std::string, or a char*).

If you are developing a simple algebra library that can do things like

5x + 5x = 10x
(3x + 4) - (x + 1) = 2x + 3

you will need to make a class that can hold an algebraic term (ie, a value of the form c*x^k, where c and k are both integer quantities.

hi smith.thank you for your comments.it will be of great use if you can explain me with an example. iam able to understand it but not able to sort it out.
It depends on how complex you want to get. Are you only going to support first order terms? Or first order terms and constants? Or nth order terms?

In the first case, all you really need is

1
2
3
4
5
6
7
8
9
10
11
12
struct Term {
    explicit Term( int t = 0 ) : term( t ) {}

    Term operator+( const Term& t2 ) 
        { return Term( term + t2.term ); }

    friend std::ostream& operator<<( std::ostream& os, const Term& t )
        { return os << std::dec << t.term << 'x'; }

  private:
      int term;
};


etc, etc.
Topic archived. No new replies allowed.