21275

how can I strcat one character to array character in c++

I want to get one character from cin.get() and add it to a array character. I use strcat but the single character has an error. please help me if you know. thanks for all answers.

void main (void) { char e[80]="hi"; char c; cin.get(c); strcat(e,c); cout << "e: " << e << endl; getch(); }

This is part of my code that I want to do this.

Answer1:

stncat() concatenates two strings, method signature looks like this,

char * strncat ( char * destination, const char * source, size_t num );

but you are trying to concatenate a char which is not right!

As you are using C++, it is safe and easy to do it in C++ style rather than using C style.So use

std::string cAsStr(c); // Make the string e += aAsStr; // + operator does the concatenation

If you are desperate to do it in the C style, use:

char cAsStr[] = { c, '\0' }; // Making a C-style string strcat(e, cAsStr); // Concatenate

Answer2:

Change strcat(e,c) to strncat(e, &c, 1)

Answer3:

A little change to your code would do it.

char e[80]="hi"; char c[2] = {0}; // This is made as an array of size 2 cin.get(c[0]); // Character is read into the first position. strcat(e,c); cout << "e: " << e << endl;

Answer4:

char s[] = { c, 0 }; strcat(e, s);

But please, just use std::string:

string e="hi"; char c; cin.get(c); e += c; cout << "e: " << e << endl;

Recommend

  • Auto generate ID in C++
  • Sorting in Dask
  • Writing a backspace in a file
  • i can't understand the following verilog code
  • VBA find function getting error 424 object required [duplicate]
  • linked list program
  • iTunes Connect yellow warning icon
  • Python: “Print” and “Input” in one line [duplicate]
  • JavaScript - Can we set javascript objects in cookies?
  • Which is better when storing some large texts: strings in XML resource file or a java Strings in a c
  • Android: make background image changable
  • How would I send and receive packets over a WebSocket in Javascript
  • Iterating over a container bidirectionally
  • What is “PHP-standardized” version number strings?
  • std::system Exception when instantiating a singleton object
  • How to return a number as a binary string with a set number of bits in python
  • Custom locale in Android
  • Send the string to its hex equivalent
  • Loop through each key and value of php multidimensional array
  • OpenMP and File I/O
  • How to search a CSV file with php by checking if a date falls between 2 ranges
  • several dataProvider per one Test in TestNG
  • Hash Code in SQL Server?
  • How to specify input and output paths from cmd.exe for a PowerShell script?
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Swift: Switch statement fallthrough behavior
  • How to test if a URL from an Eclipse bundle is a directory?
  • C++ and preprocessor macros: variadic type
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • What is Eclipse's Declaration View used for?
  • VS2008 Enable C++ Exception with SEH
  • When should I choose bucket sort over other sorting algorithms?
  • Jquery - Jquery Wysiwyg return html as a string
  • SVN: Merging two branches together
  • Acquiring multiple attributes from .xml file in c#
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Append folder name and increment by 1 using batch script
  • java string with new operator and a literal