Solved

taking substrings of a string

Posted on 2013-05-18
3
246 Views
Last Modified: 2013-05-18
Why this little code doesn't work?I want to take a string and then assign substrings to another string.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string x = "ALERT";

string y = x[0] + x[1];   
cout << y;    //PRINT AL
return 0;
}

Open in new window

0
Comment
Question by:codeBuilder
  • 2
3 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 39177182
0
 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points
ID: 39177188
x[0] returns char, and + on char doesn't mean concatenate. It means add the two char numbers together.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 39177233
Since phoffric has elaborated why your attempt does not work, here's what would work - and it's surprisingly close to your question title:

#include <iostream>
#include <string>
using namespace std;
int main()
{
string x = "ALERT";

string y = x.substr(0,2);   // 1st parameter is start position, 2nd is length
cout << y;    //PRINT AL
return 0;
}
                                  

Open in new window


See also http://en.cppreference.com/w/cpp/string/basic_string/substr and http://en.cppreference.com/w/cpp/string/basic_string
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
getting a fire fox page (view page source ) in c++ 6 42
FMX enumerated colours 2 101
C++ to C# code conversion issue 4 109
object oriented programming comparison 5 72
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question