Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to create a string that includes a vector, and I am not quite sure how I can achieve this.
My string is as follows:
char * cmd = "-1 $Controller SendPosition([VECOTR VALUE HERE]) \0";
The float that I want to insert into the string at the position [FLOAT VALUE HERE] is returned from the function:
object.getPosition()
An example of the final string that I need should look like this:
-1 $Controller SendPosition(43.611, 110.681, 136.22) \0
use stringstream to concat strings and values.
#include <sstream>
std::stringstream ss;
ss << "-1 $Controller SendPosition(" << vec[0];
for( size_t i=1; i<vec.size(); i++ ) ss << ", " << vec[i] ;
ss << ")";
cout << ss.str();
This should work:
std::vector<char> v;
... Add to vector
std::string s("-1 $Controller SendPosition([");
s += v.front();
s += "])";
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
Here is an example of what I mean.
Input: 10.20.50
a = 10
b = 20
c = 50
you will need to store the input in a string or char[] and then iterate over the string or char[] and write some code that will identify the separate parts of the input and convert them to ints using stoi().
this would work but just an example (and i think it will not print the final number unless the input is ended with "." but this should give you an idea of what you could do.
std::string i = "";
std::cin >> i;
std::string buffer = "";
for (auto c : i)
{
if (c != '.')
{
buffer += c;
}
else
{
int num = std::stoi(buffer);
buffer = "";
std::cout << num << ", ";
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a project in c++, and need to find largest filename from the text file.
My text file example is:
foundedindex = instline.find(" ");
inst_host = instline.substr(0, foundedindex);
//cout << inst_host << " a" << endl;
obj[count].sethost(inst_title);
So, I want to read only "index.html", "23,html", "24.html" etc.
When I seperate all line by one by like in code, sorting takes too much time.
Please help me.
This is a way to get the columns of each line.
std::string temp;
std::vector<vector<std::string>> data;
while(std::getline(file, temp))
{
std::vector<std::string> x;
std::istringstream liney(temp);
while(std::getline(liney, temp, ' '))
{
x.push_back(temp);
}
data.push_back(x);
}
// Then using a column loop through a 2d array.
int row = 0;
for(int i = 0; i<data[row].size(); i++)
{
for(int s = 0; s<data.size(); s++)
{
data[s][i]; // Do something
}
row++;
}
Hope this helps.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am very new at coding and I just had a quick question.
How would you find the nth character from a string in an array for c++?
so for example
{"Hey you" "echo" "lake" "lend" "degree"}, n=0
would equal "hello"
thanks! I'm not going to ask for the full code but just tips on where to get started.
Here are some examples:
unsigned int n = 3;
static const char hello[] = "hello";
cout << hello[n] << "\n";
const std::string apple = "apple";
cout << apple[n] << "\n";
static const char * many[] = {"These", "are", "many", "strings"};
cout << many[n][n] << "\n";
cout << many[n] << "\n";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I know how to use a string to calculate the number of characters, but I'm not sure how to use a function to do that. have to use CSTRING. THANK YOU ALL
#include <cstring>
char a[10];
cout << "Please enter anything: ";
cin.getline(a,10);
cout << "You type " << strlen(a) << " letters long"<<endl;
You're probably looking for std::string since you're question mentioned C++ and not only C.
include <string>
std::string myString = "Something";
size_t stringLength = myString.size();
It's simple. Just type your code inside a function()
int stringlengthfunction()
{
char str[80];
int i;
cout<<"\n enter string:";
cin.getline(str,80);
int n=strlen(str);
cout<<"\n lenght is:"<<n;
getch();
return 0;
}
or pass your string as a parameter to the function
int stringlengthfunction(string str)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
word[0] returns the entire string instead of the first element.
How do I access elements in a const string& word that I obtain from a function.
void something(const string& word)
{
int i = 0;
for (int i = 0; i < word.size(); i++)
cout << word[i];
}
Is there a way to convert it into a string??
Please help!!
Works correctly in my system, no problem. Here's sample code:
#include <iostream>
#include <string>
void myfunc(const std::string& word){
std::cout << word[0] << std::endl;
}
int main(void){
myfunc("test");
}
Output:
~$ ./a.out
t
If you just want to convert from const string into string, just copy it.
std::string new_word = word;
std::cout << new_word;