WARNING: Extremely limited knowledge of C++ and coding in general. Please refrain from advanced terminology.
for ( i = 0; i < answer.size(); ++i) {
if (guess == answer.at(i)) { //to display correct letters in answerDisplay
answerDisplay.replace( (2 * i), 1, answer.at(i) );
correctGuesses += 1;
}
Given: answerDisplay and answer are strings.
When I run my program there is a compile-time error at the third line of what I've posted saying:
invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
What's the problem? How can I fix it? All other posts with this error talked about pointer characters but I don't know what those are.
Pointer characters are they way plain strings are implemented in C and C++. In C++ you have the nice class std::string, but string literals are still array of characters. And arrays in C and C++ can be seen as pointers.
For example, "hello" is of type const char[6] (5 characters plus the ending NUL), but it can be trivially converted to const char *, and that in turn can be converted to std::string.
In line 3, the only relevant code is a call to the member function std::string::replace(). There are a lot of overrides of this function (different sets of parameters to be used), but the one the compiler is trying to use is this one:
string& replace (size_t pos, size_t len, const char* s);
As you can see, it takes two numbers and a const char * (an old-string/char-array). But you are passing as third parameter answer.at(i) that is of type char. Hence the error:
invalid conversion from ‘char’ to ‘const char*’
Solution? You can build a string from that char:
answerDisplay.replace( (2 * i), 1, std::string(1, answer.at(i))
Or you can get a substring of the original string instead of a plain character.
answerDisplay.replace( (2 * i), 1, answer.substr(i, 1))
Related
I compiled .cc file whith g++ on linux ubuntu, I want to use srtcmp() function to compare two strings. the strings are not constant. user will give both of them, but I get this error:
error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
and this is my code:
if (!strcmp(a[i].personalNo,pcode)){
#some code
}
which function can I use instead of strcmp() to compare two strings?
The problem isn't on the function but on the way that you're using it.
int strcmp ( const char * str1, const char * str2 );
strcmp takes two const char * arguments.
The error tells you that you are giving the function a char so the problem is on the types of personalNo and/or pcode. Your mistake is probably on the declaration of the type of those two variables. You would want to change their type to char * as char only stores one character while char * is an array of characters.
Also, an another way to compare two strings in C++ is to use std::string. Then you can just do the following (provided that both personalNo and pcode are std::string:
if (a[i].personalNo != pcode){
#some code
}
I am trying to print the elements of a set containing strings on graphics.h console using outtext() function,but i get this error:
cannot convert 'std::string {aka std::basic_string}' to 'char*' for argument '1' to 'void outtext(char*)'|
this the piece of code that gives error:
for(i=0;i<20;i++){
for(j=0;j<20;j++){
outtext(str[i][j]);
}
}
the template for the outtext function in the graphics.h header is like this:
void outtext(char *textstring);
i have used c_str() like this:
for(i=0;i<20;i++){
for(j=0;j<20;j++){
outtext(str[i][j].c_str());
}
}
but this time it gives this error:
error: invalid conversion from 'const char*' to 'char*' [-fpermissive]|
You can try this one as well:
char *cstr = new char[21]; // just in case string length is maxed at 20, leave 1 character for '\0'
for (int i = 0; i<20; i++) {
for (int j = 0; j<20; j++) {
strcpy_s(cstr, str[i][j].length() + 1, str[i][j].c_str());
outtext(cstr);
}
}
delete[] cstr;
Just added a char* string to temporarily hold the converted std::string value. The tricky part is that char* strings normally have the terminating character \0 which std::string don't have, so you have to add 1 more character to the size of each "row" of str.
I take it this question is about the 30 years old BGI graphics library and Borland C++. The root of the problem is that this library was poorly written, as it didn't implement const correctness.
The Turbo C++ compiler did not follow anything remotely close to any C++ standard, so you are mostly out of luck. If you had a proper C++ compiler you could use const_cast, but I very much doubt this is available to you.
The only solution left is the dirty, bad way:
outtext((char*)str[i][j].c_str()); // bad practice
You should never cast away const like this in neither C nor C++.
If you can change the prototype of the output function then it is better to change void outtext(char *textstring); to void outtext(const char *textstring); because there is no need for the output function to modifiy the string. Otherwise you could use const_cast before passing to the function like outtext(const_cast<char*>(str[i][j].c_str())) or copy the string to another char* and passed the copied value.
I'm using a function to download a file.
void downloadFile(const char* url, const char* fname) {
//..
}
This is called like :
downloadFile("http://servera.com/file.txt", "/user/tmp/file.txt");
This working fine.
But I want to change the URL to be a value from an array. The array stores encrypted values which when decrypted are strings, so I get the issue error: cannot convert ‘std::basic_string<char>’ to ‘const char*’
I've tried:
string test = decode(foo[5]);
const char* t1= test.c_str();
downloadFile(t1 "filename.txt", "/user/tmp/file.txt");
downloadFile(t1 + "filename.txt", "/user/tmp/file.txt");
and
downloadFile((decode(foo[5]).c_str()) + "filename.txt", "/user/tmp/file.txt");
which gives:
error: invalid operands of types ‘const char*’ and ‘const char [17]’ to binary ‘operator+’
What am I doing wrong ?
Thanks
C-strings can't be concatenated with +.
Use std::string::+ instead:
downloadFile((test + "filename.txt").c_str(), "/user/tmp/file.txt");
Note that c_str only returns a pointer to the std::string's internal character array, so it's valid only during the execution of the downloadFile function.
Try this:
downloadFile((decode(foo[5]) + "filename.txt").c_str(), "/user/tmp/file.txt");
The operator+ is not defined for char arrays.
The main problem in your code is that you are trying to use operator+ to concatenate raw C strings (i.e. raw const char* pointers, or raw char [] arrays), which doesn't work.
In C, you should use proper library functions (like strncat or safer variants) to do that; but since you are using C++, you can do better, and write easier code: just use a C++ string class, like std::string.
In fact, the C++ standard library offers convenient overloads for operator+ that work with std::string, so you can concatenate C++ strings in an easy, intuitive and safe way; for example:
// Build your URL string
std::string test = decode(foo[5]);
std::string url = test + "filename.txt";
// Use std::string::c_str() to convert from C++ string
// to C raw string pointer const char*
downloadFile(url.c_str(), "/user/tmp/file.txt");
I made a struct of strings and every time I try and compare my strings, it says i am comparing ints and chars...but I am only comparing strings?
while(gap > 0){
passOk=true;
for(int i =0; i < *total-gap; i++)
if(strcmp(individualf->firstnames[i] , individualf->firstnames[i+gap])>0){
exchange(individualf[i], individualf[i+gap]);
passOk = false;
}
if(passOk)
gap /= 2;
}
}
MY complier error is: cannot convert ‘std::string {aka std::basic_string}’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’
if(strcmp(individualf->firstnames[i] , individualf->firstnames[i+gap])>0){
std::string has an operator>, use it:
if (individualf->firstnames[i] > individualf->firstnames[i + gap])
// stuff
If for some reason you must use strcmp, then just realize that std::string is not a const char*, and use std::string::c_str() to get a pointer to the string's memory:
if (strcmp(individualf->firstnames[i].c_str(), individualf->firstnames[i + gap].c_str()) > 0)
// stuff
You're treating C++ (STL) std::strings as old-school C strings. Forget all about C strings and just use the C++ ones.
If you need to compare them, they have a built-in compare method. If you need to play with case, etc, I highly recommend the Boost String Algorithms.
I am trying to do http://www.spoj.com/problems/SHLIGHTS/, for which I have designed a solution. I am very new to C++(about 14 days), and I am facing a lot of problems. Earlier I used Python, and there was nothing of these errors, anyways, I wrote this..
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
//example is GBGBBB
//t=0, GBGBBB then t=1,BGBGBB then t=2 BBGBGB, then t=3 BBBGBG
//search for GB and replace it with BG
//we need a function that replaces things
string swapSEQ(string SEQ)
{
unsigned int sizeSEQ=SEQ.size();
unsigned int curr(0);
while (curr<sizeSEQ-1)
{
if (SEQ[curr]=="G" and SEQ[curr+1]=="B")
{
SEQ[curr]="B";SEQ[curr+1]="G";curr+=2;
}
else {++curr;}
}
return SEQ;
}
int main()
{
unsigned int numCases;
scanf("%d",&numCases);
// cin>>numCases;
for (unsigned int currentCase=0;currentCase<numCases;++currentCase)
{
string SEQ;
//scanf("%s",&SEQ);
cin>>SEQ;
string swapped=swapSEQ(SEQ);
unsigned long long t=0;
while (swapped!=SEQ)
{
swapped=swapSEQ(SEQ);++t;
}
printf("%lld\n",t);
}
return 0;
}
I know that's a lot of details, but that's it. SPOJ shows blank lines after inputs and outputs, but after reading the description, I understand we have to do things in single lines. Here's what I get with my g++4.7 compiler(LINUX)
SHLIGHTS.cpp: In function ‘std::string swapSEQ(std::string)’:
SHLIGHTS.cpp:17:18: error: comparison with string literal results in unspecified behaviour [-Werror=address]
SHLIGHTS.cpp:17:18: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
SHLIGHTS.cpp:17:37: error: comparison with string literal results in unspecified behaviour [-Werror=address]
SHLIGHTS.cpp:17:37: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
SHLIGHTS.cpp:17:52: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
SHLIGHTS.cpp:17:66: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
cc1plus: all warnings being treated as errors
*What is happening? There's something about pointers, const char and unspecified behaviour.
**I know pointers are sort of variables that point to memory locations, nothing more.
**I've used scanf at some places and cin at others(if I replace scanf by cin, I get the same errors)
**Is it something about the fact that I returned a string that took as argument?
**Where did I use a pointer?
**Am I wrong about this- strings in c++ are char arrays? If no, then where is the invalid conversion?
Thanks in advance, and apologies for anything wrong. If it's too long, please answer any of the doubts.
You need to compare SEQ[curr] with 'G' not "G" since it's a char and not a string.
You should use operator && instead of and.
Something with your logic is worng. At one index of a string you can have only 1 char. So writing if (SEQ[curr] == 'G' && SEQ[curr] == 'B' is same as writing if (false).
It is not an error, but please don't abuse your code by writing more than one commend at a line.
If you writing is C++ please use cin , not scanf.
Why are you creating sizeSEQ if you never use it? Don't!
you should use 'G' instead of "G" and so on. When you access a char array (e.g. arr[5]) you obtain a char, which you can compare with a char literal (being: 'G') and not with a cstring (e.g. "G" or "Google").
The compiler is your friend, it points out that the problem is:
comparison with string literal