Q1: put the strings in an array and then sort them.
#include <vector>
#include <string>
#include <algorithm>
<snip>
std::vector<std::string> strarray[3];
<snip>
std::sort(strarray.begin(), strarray.end());
Q2: Open and close brackets are incorrect.
if (ch = 32)
{
cout << space << endl;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>>if(ch = 48,49,50,51,52,53,54,55,56,57)
You can't construct an if statement like that
if(ch >= 48 && ch <= 57)
or better yet if( ch >= '0' && ch <= '9')
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I dont understand how to use:
std::sort(strarray.begin(), strarray.end());
changing to
if( ch >= '0' && ch <= '9')
still gives me same result (underscore)
I think you're going to have to post your updated program for us to understand what you have tried.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
if (ch = 32)
{
You should change this to : if (ch == 32)
The single '=' means that you are checking if ch can be assigned the value '32'. What you want to do is : check if 'ch' contains the value '32'. (double ==)
and change this line: space = 95; with space = '_'; Magic numbers in a program are never a good thing.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
if (ch = 32)
{
cout << space << endl;
}
Hmmm, I just noticed this... ;)
But to get back on topic :
Here's a link about sort
What you need to do:
int main()
{
std:string str;
std::vector<std::string> strarray; // not strarray[3]
for (int c =0; c <2;c++) //loop 3 times for input
{
//get input in str
// push input in vector using pushback
}
//sort the vector here
//loop through the vector to display the results
return 0;
}
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
No. You've made a vector of ints, but you need a vector of strings
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403