Link to home
Start Free TrialLog in
Avatar of joedfuse
joedfuseFlag for United States of America

asked on

C++ string manip help!!!!

Hello all,

In my mission to learn c++ I ran into a roadblock yet again. I got most everything working and just need some guidance on the last part. Also, the code im attaching works great in xcode but for soem reason comes up short with a couple errors in visual studio. Not sure why this happens either.

Basically I need to take a user input string and loop until the user types quit. While looping I change the string to upper case, output it, change the string to lowercase, outputit it, display it in proper case output it, sort it in ascii form then output it anb finally sort it alphabetically then output it. The program outputs all the transformation on screen at same time.

I have 3 problems, First, my function for the proper case  is displaying the result twice. I think it has something to do with the do loop in the beginning. Secondly, I have no idea how to start the alpha sort and thirdly, why does this work in xcode and not Visual studio. I dont see anything out of the ordinary here. I commented all the code to help organize it.

Thanks in advance for all your help.

//libraries to be used
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
#include <locale>

using namespace std;

//prototypes defined
string proper(string name);
string sort(string name);

int main()
{
    //name variables being used
    string name;
    
    //do loop will run as long as while statement is true
    do
    {
        cout << "Please enter your name (or quit to exit): ";
        
        getline(cin, name);
        
        //changes string to uppercase
       transform(name.begin(), name.end(), name.begin(), ::toupper);
        
        if(name == "QUIT")
        {
            cout << "\nGoodbye!" << endl;
            return EXIT_SUCCESS;
        }
        
        else
        {
            //changes string to upper case
            transform(name.begin(), name.end(), name.begin(), ::toupper);
            cout << "\nYour name in upper case is: " << name << endl;
            
            //changes string to lower case
            transform(name.begin(), name.end(), name.begin(), ::tolower);
            
            //displays string in lower case
            cout << "\nYour name in lower case is: " << name << endl;
            
            //displays string in proper case
            cout << "\nYour name in proper case is: " << proper(name) << endl;
            
            //displays string in ASCII sort
            cout << "\nYour name in ASCII sort is: " << sort(name) << endl;
            
            //displays string in Alpha sort
            cout << "\nYour name in Alpha sort is: " << '\n' << endl;
            
        }
        
    }

    //when name == "QUIT", the 'do' loop will stop
    while (name != "QUIT");

    //returns a successful exit
    return EXIT_SUCCESS;
}

//function to display string in proper case
string proper(string name)
{
    long l = name.length();
    name[0] = toupper(name[0]);
    cout << name[0];
    
    for(int i = 1; i < l; i++)
    {
        if(name[i] != ' ')
        {
            cout << name[i];
            continue;
        }
        
        if(name[i] == ' ')
        {
            cout << ' ';
            
            i++;
            
            name[i] = toupper(name[i]);
        }
        
        cout << name[i];
    }
    
    return name;
    
}
//function to sort in ASCII style
string sort(string name)
{
    sort(name.begin(), name.end());
    
    return name;
}

Open in new window

Avatar of Zoppo
Zoppo
Flag of Germany image

Hi joedfuse,

the first problem simply is you write the result to output twice, first char by char in the proper function itself, next in the calling function you print out the returned name.

I would suggest to remove all output in the proper function, this is a function to modify the string, not to output anything.

For the sorting you can implement your own sort comparsion function which i.e. compares the characters case independant. You can find some good info about using sort with own comparsion functions i.e. here: http://www.codeproject.com/Articles/38381/STL-Sort-Comparison-Function.

About the last: Can you post the first some errors VisualStudio generates?

Hope that helps,

ZOPPO
Avatar of joedfuse

ASKER

Error      1      error C3861: 'transform': identifier not found      

Error      2      error C3861: 'transform': identifier not found      

Error      3      error C3861: 'transform': identifier not found      

Error      4      error C2660: 'sort' : function does not take 2 arguments      

These are all the errors
ok, maybe there's a header missing which for any reason isn't needed (or available) in xcode. Try to add this line after the last include in your code:

    #include <algorithm>

Hope this helps,

ZOPPO
That helped get the errors away.

The link kinda just confused me some more.... there's a lot of information. What would be the steps to sort the string alphabetically?

Thanks
Well, the way to go is to use the STL sort function with a predicate-functor (or maybe lambda) which implements alphabetical sorting for single characters of a string.

I'm not sure but I don't think there's a ready-to-use thing for this in STL since IMO it's not a common use case to sort the single letters of a string. So I think you need to write your own one.

Here you can see a sample which uses a functor object which implements a function to sort the characters using their lowercase values, if these are equal capital letters are placed before small letters:
	string strText1 = "The Quick Brown Fox Jumps Over The Lazy Dog";
	string strText2 = "The Quick Brown Fox Jumps Over The Lazy Dog";

	cout << "'" << strText1 << "'" << endl;

	// This is the ASCII sort as you already have
	sort( strText1.begin(), strText1.end() );

	// Here the predicate functor is implemented
	struct my_pred : public binary_function<char, char, bool>
	{
		bool operator()(const char& l, const char& r) const
		{
			char l1 = tolower( l );
			char r1 = tolower( r );

			if ( l1 == r1 )
			{
				// ensure capital letters are sorted to front
				return l < r;
			}

			return ( l1 < r1 );
		}
	};

	// this sort uses the predicate
	sort( strText2.begin(), strText2.end(), my_pred() );

	cout << "'" << strText1 << "'" << endl;
	cout << "'" << strText2 << "'" << endl;

Open in new window

The output of this code is this:
'The Quick Brown Fox Jumps Over The Lazy Dog'
'        BDFJLOQTTaceeeghhikmnoooprrsuuvwxyz'
'        aBcDeeeFghhiJkLmnOooopQrrsTTuuvwxyz'

Open in new window

Hope that helps,

ZOPPO


PS: I edited the code since there was a little error in it.
Thanks for the code above...

I tried to apply it and it seems to make everything lower case.

Also my proper case is still duplicating

Here is the code

//libraries to be used
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
#include <locale>
#include <algorithm>

using namespace std;
//prototypes defined
string proper(string name);
string sort(string name);

int main()
{
    //name variables being used
    string name;
    //do loop will run as long as while statement is true
    do
    {
        cout << "Please enter your name (or quit to exit): ";
        getline(cin, name);
        //changes string to uppercase
        transform(name.begin(), name.end(), name.begin(), ::toupper);
        
        if(name == "QUIT")
        {
            cout << "\nGoodbye!" << endl;
            return EXIT_SUCCESS;
        }
        
        else
        {
            //changes string to upper case
            transform(name.begin(), name.end(), name.begin(), ::toupper);
            cout << "\nYour name in upper case is: " << name << endl;
            //changes string to lower case
            transform(name.begin(), name.end(), name.begin(), ::tolower);
            //displays string in lower case
            cout << "\nYour name in lower case is: " << name << endl;
            //displays string in proper case
            cout << "\nYour name in proper case is: " << proper(name) << endl;
            //displays string in ASCII sort
            cout << "\nYour name in ASCII sort is: " << sort(name) << endl;
            
            // Here the predicate functor is implemented
            struct my_pred : public binary_function<char, char, bool>
            {
                bool operator()(const char& l, const char& r) const
                {
                    char l1 = tolower( l );
                    char r2 = tolower( r );
                    
                    if ( l1 == r2 )
                    {
                        // ensure capital letters are sorted to front
                        return l < r;
                    }
                    
                    return ( tolower( l ) < tolower( r ) );
                }
            };
            
            // this sort uses the predicate
            sort( name.begin(), name.end(), my_pred() );

            
            
            //displays string in Alpha sort
            cout << "\nYour name in Alpha sort is: " << name << '\n' << endl;
            
            
        }
        
    }
    //when name == "QUIT", the 'do' loop will stop
    while (name != "QUIT");
    //returns a successful exit
    return EXIT_SUCCESS;
}

//function to display string in proper case
string proper(string name)
{
    long l = name.length();
    name[0] = toupper(name[0]);
    
    for(int i = 1; i < l; i++)
    {
        if(name[i] != ' ')
        {
            cout << name[i];
            continue;
        }
        
        if(name[i] == ' ')
        {
            cout << ' ';
            
            i++;
            
            name[i] = toupper(name[i]);
        }
        
        cout << name[i];
    }
    
    return name;
    
}


//function to sort in ASCII style
string sort(string name)
{
    sort(name.begin(), name.end());
    
    return name;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great help.