"Name" Program *Solved*

Case = Not Sensitive?

Page 1 of 1

9 Replies - 1362 Views - Last Post: 12 August 2008 - 05:17 PM Rate Topic: -----

#1 Xarver   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 60
  • Joined: 06-August 08

"Name" Program *Solved*

Post icon  Posted 11 August 2008 - 08:00 PM

I'm creating a (very) simple "name" program in C++ just for practice/fun.
But I want to know how to make it not case-sensitive.
So Xarver will be the same as XARVER or xArVeR
or Douchebag would be the same as DoUcHeBaG :D
Here's my code:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    system("TITLE Name");
    cout << "Enter Your Name(0 to Quit): ";
    string name;
    getline(cin, name);
    cin.ignore();
    if(name == "Xarver")
    {
        cout << "Nice Name You Got There. Heh Heh...\n";
        cin.get();
    }else if(name == "N00B" || name == "Noob")
    {
        cout << "Haha, You're a Noob!\n";
        cin.get();
    }else if(name == "Douchebag" || name == "Douche")
    {
        cout << "Hey, Douchebag, Get Me A Soda!\n";
        cin.get();
    }else if(name == "1337")
    {
        cout << "| r0><!!! 101 r0|=1M|=40\n";
        cin.get();
    }else if(name == "Mr. Mustache")
    {
        cout << "Hello, Mr. Mustache!\n";
        cin.get();
    }else if(name == "Waffles Pancakes and French Toast")
    {
        cout << "Teh Breakfast is Here!!!\n";
        cin.get();
    }else {
        cout << "Hello " << name << "\n";
        cin.get();
    }
    cout << "Name Identification: Done\n\n";
    for(int i = 10; i > 0; --i)
    {
        cout << i;
        cin.get();
        if(i == 1)
        {
            cout << "\nBOOM!\n";
            cout << name << " is Dead...";
            cin.get();
        }
    }

    return 0;
}


Any easy way to do this? :)

P.S. A bit of humor in the program :P

~ Xarver

This post has been edited by Xarver: 12 August 2008 - 03:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: "Name" Program *Solved*

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: "Name" Program *Solved*

Posted 11 August 2008 - 08:25 PM

well here is one version:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

bool ignoreCaseEq(const string a, const string b );

int main() {
	string name;
	cout << "Name: ";
	cin >> name;
	if (ignoreCaseEq(name, "Xaver")) {
         cout << "OK" << endl;
    } else {
        cout << "Owch" << endl;
    }
    

    return 0;
}

bool ignoreCaseEq(const string a, const string b ) {
	string tempa = a;
	string tempb = b;
	transform(tempa.begin(), tempa.end(), tempa.begin(), (int(*)(int))toupper);
	transform(tempb.begin(), tempb.end(), tempb.begin(), (int(*)(int))toupper);
	return tempa == tempb;	 
}

This post has been edited by NickDMax: 11 August 2008 - 08:33 PM

Was This Post Helpful? 0
  • +
  • -

#3 jwwicks   User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 162
  • Joined: 31-July 08

Re: "Name" Program *Solved*

Posted 11 August 2008 - 08:25 PM

Hello Xavier,

View PostXarver, on 11 Aug, 2008 - 08:00 PM, said:

I'm creating a (very) simple "name" program in C++ just for practice/fun.
But I want to know how to make it not case-sensitive.
So Xarver will be the same as XARVER or xArVeR
or Douchebag would be the same as DoUcHeBaG :D
Here's my code:
Any easy way to do this? :)

P.S. A bit of humor in the program :P

~ Xarver


A case insensitive compare...

bool same = true;
typdef string::iterator SI;
SI p1 = string1.begin();
SI p2 = string2.begin();

while( (p1!=string1.end() && p2!=string2.end()) && same )
{
   if( toupper(*p1) != touppper(*p2) ){ same = false; } 
   ++p1; ++p2;
}
 



JW
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: "Name" Program *Solved*

Posted 11 August 2008 - 08:27 PM

much more efficient than my version :)
Was This Post Helpful? 0
  • +
  • -

#5 Xarver   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 60
  • Joined: 06-August 08

Re: "Name" Program *Solved*

Posted 12 August 2008 - 07:40 AM

@NickDMax: Code::Blocks throws errors at me D:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    system("TITLE Name");
    cout << "Enter Your Name(0 to Quit): ";
    string name;
    getline(cin, name);
    cin.ignore();
    if(ignoreCaseEq(name, "Xarver"))
    {
        cout << "Nice Name You Got There. Heh Heh...\n";
        cin.get();
    }else if(name == "N00B" || name == "Noob")
    {
        cout << "Haha, You're a Noob!\n";
        cin.get();
    }else if(name == "Douchebag" || name == "Douche")
    {
        cout << "Hey, Douchebag, Get Me A Soda!\n";
        cin.get();
    }else if(name == "1337")
    {
        cout << "| r0><!!! 101 r0|=1M|=40\n";
        cin.get();
    }else if(name == "Mr. Mustache")
    {
        cout << "Hello, Mr. Mustache!\n";
        cin.get();
    }else if(name == "Waffles Pancakes and French Toast")
    {
        cout << "Teh Breakfast is Here!!!\n";
        cin.get();
    }else {
        cout << "Hello " << name << "\n";
        cin.get();
    }
    cout << "Name Identification: Done\n\n";
    for(int i = 10; i > 0; --i)
    {
        cout << i;
        cin.get();
        if(i == 1)
        {
            cout << "\nBOOM!\n";
            cout << name << " is Dead...";
            cin.get();
        }
    }

    return 0;
}
bool ignoreCaseEq( const string a, const string b ) {
    string tempa = a;
    string tempb = b;
    transform(tempa.begin(), tempa.end(), tempa.begin(), (int(*)(int))toupper);
    transform(tempb.begin(), tempb.end(), tempb.begin(), (int(*)(int))toupper);
    return tempa == tempb;
}


D:\CodeBlocks\name_program.cpp||In function `int main()':|
D:\CodeBlocks\name_program.cpp|12|error: `ignoreCaseEq' undeclared (first use this function)|
D:\CodeBlocks\name_program.cpp|12|error: (Each undeclared identifier is reported only once for each function it appears in.)|
D:\CodeBlocks\name_program.cpp||In function `bool ignoreCaseEq(std::string, std::string)':|
D:\CodeBlocks\name_program.cpp|55|error: `bool ignoreCaseEq(std::string, std::string)' used prior to declaration|
||=== Build finished: 3 errors, 0 warnings ===|

------------

@jwwicks:
Your code isn't very easy to read,
so I don't know where the heck I can put it in my code. :blink:

This post has been edited by Xarver: 12 August 2008 - 07:41 AM

Was This Post Helpful? 0
  • +
  • -

#6 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: "Name" Program *Solved*

Posted 12 August 2008 - 02:00 PM

need to add in the includes, need to copy over the function declaration.
Was This Post Helpful? 0
  • +
  • -

#7 Xarver   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 60
  • Joined: 06-August 08

Re: "Name" Program *Solved*

Posted 12 August 2008 - 02:47 PM

View PostNickDMax, on 12 Aug, 2008 - 02:00 PM, said:

need to add in the includes, need to copy over the function declaration.

Mind explaining?
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
    system("TITLE Name");
    cout << "Enter Your Name(0 to Quit): ";
    string name;
    getline(cin, name);
    cin.ignore();
    if(ignoreCaseEq(name, "Xarver"))
    {
        cout << "Nice Name You Got There. Heh Heh...\n";
        cin.get();
    }else if(name == "N00B" || name == "Noob")
    {
        cout << "Haha, You're a Noob!\n";
        cin.get();
    }else if(name == "Douchebag" || name == "Douche")
    {
        cout << "Hey, Douchebag, Get Me A Soda!\n";
        cin.get();
    }else if(name == "1337")
    {
        cout << "| r0><!!! 101 r0|=1M|=40\n";
        cin.get();
    }else if(name == "Mr. Mustache")
    {
        cout << "Hello, Mr. Mustache!\n";
        cin.get();
    }else if(name == "Waffles Pancakes and French Toast")
    {
        cout << "Teh Breakfast is Here!!!\n";
        cin.get();
    }else {
        cout << "Hello " << name << "\n";
        cin.get();
    }
    cout << "Name Identification: Done\n\n";
    for(int i = 10; i > 0; --i)
    {
        cout << i;
        cin.get();
        if(i == 1)
        {
            cout << "\nBOOM!\n";
            cout << name << " is Dead...";
            cin.get();
        }
    }

    return 0;
}
bool ignoreCaseEq( const string a, const string b ) {
    string tempa = a;
    string tempb = b;
    transform(tempa.begin(), tempa.end(), tempa.begin(), (int(*)(int))toupper);
    transform(tempb.begin(), tempb.end(), tempb.begin(), (int(*)(int))toupper);
    return tempa == tempb;
}



I've never used transform() or toupper()
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: "Name" Program *Solved*

Posted 12 August 2008 - 03:16 PM

well to get your program to compile you need to declare the function (functions must be declared or defined before they are used).

add bool ignoreCaseEq(const string a, const string b ); before the main() function.

toupper() is a C function that converts a character to upper case (you could use tolower just as easily).

The transform() algorithm is similar to the for_each() algorithm.

The for_each algorithm is used to run a function on each element of a STL container. My first though was to use for_each, but it would need a functor to work and I thought against it.

The transform() algorithm allows use to store the results of the for_each into another container (or the same container).

So to use the transform() algorithm you need 2 iterators (begin and end) of the source container, 1 iterator for the destination, and a function (or functor).

transform(source.begin(), source.end(), dest.begin(), toupper);

now the fist thing you notice is that the program used (int(*)(int))toupper Well this was because there are two functions named toupper and I needed the one int toupper(int); so I used a function pointer to help the compiler know which function I wanted to use.

The next thing you notice is that my source was the same as my destination, this is because string iterators do not add characters, so I need the size to be the same between the source and destination. Using the copy constructor was just an easy way of making sure the sizes matched. I could have used:
transform(a.begin(), a.end(), tempa.begin(), (int(*)(int))toupper);

but why drag a into this.
transform(tempa.begin(), tempa.end(), tempa.begin(), (int(*)(int))toupper); might as well use that.


This is one way to convert a string to upper case.
Was This Post Helpful? 0
  • +
  • -

#9 Xarver   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 60
  • Joined: 06-August 08

Re: "Name" Program *Solved*

Posted 12 August 2008 - 03:25 PM

Thanks for the explanation!
The Program works great now!

And I totally forgot to declare

Quote

bool ignoreCaseEq(const string a, const string b );

:D

~ Xarver :)
Was This Post Helpful? 0
  • +
  • -

#10 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: "Name" Program *Solved*

Posted 12 August 2008 - 05:17 PM

of course something like this may be more efficient.
bool ignoreCaseEq(string a, string b ) {
    bool retVal = true;
	string::iterator iter1 = a.begin();
	string::iterator iter2 = b.begin();
	while (iter1++ != a.end() && iter2++ != b.end() && retVal) {
		  retVal = toupper(*iter1) == toupper(*iter2);
    }
	return retVal;	 
}



The thing I don't like about this is that the function prototype can not have the "const string a" in it (indicating that the string a will not be changed withing the function).

Perhaps insane or Bench know a way to fix that, but for me the iterators cause an error "cannot convert from 'const char * const' to 'char *'." deep within the STL library. So to fix that I needed to ditch the const specifier in the function signature.

This post has been edited by NickDMax: 12 August 2008 - 05:17 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1