Hi I need some help with these C++ programs in Dev C++:

#include <iostream>
using namespace std;

int main()
{

 std:string str, str2, str3;

 std::cin >> str;
  std::cin >> str2;
   std::cin >> str3;

 std::cout<<str.length()<<std::endl;
  std::cout<<str2.length()<<std::endl;
   std::cout<<str3.length()<<std::endl;[/QUOTE]

I type in a word for each string and I want to organise the result by the number of letters in each word from largest to smallest or vice versa

Second question

#include<iostream>
using namespace std;

int main()
{

  char ch, space, number;
  space = 95;
  number = 35;
  cout << "Enter a char:";
  ch=cin.get();

 {
  if (ch = 32)
     cout << space << endl;
     }
     {
  else if(ch = 48,49,50,51,52,53,54,55,56,57)
  {
          cout << number << endl;
          } 

I'm simply having problems with the result. It should be self explanitory looking at this, If I type a 'space' I want an underscore, this works. But when I go to type any 1 digit number I still get an underscore instead of a '#'

Thanks for any assistance :)

and Im very new at this, so if you say some technical jargon - I won't understand.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

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;
}

Question 2

Oh yes, thats from me mucking around with them - however when I fix it, I still get the same answer.


and thanks

>>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')

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 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.

#include<iostream>

using namespace std;

int main()
{

  char ch, space, number;
  space = 95;
  number = 35;
  cout << "Enter a char:";
  ch=cin.get();
 
 
  if (ch = 32)
 {
     cout << space << endl;
     }

  else if( ch >= '0' && ch <= '9')
  {
          cout << number << endl;
          }  
  else cout << "a";     

  system("pause");
  return 0;
}

Line 22 should give me #, I still get _

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{

 std:string str, str2, str3;
 
 std::cin >> str;
  std::cin >> str2;
   std::cin >> str3;
   
 std::cout<<str.length()<<std::endl;
  std::cout<<str2.length()<<std::endl;
   std::cout<<str3.length()<<std::endl;
   
std::vector<std::string> strarray[3];
std::sort(strarray.begin(), strarray.end());

       system ("pause"); 
       return 0;
       }

I didnt understand what he ment by <snip>

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.

Edited 5 Years Ago by Nick Evan: n/a

Thanks a lot, was just reading up on it :)

Now I just need someone to explain the 2nd program for me

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;
}

Edited 5 Years Ago by Nick Evan: n/a

Ok thanks but I dont know how to assign the number of characters in str, str2 or str3

to vec.push_back();

Oops ill just clear up my last post

int main() 
{
std::vector<int> vec;
vec.push_back(); vec.push_back(); vec.push_back(); 
std::sort(vec.begin(), vec.end());
for (int i = 0; i < vec.size(); ++i) 
std::cout << vec[i] << ' ';
}

how do I make str, str2, str3 part of vector int?

is this right?

This question has already been answered. Start a new discussion instead.