Whats Wrong with my Code?

if-else conditional statement

Page 1 of 1

6 Replies - 409 Views - Last Post: 12 August 2009 - 04:21 AM Rate Topic: -----

#1 Suigetsu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 08-August 09

Whats Wrong with my Code?

Post icon  Posted 11 August 2009 - 06:31 AM

Why is it that even if i type the right "position" that is stated in my "if condition" it won't execute, instead it always execute the else statement
#include<iostream>
using namespace std;
int main ()
{
	char a[10];
	cout<<"\t\tPositions: "<<endl
		<<"\nManager"<<"\t\tEmployee"<<endl<<endl;
	cout<<"What's Your Position: ";
	cin>>a;

	if(a=="Manager"){ //even if i type this right it wont execute
		cout<<"asd"<<endl;
	}
	else if(a=="Employee"){ //same to this
		cout<<"kdkd"<<endl;
	}
	else{ //this is what i always get as a result
		cout<<"fghfghghh"<<endl;
	}

	return main();
} 

This post has been edited by Suigetsu: 11 August 2009 - 06:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Whats Wrong with my Code?

#2 tobix10  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 06-April 09

Re: Whats Wrong with my Code?

Posted 11 August 2009 - 06:52 AM

for char arrays use strcmp from <cstring>
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Whats Wrong with my Code?

Posted 11 August 2009 - 07:03 AM

Try "string a;" instead and see how far you get. Character arrays are poor from in C++.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Whats Wrong with my Code?

Posted 11 August 2009 - 08:39 AM

#include<iostream>
#include <string>

using namespace std;

int main ()
{
	string a;
	cout<<"\t\tPositions: "<<endl
		  <<"\nManager"<<"\t\tEmployee"<<endl<<endl;
	cout<<"What's Your Position: ";
	cin>>a;

	if(a=="Manager")
		cout<<"asd"<<endl;
	else if(a=="Employee") 
		cout<<"kdkd"<<endl;
	else
		cout<<"fghfghghh"<<endl;
	

	cin.get();
	return 0;
} 


Was This Post Helpful? 0
  • +
  • -

#5 Suigetsu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 08-August 09

Re: Whats Wrong with my Code?

Posted 12 August 2009 - 01:42 AM

thanks for all the help now i can go work on my program. BTW <string> is case sensitive. Anyway thanks again...Sorry for the late reply
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,899
  • Joined: 10-May 07

Re: Whats Wrong with my Code?

Posted 12 August 2009 - 01:46 AM

Since it's case sensitive use toupper() or tolower() to assure that all checks are the same :)
Was This Post Helpful? 0
  • +
  • -

#7 Suigetsu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 08-August 09

Re: Whats Wrong with my Code?

Posted 12 August 2009 - 04:21 AM

View Postno2pencil, on 12 Aug, 2009 - 12:46 AM, said:

Since it's case sensitive use toupper() or tolower() to assure that all checks are the same :)

Would you mind giving me code sample of where to put it.Sorry just a beginner in c++. And i already made my Payroll Slip Program and some of the results are wrong and I'm having analyzing my mistake T_T.I already post my code here and hope i get some tips of what my mistake is.
Thanks Again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1