Hello I have the following code:
#include <iostream.h>
#include <fstream.h>
#include <string>
#include <iomanip>
int length(const char* str)
{
int length;
for (int i=0;str[i]!='\0'; i++)
{
length=i;
}
return length;
}
void break_lines(string buf)
{
const char* str;
strcpy(str, buf); <<<-----------------------
--- HERE
int l = length(str);
char buffer[l+1];
for (int i=0, j=0;i<=l; i++)
{
if (str[i] != ' ')
{
buffer[j] = str[i];
}
else
{
cout << buffer << endl;
}
}
}
int readFile()
{
ifstream inFile("rules.txt");
string line;
while (!inFile.eof())
{
getline(inFile, line);
break_lines(line);
cout<<line<<endl;
}
inFile.close();
return 0;
}
int main()
{
readFile();
return 0;
}
I would like to convert the "buf" variable to "str" character array but I get the following error:
c:\applications\quincy99\a
ssignment\
start.cpp:
In function `void break_lines(basic_string<c
har,string
_char_trai
ts<char>,_
_default_a
lloc_templ
ate<false,
0> >)':
c:\applications\quincy99\a
ssignment\
start.cpp:
20: passing `const char *' as argument 1 of `strcpy(char *, const char *)' discards qualifiers
c:\applications\quincy99\a
ssignment\
start.cpp:
20: cannot convert `buf' from type `string' to type `const char *'
Any help of how I can do it is appreciated.
I am using the g++ compiler.
Regards
Start Free Trial