multi-character character constant error message?

Sep 14, 2014 at 7:06am

I keep getting this warning message and I do not know how to fix it. Is it because I'm using char to instead of strings to replace all 't' with 'lp'?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<string>
#include <stdio.h>
using namespace std;

char * scanf(char * a)
{
while(*a!='\0')
{
if(*a=='t')
*a='lp';
a++;
}
return a;
}


int main(void)
{
char string[]="the man is not thinking what the weather is like today";
scanf(string);
printf("%s \n",string);
system("PAUSE");
}
Sep 14, 2014 at 7:10am
lp is two characters not one. So who knows what the actual value of 'lp' is. Also, you have a few naming conflicts that I am surprised you didn't get warnings for. Such as the function scanf, and an array named string while you have the string library and the std:: namespace in the global scope. To do it the way currently would be kind of tricky. Basically you would have to assign each t as an l then, shift every thing to the right one space so you can assign the next character a p.
Sep 14, 2014 at 7:27am
Haha I don't even know how I only managed to get one error after doing all of that, but how would you be able to shift it then?
Sep 14, 2014 at 5:37pm
May I ask why you include iostream (c++) and stdio (c) and then only use the stdio (printf). If you can use c++ I would suggest using std::string instead of an array of characters. Because with your current array of characters you can't shift left. It would need to be dynamic. Also, why are you returning if you never actually use the return type?
Sep 14, 2014 at 6:37pm
Ah I am trying to create a program without using std::string and I was creating this program in c++ and it was a habit to put in iostream.
Sep 14, 2014 at 7:15pm
well if you are creating it in c++ you should use iostream and use cout/cin instead of printf/scanf. As for creating without std::string you are going to need dynamic arrays then or allocate more space on your static array. Right now there is no padding space for the extra 7 characters to go. You could possibly try using the replace or insert/find functions in the string library for this or shift the stuff to the right manually I suppose.
Sep 21, 2014 at 8:38pm
I managed to get it to work! Sorry for responding so late!
Topic archived. No new replies allowed.