This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 7 years ago.
I'm using C++ (using CERN's ROOT framework) and I'm having a little problem with strings. I'm trying to label a histogram axis using a string defined by the user earlier in the code. Here are the relevant parts of the code:
string xlabel;
...
cout << "Enter x-axis label:" << endl;
getline(cin >> ws, xlabel);
...
hist->GetXaxis()->SetTitle(xlabel);
Where the last line is just syntax that ROOT uses (usually xlabel here would be in quotation marks and you can type in what you want the label to be, but I am trying to input the string defined earlier in the code.)
Anyway, when I compile this, I get the following error:
error: no viable conversion from 'string'
(aka 'basic_string<char>') to 'const char *'
hist->GetXaxis()->SetTitle(xlabel);
^~~~~~
I have tried re-defining xlabel as a const char * but it didn't like that either. Does anyone have any suggestions on how I could define this string?
Thanks in advance!
Do this:
hist->GetXaxis()->SetTitle(xlabel.c_str());
// ^^^^^^^^
Related
This question already has answers here:
Single quotes vs. double quotes in C or C++
(15 answers)
Closed 5 years ago.
Goal
At the end, I want to know why C++ doesn't support char letter = "C"; but does support char letter = 'C'; (notice that the quotation marks are different).
Code
I am using Repl.it as a code platform.
#include <iostream>
int main()
{
char letter = "C";
std::cout << letter;
}
Error message
main.cpp: In function 'int main()':
main.cpp:5:19: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
char letter = "C";
They are needed because 'C' and "C" represent completely different types - the first is an integer value, while the second is an array of two characters (the letter 'C' plus an implicit null-terminator). Both are useful, and you need some way of saying which one you want, which is what the different kinds of quotes do.
Single quotes are for single characters whereas double quotes are used to create string literals. They mean different things.
See a more thorough explanation.
This question already has answers here:
Convert a single character to a string?
(9 answers)
Closed 6 years ago.
I am practicing on LeetCode.
I have the following CPP code:
string s="abcd";
queue<string> q;
string temp;
temp=s[2];
queue.push(temp);
Error:
Line 5: conversion from
'__gnu_cxx::__alloc_traits >::value_type {aka
char}' to non-scalar type 'std::__cxx11::string {aka
std::__cxx11::basic_string}' requested
I am wondering why this happen? I can't really change thte structure of this because temp will be growing over time (eg., temp='a'+'b'+'c'; queue.push(temp));
Read your code again ,
I am sure that you wanted to write :
queue.push(temp);
like this :
q.push(temp);
This question already has answers here:
What is the type of a string literal in C++? [duplicate]
(2 answers)
Closed 7 years ago.
When I use :
std::cout << "Hello world ";
Which type is "Hello world" ?
Where does it stored , so I can get it out and work with it ?
For some reasons, I don't want to use something like :
std::string str = "Hello world";
std::cout << str;
Please help me, I searched an hour but still no answer.
The type of a string literal is "constant array of char", with as many elements as characters in the literal, plus one for a final null character. Other versions of string literals (wide, unicode) are arrays of other character types (wchar_t, char16_t etc.) (e.g. see here).
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Why can't I write to a string literal while I *can* write to a string object?
(4 answers)
Is it possible to modify a string of char in C?
(9 answers)
Closed 8 years ago.
Basically, I am trying to increment the int value of port. This should be easy but I am a little stuck.
It compile fine, but I got this error when I run it:
Access violation writing location 0x001f5834
#include "stdafx.h"
#include "iostream"
using namespace std;
#define TESTING "5002"
int main()
{
char* port = TESTING;
int portint;
sscanf ( port, "%d", &portint );
portint++;
cout << portint << endl; // it works fine up to here, it prints 5003
sprintf ( port, "%d", portint);
return 0;
}
By default, compiler treats string literals as immutable, and an attempt to modify the contents of one results in an access violation error at run time because these strings are put into code segment, and it's read only. In your case, TESTING is a string literal, you can't not change its values. Try:
char port[] = "5002";
Meanwhile, the compiler should have warning on this: when you assign a const char* type to a char* type.
MS C++ compiler has a compiler option regards this: Zc:strictStrings.
You are trying to write "5003" back into "5002". "5002" is a string literal and cannot be written to.
I'll try to find a good duplicate for this question, because it has been asked in many ways, many times.
In your usage, "5002" becomes a static array of characters and as such can not be modified. I believe K&R address this, but I don't have the book in front of me right now. Behavior would be different if you had declared an array.
This question already has answers here:
printf with std::string?
(9 answers)
Closed 9 years ago.
So this is my code
string name;
cout <<"\n Enter Your name : \n";
cin >> name;
printf("%s" , name);
and for some weird reasons codeblocks crashes at this
why ?
also how could I fix it ?
thanks
You should compile with all warnings (e.g. g++ -Wall). You'll get a useful warning. You want to use c_str like this
printf("%s", name.c_str());
BTW, why use printfand why do you forget a \n at the end of the printf format string? (or use fflush)
Better code:
cout << name << endl;
If you need to pass your std::string to a function that accepts / uses C-style strings (const char *) for input, use .c_str(). It returns a const char *.
This is what you should do when needing to work with existing libraries, system calls, etc. For your own code, it is usually better to find a more C++ way of doing it.
In this case:
std::cout << name << std::endl;