Could you explain why the code bellow never pass the if ?

Hi Experts!

Could you explain why the code bellow never pass the if ?

What is needed for that?

		#include <cmath>
		/*14. Entrar com a sigla do estado de uma pessoa e imprimir uma das mensagens: carioca, paulista, mineiro ou outros estados. */
		using namespace std;
		int main(int argc, char** argv) {
		            char sigla[3];
		            
					cout<<"digite a sigla do seu estado";
		            cin>>sigla;
		            cout<<sigla<<endl;
		            
		            if(sigla=="sp") {
					 cout<< "paulista" << endl;
					}
		            else if (sigla=="rj")
		            {
		              cout<< "carioca"<< endl;
					}
					else if (sigla=="mg")
					{
					    cout<<"mineiro"<< endl;
					}
		            else 
					{
		            cout<< "outros estados"<< endl;
					}
		                        
		            return 0;
		}

Open in new window


Thanks in advance!
Eduardo FuerteDeveloper and AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
"sp" is a ponter to a string literal
sigls is a pointer to statically allocated character arrays
They will be stored in different places, so their addresses will always be different

Did you mean to compare the contents of the character arrays?  Perhaps with strcmp?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Ok, you're right I've changed to string, it's  ok now!
0
peprCommented:
It is probably better to get used to std::string instead of char[]. Then you can compare the way you used in the question.
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Eduardo FuerteDeveloper and AnalystAuthor Commented:
Hello Pepr!

How is coded:


		#include <iostream>
		#include <cmath>
		/*14. Entrar com a sigla do estado de uma pessoa e imprimir uma das mensagens: carioca, paulista, mineiro ou outros estados. */
		using namespace std;
		int main(int argc, char** argv) {
		    
		  string sigla;
						            
		    cout<<"digite a sigla do seu estado";
		    cin>>sigla;
		    cout<<sigla<<endl;
					            
		    if (sigla=="sp")      cout << "paulista";
			else if (sigla=="rj")   cout << "carioca";
			else if (sigla=="mg")   cout << "mineiro";
			else 
			cout << "outros estados";						
	    	return 0;
							
	}

Open in new window

0
peprCommented:
This is almost right. It probably says something about cin not able to work with sigla. That is because you forgot to #include <string>. Try the following:
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int main(int argc, char** argv) {
    string sigla;
                                            
    cout << "digite a sigla do seu estado: " << flush;
    cin >> sigla;
    cout << "You have entered: '" << sigla <<"'" << endl;
                                    
    if      (sigla=="sp")   cout << "paulista";
    else if (sigla=="rj")   cout << "carioca";
    else if (sigla=="mg")   cout << "mineiro";
    else                    cout << "outros estados";
    cout << endl;
    return 0;
}

Open in new window

It looks as follows on my console:
c:\tmp\___C++\EFuerte\Q_28260680\Debug>Q_28260680.exe
digite a sigla do seu estado: rj
You have entered: 'rj'
carioca

Open in new window

0
Eduardo FuerteDeveloper and AnalystAuthor Commented:
But the code I posted already runs ok too!

1
0
peprCommented:
OK. It depends on the compiler, and on the STL implementation. For example, Visual Studio 2013 C++ complains.

The <string> may be probably included by your implementation of <iostream>. But you should be careful when you want to make it portable.
0
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Good to know it...
I'm using Dev++
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.