Convert char* to <string> - c++

This is my first post, so please be understanding for me :)
I would like to use vector of strings to make sort data easy, but I need this string also to function fun1. So I would like to convert char* word to string str but i can't manage to do it. I was searching answer for my question but I didn't find.
Please help, here is code:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <string.h>
using namespace std;
vector <string> tab;
vector <string> tab2;
int l['Z'+1];
void fun1(char *t)
{
for(int i = 0; t[i]; i++)
l[t[i]]++;
int j = 0;
for(int i = 'A'; i <= 'Z'; i++)
if(l[i])
{
t[j++] = i;
l[i--]--;
}
}
int main()
{
char * word;
string str;
ios_base::sync_with_stdio(0);
int z;
int n;
cin >> z;
while(z--)
{
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> word;
fun1(word);
str.assign(word, sizeof(word));
tab.push_back(str);
}
sort(tab.begin(), tab.end());
for(int i = 0; i < tab.size(); i++)
cout << tab[i] << endl;
}
}

Firstly, I have no idea, why you want to convert char* to string. In your solution firstly you have to allocate memory for chars
char *word = new char[HOW_MANY_CHARS]
But there is better solution. You can write:
cin >> str;
fun1(str);
tab.push_back(str);
And you have to change fun1 to:
void fun1(string &t);

Related

Segmentation fault caused by simple c++ code

Absolute beginner here. I'm trying to solve this but get a segmentation fault. I tried searching for a solution but couldn't find the reason why this doesn't work.
To reproduce the error simply copy the code below and paste it into the editor under the link above.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
//using namespace std;
int main() {
int n, q;
std::cin >> n >> q;
std::vector<std::vector<int>> arr;
// handle n lines representing the arrays
for (int i = 0; i < n; i++) {
std::vector<int> numbers;
std::string line;
getline(std::cin, line);
std::istringstream iss(line);
int enterNumber;
while (iss >> enterNumber)
{
numbers.push_back(enterNumber);
}
arr.push_back(numbers);
}
// handle q lines representing i, j
int x, y;
for (int i = 0; i < q; i++) {
std::cin >> x >> y;
std::cout << arr[x][y];
}
return 0;
}
What am I missing? Why doesn't it work?
Input that causes the segmentation fault:
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
Expected output:
5
9
1) getline(std::cin, line); . Line contains space and numbers.
2) Handle spaces in lines
3) Handle first int in line for array length. (You tried to add array length in array itself)
Here is working code for reference. (Passed all test cases)
#include <vector>
#include <iostream>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
vector< vector<int> > arr;
int temp, array_count;
for(int i = 0; i < n; i++) {
vector<int> numbers;
cin>>array_count;
for(int j = 0; j < array_count; j++) {
cin>>temp;
numbers.push_back(temp);
}
arr.push_back(numbers);
numbers.clear();
}
// handle q lines representing i, j
int x, y;
for (int i = 0; i < q; i++) {
cin >> x >> y;
cout << arr[x][y]<<"\n";
}
return 0;
}
To fix the segmentation fault simply add std::cin.get(); after the first std::cin(third line in your main function).
The segmentation fault happened because getline(std::cin, line);returned an empty string during the first iteration of the for loop. See this.
Note that even after fixing the segmentation fault issue your code is still wrong (doesn't solve the challenge) :p
Try this:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
//using namespace std;
int main() {
int n, q;
std::cin >> n >> q;
std::cin.get();
std::vector<std::vector<int>> arr;
// handle n lines representing the arrays
for (int i = 0; i < n; i++) {
std::vector<int> numbers;
std::string line;
getline(std::cin, line);
std::istringstream iss(line);
int enterNumber;
while (iss >> enterNumber)
{
numbers.push_back(enterNumber);
}
numbers.erase(numbers.begin()); // because first value is k
arr.push_back(numbers);
}
// handle q lines representing i, j
int x, y;
for (int i = 0; i < q; i++) {
std::cin >> x >> y;
std::cout << arr[x][y] << std::endl;
}
return 0;
}

Segmentation Fault whenever using vector<>

int main()
{
int n;
cin >> n;
char* temp_char;
vector<string> arr;
for (int i = 0; i < n; i++)
{
cin >> temp_char;
}
return 0;
}
Hi, whenever I have vector<string> arr; in this program, no matter I actually use this variable arr or not, after the first cin in the first forloop, I receive segmentation fault, don't know why is this happening? I do need to use vector<string> in the later programming.
I'm compiling with g++ under Ubuntu, any help would be much appreciated!
char* temp_char;
vector<string> arr;
for (int i = 0; i < n; i++)
{
cin >> temp_char;
}
Why the pointer?
What happens here is that you create a char* which points to an undefined location. std::istream (of which std::cin is an object) provides a special way to read input into a char*, which however works only when the target char* points to valid memory.
Otherwise, the behaviour is undefined, which means anything can happen, including crashes.
The solution is not use pointers but std::getline:
#include <iostream>
#include <string>
#include <vector>
int main()
{
int n;
std::cin >> n;
std::cin.ignore();
std::vector<std::string> arr;
for (int i = 0; i < n; ++i)
{
std::string line;
std::getline(std::cin, line);
arr.push_back(line);
}
}
I'm not sure in your code, but I think you have to use
char temp_char
insted of
char* temp_char
or at least allocate some memory to temp_char:
#include <iostream>
#include <vector>
int main()
{
int n;
std::cin >> n;
char* temp_char = new char[100];
std::vector<std::string> arr;
for (int i = 0; i < n; i++)
{
std::cin >> temp_char;
arr.push_back(temp_char);
}
for (auto s : arr)
std::cout << s << std::endl;
return 0;
}

Error trying to convert string char into int

I have a simple program where I want to store the input into matrices for easy access. I am having trouble converting a simple string character into an int, can someone explain why my code is giving me this message when I try and compile?
acm.cpp:20:42: error: request for member ‘c_str’ in ‘temp.std::basic_string<_CharT, _Traits, _Alloc>::operator[]<char, std::char_traits<char>, std::allocator<char> >(((std::basic_string<char>::size_type)j))’, which is of non-class type ‘char’
The problem seems to be with my use of the c_str() function, but if i'm not mistaken this is necessary for converting characters into int values.
My code:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
// read input from console
int N, M;
cin >> N; // number of people
cin >> M; // max number of topics
// read in binary numbers into matrix
int binaryNumbers[N][M];
for (int i = 0; i < N; i++) {
string temp;
cin >> temp;
for (int j = 0; j < M; j++) {
binaryNumbers[i][j] = atoi(temp[j].c_str());
cout << binaryNumbers[i][j] << endl;
}
}
return 0;
}
Use:
binaryNumbers[i][j] = temp[j] - '0';
You don't need to use atoi for a single digit, its numeric value is simply its offset from '0'.
But if you really want to use atoi, you will have to create a separate string:
for (int j = 0; j < M; j++) {
char digit[2] = "0";
digit[0] = temp[j];
binaryNumbers[i][j] = atoi(digit);
cout << binaryNumbers[i][j] << endl;
}

Convert a double* to std::string in C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to convert a double* to string in C++:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
double *f = new double[5];
for(i = 0; i < 5; i++)
{
f[i] = 5;
}
string f_str;
//this is for double to string
//f_str = std::to_string(f);
//i want for double*
cout << f_str << '\n';
delete [] f;
return 1;
}
Try to use to_string:
std::stringstream ss;
for(i = 0; i < 5; i++)
{
f[i] = 5;
ss << std::to_string(f[i]) << ", ";
}
string f_str = ss.str();
Try this
#include <iostream>
#include <string>
#include <sstream>
int main( )
{
const int SIZE(5);
double *f = new double[SIZE];
// fill data
for(int i(0); i < SIZE; i++)
f[i] = 5;
std::string doubArray2Str;
for(int i(0); i < SIZE; ++i){
std::ostringstream doubleStr;
if ( i == SIZE - 1 )
doubleStr << f[i];
else
doubleStr << f[i] << ",";
doubArray2Str += doubleStr.str();
}
std::cout << doubArray2Str << std::endl;
delete [] f;
return 0;
}
You can try the following code. Hope this will help you buddy. Thanks.
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#define MAX 1000
using namespace std;
typedef long long ll;
string DOUBLE_TO_STRING(double data)
{
string number;
stringstream out ;
out << data;
number = out.str();
return(number);
}
int main()
{
ll n;
while(cin >> n)
{
double a[MAX];
vector<string> str;
for(ll i=0; i<n; i++){
cin >> a[i];
str.push_back(DOUBLE_TO_STRING(a[i]));
}
for(ll i=0; i<str.size(); i++)
cout << str[i] << "\n";
}
return 0;
}

Pointer to vector

I've got this code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> *vecptr;
int veclen;
void getinput()
{
string temp;
for(int i = 0; i < 3; i++)
{
cin>>temp;
vecptr->push_back(temp);
}
veclen = vecptr->size();
}
int main()
{
getinput();
for(int i = 0; i < veclen; i++)
{
cout<<vecptr[i]<<endl;
}
return 0;
}
My compiler(G++) throw me some errors: test2.cpp:28:17: error: no match for 'operator<<' in 'std::cout << *(vecptr + ((unsigned int)(((unsigned int)i) * 12u)))' ...
What's wrong? What can I do to fix it?
The program is still not completely right. You have to initialize the vector pointer and then give it a size and the use it. A full working code could be,
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> *vecptr = new vector<string>(10);
int veclen;
void getinput()
{
string temp;
for(int i = 0; i < 3; i++)
{
cin>>temp;
(*vecptr)[i] = temp;
}
veclen = (*vecptr).size();
}
int main()
{
getinput();
for(int i = 0; i < veclen; i++)
{
cout<<(*vecptr)[i]<<endl;
}
return 0;
}
Although I have mentioned the size as 10 you could make it a variant.
You need to dereference vecptr here to get the underlying vector:
cout << (*vecptr)[i] << endl;
You will also need to initialize vecptr.