Member Avatar for jvicta

Can anybody tell me how to fix this...i've tried many ways but it still ends up the same...???

//Ch7AppE03JV.cpp
//displays an employee's name when the employee's ID is entered
//Created/revised by Justin Victa on October 14, 2011

#include <iostream>
#include <string>


using std::cout;
using std::cin;
using std::endl;
using std::string;


int main()
{
	//declare variables
	int ID = 0;
	string Name = "";

	//get ID number
	cout << "Please enter ID number: ";
	cin >> ID;

	while (ID >= 0)
	{	
		if (ID = 1234)
		
		Name = "Sue Nguyen";
	//display employee name
		cout << "Employee name: " << Name << endl;
		break;
	else if // <--error C2181: illegal else without matching if
		(ID = 1345) 
		Name = "Janice Blackfeather";
	//display employee name
		cout << "Employee name: " << Name << endl;
		break;
	else  if// <--error C2181: illegal else without matching if
		(ID = 3456) 
		Name = "Allen Kraus";
	//display employee name
		cout << "Employee name: " << Name << endl;
		break;
	else if // <--error C2181: illegal else without matching if
		(ID = 4567) 
		Name = "Margie ODonnell";
	//display employee name
		cout << "Employee name: " << Name << endl;
		break;
	else // <--error C2181: illegal else without matching if
		Name = "Incorrect ID - Please try again."; 
	//end ifs

		//get next ID
		cout << "Next ID: ";
		cin >> ID;
	} //end while

	
	return 0;
} //end of main function

Recommended Answers

All 3 Replies

ye' need to enclose your if() case in curly braces;

if(something)
{
      //do stuff
}

else if(something)
{
     //do other stuff
}

else
{
     //do something else
}

simple mistake.

commented: ~ ty dude. :) +0
Member Avatar for jvicta

thanks that helped. but now, when i input any ID number, i still get the name "Sue Nguyen." plus i dont get the invalid message.

#include <iostream>
#include <string>


using std::cout;
using std::cin;
using std::endl;
using std::string;


int main()
{
	//declare variables
	int ID = 0;
	string Name = "";

	//get ID number
	cout << "Please enter ID number: ";
	cin >> ID;

	while (ID >= 1234)
	{
		//get next ID
		cout << "Next ID: ";
		cin >> ID;
	
	if (ID = 1234)
	{
		Name = "Sue Nguyen";
	//display employee name
		cout << "Employee name: " << Name << endl;
		cout << endl;
	}
	else if (ID = 1345) 
	{
		Name = "Janice Blackfeather";
	//display employee name
		cout << "Employee name: " << Name << endl;
		cout << endl;
	}
	else if (ID = 3456) 
	{
		Name = "Allen Kraus";
	//display employee name
		cout << "Employee name: " << Name << endl;
		cout << endl;		
	}
	 else if (ID = 4567) 
	 {
		Name = "Margie ODonnell";
	//display employee name
		cout << "Employee name: " << Name << endl;
		cout << endl;
	}
	else Name = "Incorrect ID - Please try again."; 
	//end ifs
		
	} //end while

	
	return 0;
} //end of main function

you need to use the == operator in your if statements not the = operator. == checks if two things are equal and = is assignment.

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.