Char to Int variable

Feb 19, 2016 at 9:52pm
Hello!
I need to transform character to int variable.
For example:

1
2
  int x=1;
  char ch='x';


and I need to return ch as an int variable (which is 1) not character (which is 'x')

Neither do I need to transform 'x' to ascii.
Hope you understood, thanks in advance!
Last edited on Feb 19, 2016 at 10:01pm
Feb 19, 2016 at 9:54pm
1
2
3
char x = '5';
int number = x - '0'; // transforms the character 5 to an integer of value 5
// http://stackoverflow.com/questions/5029840/convert-char-to-int-in-c-and-c 
Feb 19, 2016 at 10:00pm
Thanks, but not what I was looking for. As you can see I have char with symbol 'x' which is not a number.
Feb 19, 2016 at 10:05pm
Well, x can't be an integer. Since it is not an integer. Why would you want an integer of value 'x'?
Feb 19, 2016 at 10:06pm
I still don't understand the question being asked.

Let's start here, (from the OP)
char ch='x';
Now some sort of int is needed. Can you give a specific example of what value the integer should have?

Feb 19, 2016 at 10:35pm
My code is something more like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int x=1;
int y=2;
int z=3;
int number;
char ch;

cin >> number;
switch (number) {

 case 1: ch='x';
 case 5: ch='y';
 case 7: ch='z';
}


lets say if it is case 7, I need to return 3. But first I have to save ch as 'z' and only out of that occurs that returned result is 3.
Last edited on Feb 19, 2016 at 10:52pm
Feb 19, 2016 at 10:39pm
It's still not making much sense.

But first I have to save ch as 'z' and only out of that occurs that returned result is 3.
???

Can't you just set ch to 'z' and after that return the number 3?

1
2
3
case 7: 
      ch = 'z';
      return 3;
Last edited on Feb 19, 2016 at 10:39pm
Feb 19, 2016 at 10:53pm
its kinda like :
int variable = value;

where variable must be ch;
Feb 19, 2016 at 11:02pm
But why? Why do you have to save a character in an intereger variable. Why can't the character just be in a character variable?
Feb 19, 2016 at 11:10pm
its kinda like :
int variable = value;

where variable must be ch;

Well, that might be better expressed as
int variable = f(ch);
That is, the value of variable is dependent upon the value of ch.

Of course you don't literally need to call a function (though you might), but I use it to express the idea. One way to express it is
1
2
3
4
5
6
7
8
9
int f(char ch)
{
    switch ch:
    {
        case 'x': return 1;
        case 'y': return 2;
        case 'z': return 3;
    }
}

again I use the function only to express the idea, The point being, you already have a switch-case, and it would make sense to set both ch and variable at the same time.
Last edited on Feb 19, 2016 at 11:11pm
Feb 19, 2016 at 11:12pm
Its a homework I have to make a Touring machine. I have to make my program the same as Touring machine would work (without break functions or simpler code e.c.) I define char as state in witch it is.
Last edited on Feb 19, 2016 at 11:12pm
Feb 19, 2016 at 11:12pm
@Chervil,

Your snippet of code should be -

1
2
3
4
5
6
7
8
9
int f(char ch)
{
	switch (ch)
	{
	case 'x': return 1;
	case 'y': return 2;
	case 'z': return 3;
	}
}
Feb 19, 2016 at 11:24pm
@TarikNeaj Thank you, you are correct.

MagicKriss wrote:
I have to make my program the same as Touring machine would work (without break functions or simpler code e.c.)

Well, I only suggested switch-case since that was what you posted. Note that without the break; the code posted at first would not work as intended. number 1, 5 or 7 would all give the same outcome, ch = 'z'.
1
2
3
4
5
6
switch (number) {

 case 1: ch='x';
 case 5: ch='y';
 case 7: ch='z';
}


I get the feeling we're all engaged in a merry dance here, where after several posts back and forth, we still may not have reached the actual question.
Topic archived. No new replies allowed.