printing a set containing strings on graphics.h in c++ - c++

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.

Related

C++ convert std::string to char

I have following scenario:
struct MyStruct
{
char name[25];
char address[12];
}
Somewhere::SomeMethod(std::shared_ptr<SomeArgumentClass> args)
{
// GetName() and GetAddress() both return std::strings
::MyStruct newValue = {args->GetName(), args->GetAddress()};
// add newValue to some structure that needs to have chars
}
Error:
error C2440: 'initializing' : cannot convert from 'std::string' to 'char'
error C2440: 'initializing' : cannot convert from 'std::string' to 'char'
I am not able to get my std::string converted to a char.
Thanks a lot!
Firstly, your terminology is incorrect. You don't want to convert to char, which is a single character. I realise that the error message claims you're trying to convert to char, but that's because your incorrect code has confused the compiler into thinking you're trying to initialise individual elements of the array name. Be wary of the specifics of an error message when you write the wrong code, because the compiler cannot read your mind — it can only go off of what you've written!
In reality you want to copy the characters inside your std::string, into the array of char, that is your member.
Like so:
Somewhere::SomeMethod(std::shared_ptr<SomeArgumentClass> args)
{
const auto& name = args->GetName();
const auto& address = args->GetAddress();
::MyStruct newValue;
std::copy(std::begin(name), std::end(name), &newValue.name[0]);
std::copy(std::begin(address), std::end(address), &newValue.address[0]);
// add newValue to some structure that needs to have chars
}
But you need to add bounds checking too. To do that, roughly I might consider replacing each std::copy call with something like:
std::copy_n(
&name[0],
std::min(name.size(), sizeof(newValue.name)),
&newValue.name[0]
);
You'll also need some null termination depending on how many characters were copied.
Generally though the salient point is that you need to actually copy those characters because there is no one-step way to do it. It stems in part from how arrays can't be assigned-to, but also from the fact that their dimensions are part of the type which makes things more complicated.
If this seems like a pain in the arse, that's because it is — ideally you would stick with std::string across the board because it's far superior, and was specifically introduced to make it easier to handle arrays of char. You can still pass a const char* (or char*!) pointing to the string's data into a C API. Of course if ::MyStruct itself is a third-party C type, you have no choice but to do it the hard way.

C++: incompatible types in assignment of

I create this struct:
struct message_text{
char text[1024];
};
message_text instance;
instance.text = "HELLO WORLD, FIRST MESSAGE";
I get this error:
error: incompatible types in assignment of ‘const char [27]’ to ‘char [1024]’
instance.text = "HELLO WORLD, FIRST MESSAGE";
I don't understand this error because the size of instance.text does not exceed the maximum message size, which is 1024 bytes.
I have seen other similar questions but I could not understand what my mistake was when creating the structure
Help is appreciated
char arrays don't work like that - you can't assign them in the same way you would an int, for example (although you can do it when initializing the array). If you want to copy that literal into your char array, use strncpy.
Or, better than that, seeing as you've tagged it as C++ - just use the standard string class.
As others mentioned, you should be using std::string. But if you really want to assign a string literal to the array, you can do like below:
struct message_text{
char text[1024];
template <int N>
void assignText(const char (&other)[N]) {
static_assert(N < 1024, "String contains more than 1024 chars");
for(int i =0 ; i < N ; ++i) {
text[i] = other[i];
}
}
};
Note:
The above only works for assigning from string literals or char arrays. Does not support null terminated strings.
Since assignText is a template, it generates a function for each usage with unique N. This might lead to increased binary size.
Since assignText is a template, you have to define it in a header or within the same source file.
Compiler Explorer: https://godbolt.org/g/1oLcAq

cannot convert from 'const char [3]' to 'char *' x100000 (Qt Creator C++ Windows 32)

Everything was working fine just five minutes ago when I tapped f5 and got 102 errors:
error: C2440: 'initializing' : cannot convert from 'const char [17]' to 'char *'
Conversion from string literal loses const qualifier (see /Zc:strictStrings)
That specific one is at line 30:
char* hexchars = "0123456789ABCDEF";
I haven't touched the file the errors are in for at least a week. I'd normally say I accidentally changed something in the compile args or something, but I haven't opened settings since much before it started erroring.
Any ideas? I must have absentmindedly changed some setting but I really can't remember thinking "uh oh what did I just do?"
When you use code like this
char *astring2 = "some letters";
C++ (and C) puts that into read only memory. You can not modify the contents of a char pointer initialized with a literal even if it is not const.
Also you can not change the address of the pointer because it will cause a memory leak due to the rule above.
This, however, does not follow that rule UNLESS you make it const:
char astring[] = "some letters that can be changed";
char *ptrToString = astring; //work
astring2 = astring //not work
String literals are of type char const[N] since C++ was first standardized. At this point C didn't support const and a lot of code assigned string literals to char*. As a result a special rule was present in C++ which allowed initialization of char* from string literals. This rule was immediately deprecated.
C99 introduced a const keyword, too. When C++11 was standardized the deprecated rules was pulled and it is no illegal to initialize a char* from a string literal as it should have been right from the stand. The expectation was that C++ compilers warned about the deprecated assignment since years (and all vendors stated they did), i.e., users had years of lead-time to fix their code.
The obvious fix is to initialize a char const* instead of a char* from a string literal.
If you really need a pointer to a mutable array of chars you can create it and get it initialized using
char array[] = "string literal";

invalid conversions, unspecified behaviour and char arrays?

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

Beginner C++ Question

I have followed the code example here
toupper c++ example
And implemented it in my own code as follows
void CharString::MakeUpper()
{
char* str[strlen(m_pString)];
int i=0;
str[strlen(m_pString)]=m_pString;
char* c;
while (str[i])
{
c=str[i];
putchar (toupper(c));
i++;
}
}
But this gives me the following compiler error
CharString.cpp: In member function 'void CharString::MakeUpper()':
CharString.cpp:276: error: invalid conversion from 'char*' to 'int'
CharString.cpp:276: error: initializing argument 1of 'int toupper(int)'
CharString.cpp: In member function 'void CharString::MakeLower()':
This is line 276
putchar (toupper(c));
I understand that toupper is looking for int as a parameter and returns an int also, is that the problem? If so how does the example work?
Also,
char* str[strlen(m_pString)];
int i=0;
str[strlen(m_pString)]=m_pString;
is not valid C++ - arrays must be dimensioned using compile time constants - this is a C99 feature. And I really don't think the code would do what you want it to, even if it were legal, as you seem to be accessing one past the end of the array. It would be handy if you posted the complete class definition.
I don't think your code does what you want it to do and in fact if it compiled it would explode.
char* str[strlen(m_pString)]; // you've made an array of X C strings where
// X is the length of your original string.
int i=0;
str[strlen(m_pString)]=m_pString; // You've attempted to assign the C string in your array
// at location X to point at you m_pString. X is the
// same X as before and so is 1 past the end of the array
// This is a buffer overrun.
I think what you actually wanted to do was to copy the content of m_pString into str. You'd do that like so:
char * str = new char[strlen(m_pString)];
memcpy(str, m_pString); // I may have the operands reversed, see the docs.
The easier way to do this though is to stop using C strings and to use C++ strings:
std::string str = m_pString;
There are more issues, but this should get you steer you more toward the right direction.
You need to feed toupper() an int (or a char) instead of a char *, which is how you've declared c.
try:
char c;
Also,
char* str[strlen(m_pString)];
is an an array of pointers to characters, not just a single string.
This line:
str[strlen(m_pString)]=m_pString;
is an assignment to a bad pointer then, since there was no allocation.
I'm going to go with the assumption that m_pString is a C style string (char *). You're doing way more fiddling than you need to be doing.
void CharString::MakeUpper()
{
char* str = m_pString; // Since you're not modifying the string, there's no need to make a local copy, just get a pointer to the existing string.
while (*str) // You can use the string pointer as an iterator over the individual chars
{
putchar (toupper(*str)); // Dereference the pointer to get each char.
str++; // Move to the next char (you can merge this into the previous line if so desired, but there's no need.
}
}
In the example you cite, the reason it works is because of how the variables are declared.
int main ()
{
int i=0;
char str[]="Test String.\n"; // This is a compile time string literal, so it's ok to initialize the array with it. Also, it's an array of `char`s not `char*`s.
char c; // Note that this is also a `char`, not a `char *`
while (str[i])
{
c=str[i];
putchar (toupper(c));
i++;
}
return 0;
}
Because of the error-prone ways of using C strings, your best bet is std::string:
void CharString::MakeUpper()
{
string str(m_pString);
transform(str.begin(), str.end(), ostream_iterator<char>(cout), &toupper);
}
There is not a built-in conversion from char * to int, which is why the error occurs. Since you're trying to capitalize a character, you need to dereference the pointer.
putchar(toupper(*c));