I have an assignment to program a simple program that names each member in my family and how they are related to me. The program compiles and all, but as soon as i type in any name, this is what shows up:
Run Command: line 1: 3805 Segmentation fault: 11 ./"$2"

my code is as follows:

#include <iostream>
#include <string>

using namespace std;
int main() 
{
	
char genderType;
char* name;
cout<<"Enter M if you are a male. Enter F if you are a female: ";
cin>>genderType;
	
if (genderType == 'm' || genderType=='M')
	{
		cout<<"You are a male."<<endl;
		cout<<"Please type in your first name in all lowercase letters: ";
		cin>>name;
			
			if (name=="hussein")
				cout<<"You are my eldest brother! ";
			else if (name=="ahmed")
				cout<<"You are my older brother! ";
			else (name =="kamal");
				cout<<"You are my father! ";
	}
		
	else if (genderType=='f' || genderType=='F')
	{
		
		cout<<"You are a female."<<endl;
		cout<<"Please type in your first name in all lowercase letters: ";
		cin>>name;
			
		if (name=="hinda")
			cout<<"You are my eldest sister! ";
		else if (name=="iman")
			cout<<"You are my older sister! ";
		else (name=="ikhlas");
			cout<<"You are my mother! ";
	}
		
		
else 
	{
		cout<<"Sorry, you can only be a male or female! ";
	}
	
	
	
	
	
}

Recommended Answers

All 6 Replies

name is a pointer to a char , but you never allocate any memory for it. You probably want to do something like

char name[ MAX_NAME_LENGTH ];

Also, in future, please post code using code tags and take the time to format the code correctly in your posts :)

#include <iostream>
#include <string>

using namespace std;
int main() 
{
	
	char genderType;
	char name[12];
	cout<<"Enter M if you are a male. Enter F if you are a female: ";
	cin>>genderType;
	
	if (genderType == 'm' || genderType=='M')
		{
			cout<<"You are a male."<<endl;
			cout<<"Please type in your first name in all lowercase letters: ";
			cin>>name;
			
				if (name=="hussein")
				cout<<"You are my eldest brother! ";
				
				else if (name=="ahmed")
				cout<<"You are my older brother! ";
				
				else if (name=="kamal")
				cout<<"You are my father! ";
				
				else 
				cout<<"You are not a male, silly goose";
				
		}
		
	else if (genderType=='f' || genderType=='F')
		{
		
			cout<<"You are a female."<<endl;
			cout<<"Please type in your first name in all lowercase letters: ";
			cin>>name;
			
			if (name=="hinda")
			cout<<"You are my eldest sister! ";
			else if (name=="iman")
			cout<<"You are my older sister! ";
			else if (name=="ikhlas");
			cout<<"You are my mother! ";
		}
		
		
	else 
		{
			cout<<"Sorry, you can only be a male or female.";
		}
	
	
	
	
	
}

my code runs good and compiles except i have one slight problem. If you are any male, it automatically prints :you are my father. And if you are any female: it automatically prints:You are my mother.

i mean it prints you are not a male silly goose for any male

remove the semicolon ; on line 45
and write an else statement inside the

else if (genderType=='f' || genderType=='F')

statement after the last else if statement inside it if you want a default output if the name's is not equal either the if and else if statements after it

i mean it prints you are not a male silly goose for any male

well the else statement is the default if neither the name is not equal to any of the names given in the if and else if statements so it prints what it is suppose to print

The == operator on char arrays doesn't do what you think it does. You have to use strcmp from #include <cstring> to do the kind of comparison that you're trying to do. Or, you could use std::string instead, which does have an == operator that will perform the comparison that you're looking for.

Thanks everyone for everything!!! My program compiles and runs like butter now!!! I am new to this community, but i am glad to be a member now!!! Once again, i really appreciate all of the help and DaniWeb will be the first place i go next time i have questions!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.