...

View Full Version : Learners question regarding C++



XxANONxX
10-22-2007, 09:49 AM
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....

shyam
10-22-2007, 10:55 AM
switch (menuItem)
{
case 'e':
case 'E': ...

default:
// anything else that is not covered by the above case statements
}

XxANONxX
10-22-2007, 06:37 PM
thats what i have. and it still gets a runtime error. menuItem is a char variable.

oracleguy
10-22-2007, 08:26 PM
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.

XxANONxX
10-22-2007, 09:55 PM
oh sorry, my bad. The problem was in a completely different location.

here is the 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.

XxANONxX
10-22-2007, 11:39 PM
gah, nm guys. i figured it out, i just set location = -2



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum