*char to char conversion

Hi,

I'm making a loop that runs through a string of characters and looks for double letters. The searching procedure works fine, e.g. for the input "pepper" it finds one double (a pair of adjacent, identical characters), and for the input "nooob", it finds two pairs. Now, the thing I actually want it to do is to break up these pairs by inserting a couple of characters in between them.
What I've done so far is that I create a new string that's empty, and that fills in the characters from the input string. Whenever (in the loop over the characters from the input) it detects a double (again, the detection works), it should, instead of just adding the two letters, say "pp" to the new resulting string, it should add "pDOUBLEp". My problem is that when I try to make it do that, I get an error about me trying to convert *char to char, which I didn't even know that I did.
Anyway, here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string.h>
#include <string>
#include <stdlib.h>

using namespace std;

int main()
{
  char key[100];
  cin >> key;
  char ins1[]="X";
  char ins2[]="Y";
  char ins3[]="W";
  int length = strlen(key);
  int i; int j=0; int count=0;
  char prepared[1000];
  for(i=0;i<(length-1);i++)
    {
      if(key[i]==key[i+1])
	{ 
	  prepared[j]=ins1;
	  prepared[j+1]=ins2;
	  prepared[j+2]=ins3;
	  j=j+4;
	  count=count+1;
	}
      else
	{
	  prepared[j]=key[i];
	  j=j+1;
	  count=count;
	}
    }

  cout << "Total number of doubles: " << count << endl;
  cout << "Testing the string: " << prepared << endl;

  return 0;
}


And the error message when I try to compile:

1
2
3
4
peder@peder-ThinkPad-X220:~/C++/testing$ g++ prepareTest.cpp
prepareTest.cpp: In function ‘int main()’:
prepareTest.cpp:24:18: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]


* Note that in the code I ran to get this I had switched two of the "inserts" for something else, hence only one error.

Does anyone see what's obviously wrong here?

Thanks!
Last edited on
line 13 should be: cin.getline(key, 100, '\n');
@LowestOne: Hi! Thanks for the reply! Also, thanks for the improved input method -- I'm still quite new to C++ :)

I still get the same error, though. I also just noticed that I messed up the line numbering in the previous post. The error message is now corrected. So the error occurs at lines 24, 25, and 26.
Where in the code do I specify whether it's const char or just char?

Thanks for all help!
Line 24 needs prepared[j]=ins1[0]; and likewise for 25 and 26. That should solve your problems.

I'm not sure the specifics about const char and char. I do know that when you use double quotes, you're making a const char, and the proper code would be const char inst1[] = "X";. I think const char* inst1 = "X"; is exactly the same.

Both of these are c-strings, meaning that they are null teminated:
1
2
3
const char inst1[] = "X";
// has the same data as
char inst1[2] = {'X', '\0'};

Line three makes a character array that you can modifiy.


Last edited on
@ LowestOne: Ah! That makes sense! And it does indeed solve the problem! :)
Thank you so much for the help!
Topic archived. No new replies allowed.