I have a string array and an integer array. I want to convert the elements of string array to integer and then store them in the integer array. I wrote this code :
string yuzy[360];
int yuza[360];
for(int x = 0;x<360;x++)
{
if(yuzy[x].empty() == false)
{
yuza[x]=atoi(yuzy[x]);
cout<<yuza[x]<<endl;
}
else
continue;
}
this piece of code gives this error:
error: cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '1' to 'int atoi(const char*)'
When I write the content of the string (-75dbm) in atoi function it works fine. But when I write (yuzy[x]), I get the error. How can I make atoi works well with string array?
Thanks.
atoi() takes C strings (char pointers) and not C++ string objects. Use
atoi(yuzy[x].c_str());
instead.
As an alternative to atoi, you could use std::stoi and related functions, if you have C++11 support.
yuza[x] = std::stoi(yuzy[x]);
atoi accept a c-style string as parametter, so, you could use atoi(yuzy[x].c_str());
Related
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");
This is in reference to the following answer by Synxis.
https://codereview.stackexchange.com/questions/18684/find-all-substrings-interview-query-in-c/18715#18715
Suppose, I have to print all substrings of the string "cbaa". To do this, I have to invoke the method like this:
findAllSubstrings2("cbaa");
If I take a string from user, and do the following:
string s;
cin>>s;
findAllSubstrings2(s);
it gives the following error:
[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'void findAllSubstrings2(const char*)'
Why does this happen?
As the error message says the parameter of function findAllSubstrings2 is declared as having type const char * while you are trying to pass an argument of type std::string
string s;
//...
findAllSubstrings2(s);
You should use member function c_str or data (starting from C++ 11) of class std::string. For example
findAllSubstrings2(s.c_str());
you using string, in function is char try to use char[] s;
use c_str() method in string class when passing the argument
string s;
cin>>s;
findAllSubstrings2(s.c_str());
You probably should change the type of the parameter of the function. Somethink like:
void findAllSubstrings2(string s){
//... function implementation...
}
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 want to make function that can return two new string that is the composition of the old one but I got an above error.
string constru(string num, int pos_be, int pos_end)
{
string f_num="";
string s_num="";
f_num.append(num.at(pos_be));
f_num.append(num.at(pos_end));
num.erase(pos_be);
num.erase(pos_end);
for(int i=0; i<num.size();i++)
{
s_num.append(num.at(i));
}
return f_num,s_num;
}
The Error is at the line f_num.append(num.at(pos_be)) as well as the other lines that I used append with string. Does anyone want know what went wrong here?
The problem here is, the at function returns a char and not a string. But the append function supports a string. So you get this error. Convert the char to string before you append.
f_num.append(std::string(num.at(pos_be)));
f_num.append(std::string(num.at(pos_end)));
f_num.append(std::string(num.at(pos_be)));
f_num.append(std::string(num.at(pos_end)));
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