1
#include <iostream>
using namespace std;

int main() {

    char ch[19];
    int c;
    cin >>c;
    ch[0]=c;
    ch[1]='\0';
    cout << ch;
}

what I want is to store the intger c at the location ch[0] but it stores the ascii value corresponding to that number like if.I put 97 then it stores a. How can I put the character 9 in the character array ch? Even if I typecast the variable c to char it doesn't help.

bogor
  • 21
  • 4
  • 2
    Isn't `sprintf(ch, "%d",c);` what you want? – WhatsUp Mar 09 '17 at 08:58
  • 2
    You should probably read about [`std::to_string`](http://en.cppreference.com/w/cpp/string/basic_string/to_string). – Some programmer dude Mar 09 '17 at 08:59
  • You're writing c++, use a `std::string` with @Someprogrammerdude's solution. – Colin Mar 09 '17 at 09:02
  • Unless you have a specific reason to do that, better to use the type int for your array. As you should know, a char has a 1 byte size and an int 4 bytes size – Jesus Peralta Mar 09 '17 at 09:04
  • 1
    I suspect this is an XY problem, and you need to take a step back and think about (or ask about) what problem you're trying to solve, rather than asking questions about the method you're trying to use to solve it. –  Mar 09 '17 at 09:08
  • Side-note: [please remove the `using namespace std;`](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Quentin Mar 09 '17 at 09:18
  • @Hurkyl according to a now-deleted comment from OP, this is actually not about reading numbers at all. +1 to you and VTC as unclear. – Quentin Mar 09 '17 at 09:30
  • `cin` is a pain. It has a lot of hidden flags you have to remember to use/configure. I suggest using `getchar` inside a loop, such as `int n = -1; do { ch[++n] = getchar(); } while ((n <= 19) && (ch[n] != '\0xA'));` – Jim Fell Mar 09 '17 at 13:18
  • Pressing "97" on the keyboard places two characters into the input stream, '9' and then '7'. So, what you end up with in your character array is `ch[0] == '9'` and `ch[1] == '7'`. – Jim Fell Mar 09 '17 at 13:22

1 Answers1

1

Standard streaming operators are overloaded on the type of their operands. By default, streaming into an int will read decimal digits, streaming into a char will read one character, streaming into a std::string will read a word, and so on.

So, if you want to read a single character (the digit 9), just stream into a char, which you already have at hand:

std::cin >> ch[0];
Quentin
  • 62,093
  • 7
  • 131
  • 191
  • actually I am supposed to store a count value in a character array – bogor Mar 09 '17 at 09:05
  • @bogor do you mean that if you enter `12345`, `ch` should contain `{ '1', '2', '3', '4', '5', '\0' }`? – Quentin Mar 09 '17 at 09:07
  • 1
    @bogor I fail to see how this is in any way related to what your code snippet is doing. You have asked about reading digits, not letters... – Quentin Mar 09 '17 at 09:13
  • I am was using this part in the program I haven't put up the whole question as it was not needed – bogor Mar 09 '17 at 09:16