Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Learners question regarding C++

    I have a beginners question. I have a piece of code here and if i accidentally put a number in for menuItem the program crashes. What would be a simple way to solve this problem?

    cout << "-SEARCH MENU-\n"
    << "(E)mployee ID\n"
    << "(T)elephone extension\n"
    << "(R)oom number\n"
    << "(D)epartment code\n"
    << "Enter menu item: " << endl;

    cin >> menuItem;

    switch (menuItem)
    {
    case 'e':
    case 'E': cout << "Enter employee ID: ";
    cin >> value;
    location = LinearSearch(employee_id, count, value, start);
    break;


    etc....

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Code:
    switch (menuItem)
    {
    case 'e':
    case 'E': ...
    
    default:
        // anything else that is not covered by the above case statements
    }
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thats what i have. and it still gets a runtime error. menuItem is a char variable.

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Then you are doing something else wrong. You can cin into a char variable a number since a number is just another character. Post the complete code of that function so we can see what is going on.
    OracleGuy

  • #5
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh sorry, my bad. The problem was in a completely different location.

    here is the code...

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    int LinearSearch (int array[ ], int size, int value, bool new_search);
    
    int main()
    {
    	int count = 0;
    	int telExt[50], roomNum[50], employee_id[50], deptCode[50], value;
    	char menuItem;
    	int location;
    	char head[40];
    	bool start = true;
    	bool used = false;
    	int x = 1;
    	ifstream fin;
    
    	cout << "Reading database.txt...";
    	fin.open("database.txt");
    
    	if (fin.fail())
    	{
    		cout << "database.txt failed to open\n";
    		_exit(0);
    	}
    
    	cout << "Done!" << endl;
    	fin.getline(head, 40, '\n');
    
    	do
    	{
    		fin >> employee_id[count];
    		fin >> telExt[count];
    		fin >> roomNum[count];
    		fin >> deptCode[count];
    
    		if (!fin.fail())
    		{	count++;
    		}
    	}while (!fin.fail());
    
    	cout << count << " lines counted." << endl << endl;
    
    	cout << "-SEARCH MENU-\n" 
    		<< "(E)mployee ID\n"
    		<< "(T)elephone extension\n"
    		<< "(R)oom number\n"
    		<< "(D)epartment code\n"
    		<< "Enter menu item: " << endl;
    
    	cin >> menuItem;
    
    	switch (menuItem) 
    	{
    		case 'e':
    		case 'E': cout << "Enter employee ID: ";
    				  cin >> value;
    				  location = LinearSearch(employee_id, count, value, start);
    				  break;
    		case 't':
    		case 'T': cout << "Enter telephone extension: ";
    				  cin >> value;
    				  location = LinearSearch(telExt, count, value, start);
    				  break;
    		case 'r':
    		case 'R': cout << "Enter room number: ";
    				  cin >> value;
    				  location = LinearSearch(roomNum, count, value, start);
    				  break;
    		case 'd':
    		case 'D': cout << "Enter department code: ";
    				  cin >> value;
    				  	while ((location = LinearSearch(deptCode, count, value, start)) >= 0)
    					{
    						if (x == 1)
    						{	 
    						cout << head << endl;
    						x++;
    						}
    
    						cout << setw(10) << employee_id[location] << setw(10) << telExt[location] << setw(14) << roomNum[location] << setw(10) << deptCode[location] << endl;
    						start = false;
    						used = true;
    					}
    				  break;
    		default: cout << "Invalid menu item entered.";
    	}
    
    
    
    
    	if (used == true)
    		location++;
    	if (location == -1)
    		cout << "Employee not found." << endl;
    	if (menuItem != 'd' && menuItem != 'D' && location >= 0)
    		cout << head << endl << setw(10) << employee_id[location] << setw(10) << telExt[location] << setw(14) << roomNum[location] << setw(10) << deptCode[location] << endl;
    
    
    
    	return (0);
    }
    
    
    int LinearSearch (int array[ ], int size, int value, bool new_search)
    {
    	static int location;
    	bool found = false;
    
    	if (new_search == true)
    	{
    	for (location = 0; location < size && !found; location++)
    	{
    		if (array[location] == value)
    		{	found = true;
    		}
    	}
    	}
    
    	else
    	{
    		location++;
    	for (location; location < size && !found; location++)
    	{
    		if (array[location] == value)
    		{	found = true;
    		}
    	}
    	}
    
    		if (found == false)
    		{	location = -1;
    		}
    		else
    			location = location - 1;
    
    	return location;
    }
    The problem is actually with location and how its used without being defined if menuItem is used incorrectly.

  • #6
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    gah, nm guys. i figured it out, i just set location = -2


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •