1

For example:

char X = 3;
char Y = 6;
char Z = 9;
int scoreAll, score1, score2, score3;

cin >> score1 >> score2 >> score3; // user should enter X >> Y >> Z

scoreAll = score1 + score2 + score3;

cout << scoreAll // output should be 18

is there a way in c++ to assign an int number to a char type and then perform arithmetic operations on it using another variable?

Basically i want to type a char X and and make the compiler act like i typed 3.


Additional explanation: The user enters several characters, "XYXXZ" for example, each character has it's own value, the compiler now should add the values of these characters and output the result as an integer only (result of "XYXXZ" should be = 24).

BeyondNero
  • 53
  • 6
  • 1
    you can use switch.int getVal(char x) { switch(x) { case 'X': return 3; case 'Y': return 6; case 'z': return 9; } return -1;//invalid input } – user1438832 Apr 25 '17 at 08:45
  • 3
    There is no way to let the user enter a character or a string and use that to refer to the name of a variable, if that's what you mean. You need to do that yourself. Names in the source code and values in the program are separate universe. – molbdnilo Apr 25 '17 at 08:45
  • 2
    Your question is as clear as mud. Try providing some notional example of what you mean. – Peter Apr 25 '17 at 08:46

4 Answers4

2

You can use a std::map to map your characters/variable names into values. The user can insert then the character X, Y and Z:

std::map<char,int> values;
value['X'] = 3;
value['Y'] = 6;
value['Z'] = 9;
char score1, score2, score3;

//Here it would be advisable to check cin status/success
cin >> score1 >> score2 >> score3;

cout << value[score1] + value[score2] + value[score3] << std::endl;

Some ideas for checking cin status

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197
0

it's an horrible way to do it

honestly you should use c++ containers like maps

#include <map>
#include <stdio.h>
#include <iostream>
#include <string>

int main()
{
  std::map<std::string, int> map;
  map["X"] = 3;
  map["Y"] = 6;
  map["Z"] = 9;

  std::string res = "";
  std::cin >> res;

  for (std::map<std::string, int>::iterator it = map.begin(); it != map.end(); ++it)
  {
    if (it->first == res)
    std::cout << it->second << std::endl;
  }
}

after all using your solution (but you should use an other one) you can do something like

#include <string>
#include <iostream>
#include <stdio.h>

#define PRINTER(name) printer(#name, (name))

std::string printer(char *name, int value) {
  std::string res (name);
  return res;
}

int main()
{
  char X = 3;
  char Y = 6;
  char Z = 9;

  std::string res = "";
  std::cin >> res;

  if (res == PRINTER(X))
    std::cout << (int)X << std::endl;
}
RomMer
  • 909
  • 1
  • 8
  • 19
  • it works + c++ standards recommends to use std::string – RomMer Apr 25 '17 at 09:57
  • No, it doesn't work. In C++98 and C++03 the string literal to `char*` conversion in deprecated; since C++11 it is _illegal_. Furthermore, the C++ standard does _not_ "recommend to use std::string" in cases like this as you claim. – Lightness Races in Orbit Apr 25 '17 at 10:17
-1

Since what you want is to convert from a a numerical value the user entered, but was read as char, the simplest way is to convert that to a int.

int main()
{
   char x, y, z;
   std::cin >> x >> y >> z;
   int result = atoi(x) + atoi(y) + atoi(z);
}

atoi will convert from a numeric-letter to a number, even thougth they look the same, '1' is not 1.

Tomaz Canabrava
  • 2,320
  • 15
  • 20
-3

Try:

int main()
{

    char X = 3;
    char Y = 6;
    char Z = 9;

    int scoreAll; 
    cin >> X >> Y >> Z;
    scoreAll = X + Y + Z;
    cout << scoreAll;   
    return 0;
}

Another way:

int main()
{
    char X;
    cin >> X;
    printf("%d",X - 85);
    return 0;
}
Yasin shihab
  • 382
  • 1
  • 5