Welcome to the Newschoolers forums! You may read the forums as a guest, however you must be a registered member to post. Register to become a member today!
anyone got an idea why this WHILE loop wont stop when 'q' or 'Q' is entered at etype? It prints out "You have choosen to end this program. The results will be printed below." but it just keeps looping. For some reason the WHILE isnt reconizing the Q char in the etype and terminating the loop.
#include <iostream.h>
#include <ctype.h>
char etype;
float uSum, uSal, cSum, cSal, hSum, hSal;
float averageU, averageC, averageH;
int uCount, cCount, hCount;
main ()
{
uSum = 0;
cSum = 0;
hSum = 0;
cout << "This program is designed to calculate the average salary based on education." << endl;
cout << "When inputing salaries, please type 'U' or 'u' for university salaries," << endl;
cout << "Type 'C' or 'c' for college salaries," << endl;
cout << "Type 'H' or 'h' for highschool salaries," << endl;
cout << "And finally type 'Q' or 'q' to finish." << endl << endl;
while (etype != 'q' || etype != 'Q')
{
cout << "Please enter an education level:" << endl;
cin >> etype;
if (etype == 'u' || etype == 'U')
{
cout << "Please enter the post-university salary: " << endl;
cin >> uSal;
uCount++;
uSum = uSum + uSal;
}
else if (etype == 'c' || etype == 'C')
{
cout << "Please enter the post-college salary: " << endl;
cin >> cSal;
cCount++;
cSum = cSum + cSal;
}
else if (etype == 'h' || etype == 'H')
{
cout << "Please enter the post-highschool salary: " << endl;
cin >> hSal;
hCount++;
hSum = hSum + hSal;
}
else if (etype == 'q' || etype == 'Q')
{
cout << "You have choosen to end this program. The results will be printed below." << endl;
}
}
averageU = uSum * uCount;
averageC = cSum * cCount;
averageH = hSum * hCount;
cout << "The average post-university salary is $" << averageU << "." << endl;
cout << "The average post-college salary is $" << averageC << "." << endl;
cout << "The average post-highschool salary is $" << averageH << "." << endl;
return (0);
}
back at it...this time I cant fiqure out why it says my void statement infront of my functions is an error:
#include <iostream.h> // Bank Account Balance Program
#include <ctype.h>
char choice = 'P';
float account = '0', deposit, Cvalue;
void float Cheque (float &x)
{
cout << "Please enter the ammount on the cheque: ";
cin >> Cvalue;
x -= Cvalue;
}
void float Deposit (float &x)
{
cout << "Please enter the ammount to deposit: ";
cin >> deposit;
x += deposit;
}
void float Print (float x)
{
cout << x;
}
main()
{
while (choice != 'Q' || choice != 'q')
{
cout << "Please enter the type of transaction you would like to make:" << endl;
cout << "'C' or 'c' for chequing," << endl;
cout << "'D' or 'd' for a deposit," << endl;
cout << "'P' or 'p' for a printout of your balence," << endl;
cout << "'Q' or 'q' to quit." << endl;
cin >> choice;
if (choice == 'C' || choice == 'c')
{
Cheque (account);
}
else if (choice == 'D' || choice == 'd')
{
Deposit (account);
}
else if (choice == 'P' || choice == 'p')
{
Print (account);
}
else if (choice == 'Q' || choice == 'q')
{
break;
}
}
return 0;
}
so I did the switch statement but now it only terminates out of the while loop with a "Q", the small 'q' just does the case 'q' but doesnt terminate the while loop even though i have the toupper read for it
while (toupper(choice != 'Q'))
{
cout << "Please enter the type of transaction you would like to make:" << endl << endl;
cout << "'C' or 'c' for chequing," << endl;
cout << "'D' or 'd' for a deposit," << endl;
cout << "'P' or 'p' for a printout of your balence," << endl;
cout << "'Q' or 'q' to quit." << endl;
cin >> choice;
cout << endl;
switch (toupper(choice))
{
case 'C' : Cheque (account);
break;
case 'D' : Deposit (account);
break;
case 'P' : Print (account);
break;
case 'Q' : cout << "You have chosen to end you banking transactions for this session." << endl;
cout << "Thank you for chosing RVG Automated Banking, Have a great Day!" << endl << endl;
break;
default : cout << "Invalid Choice, please try again." << endl;
break;
}
}
WHY!
lol
frustrating and i know its likely something simple...
this is my stab and checking if an array is sorted
int count=0;
for (i=0; i < NumArr; i++)
{
Test[i]=Array[i];
}
int j, temp;
for (i=0; i < NumArr - 1; ++i)
{
for (j = NumArr - 1; j > i; --j)
{
if (Array[j] < Array[j-1])
{
temp = Array[j-1];
Array[j-1] = Array[j];
Array[j] = temp;
}
}
}
for (i=0; i < NumArr; i++)
{
if (Test[i] != Array[i])
{
count++;
}
}
if (count <= 1)
{
return(0);
}
else
{
return(1);
}
The problem is that the bold section doesnt seem to run. If i type in a non sorted array, it still returns 0 :S
gah....so close
this is my function
int IsSorted(int Array[], int i)
{
int count=0, count2=0;
This is to check if the array is sorted accending
for (i=1; i < NumArr; i++)
{
if ( Array[i+1] < Array[i] )
{
count++;
}
}
This is to check if the array is sorted decending
for (i=NumArr; i > 0; i--)
{
if ( Array[i+1] > Array[i] )
{
if ((i+1)<(i))
{
count2++;
}
}
}
This is to see if either count (accending) or count2 (decending) are more then 1 (mean not sorted)
The array can be sorted accending or decending
if ( count >= 1 && count2 >= 1)
{
cout << "a" << endl;
cout << count << endl;
cout << count2 << endl;
return(0);
}
else
{
cout << "b" << endl;
cout << count << endl;
cout << count2 << endl;
return(1);
}
}
No matter what though, it always returns "b" and 0 which is wack. If the array is 1 2 3 4 it says the count is 1 (should be 0) and the count2 is 0 (should be 4).
ANy ideas? I need to use this code. Anything else and the prof will know its not my code.
Thanks
Using modular programming techniques, design and implement a C program to maintain a database
of hospital employee information. The database will store the following information about each
employee: employee number - 5 digits, name - maximum of 20 characters, age - integer, and
department (‘E’ for Emergency, ‘O’ for Orthopaedics, ‘M’ for Maternity, and ‘G’ for General
Surgery). The hospital employee database is to be implemented as an ARRAY of structures.
Use the following structure definition
struct employ_rec {
long int empnum;
char name[21];
char dept;
int age;
};
Then in your main program, declare your database similar to:
struct employ_rec hospital[50];
int numemps = 0;
The main program is to repeatedly prompt the user for input and then call the appropriate module
to deal with the request (i.e., a menu system). The legal commands in your system are:
‘A’ or ‘a’
(numemps) The routine which handles this command should prompt the user
for the all employee information. The procedure should also only input
- Add one employee to your database and add 1 to the database counteroneemployee (i.e. there should NOT be a loop in the function which allows the user
to enter more than one employee at this time). To input 2 employees requires
that the user use the
type of information input just not the correct set of values (i.e, you will get an
integer entered for employee number however it may be negative). Also be sure
to check that same employee number is not input twice.
void InsertData(struct employ_rec hospital[], int &numemps)
‘P’ or ‘p’
void PrintData(struct employ_rec hospital[], int num)
‘C’ or ‘c’
number of which employee to change, the type of information to change (‘A’ for
Age, or ‘D’ for Department’), and the new information to be inserted. Be sure
to print an error message if the employee does not exist. Also, you can assume
that the proper type of information is given but not a correct value.
void ChangeData(struct employ_rec hospital[], int num)
‘Q’ or ‘q’
A command twice. As always, you can assume the correct- Neatly print the employee information in a table (does not have to be sorted).- Change the employee information. This routine should prompt for the employee- Quit the program.#include <iostream.h>
void InsertData(struct employ_rec hospital[], int &numemps);
void PrintData(struct employ_rec hospital[], int num);
void ChangeData(struct employ_rec hospital[], int num);
struct employ_rec
{
long int empnum;
char name[21];
char dept;
int age;
};
main ()
{
struct employ_rec hospital[50];
int numemps = 0;
int choice = 'a';
cout << "Welcome to the Trent Hospitals employee databank." << endl;
while (choice != 'Q' || choice != 'q')
{
cout << "What operation would you like to perform:" << endl;
cout << "A or a : for adding an employee to the databank." << endl;
cout << "P or p : to print an employees records." << endl;
cout << "C or c : to change an existing employees records." << endl;
cout << "Q or q : to quit." << endl;
cin >> choice;
switch (choice)
{
case 'A': case 'a': InsertData (hospital, numemps);
break;
case 'P': case 'p': PrintData (hospital, numemps);
break;
case 'C': case 'c': ChangeData (hospital, numemps);
break;
case 'Q': case 'q': cout << "You have choosen to quit the program. Goodbye." << endl;
break;
}
}
return (0);
}
void InsertData(struct employ_rec hospital[], int &numemps)
{
int x = numemps;
cout << "Please enter the employee number: " << endl;
cin >> hospital[x].empnum;
while (hospital[x].empnum > 99999 || hospital[x].empnum < 10000)
{
cout << "Invalid employee number. Please try again." << endl;
cout << "Please enter the employee number: " << endl;
cin >> hospital[numemps].empnum;
}
cout << "Please enter the employees name (20 characters max): " << endl;
cin.getline(employ_rec.name, hospital[x]);
cout << "Please enter the employees department: " << endl;
cin.getline(employ_rec.dept, hospital[x]);
while (hospital[x].dept != 'm' || hospital[x].dept != 'M' || hospital[x].dept != 'G' || hospital[x].dept != 'g' || hospital[x].dept != 'O' || hospital[x].dept != 'o' || hospital[x].dept != 'E' || hospital[x].dept != 'e')
{
cout << "Invalid department. Please try again." << endl;
cout << "Please enter the employees department: " << endl;
cin.getline(employ_rec.dept, hospital[x]);
}
cout << "Please enter the employees age: " << endl;
cin >> hospital[x].age;
x++;
}
void PrintData(struct employ_rec hospital[], int num)
{
int number;
cout << "Please enter the employee number of the person you wish to print the information about: ";
cin >> number;
int i=0, found=1;
while (found != 1 && i < hospital[])
{
if (employ_rec.empnum[i] == number)
{
found = 0;
cout << employ_rec.empnum[i] << endl;
cout << employ_rec.name[i] << endl;
cout << employ_rec.dept[i] << endl;
cout << employ_rec.age[i] << endl;
}
}
}
void ChangeData(struct employ_rec hospital[], int numemps)
{
int number, newnum;
char type, newtype;
cout << "Please enter the employee number of the person you wish to change information about: ";
cin >> number;
cout << endl;
cout << "Would you like to change the age ('a') or department ('d')? ";
cin >> type;
cout endl;
int i=0, found=1;
while ((found != 0) && i < employ_rec.empnum[numemps])
{
if (hospital.empnum[i] == number)
{
found = 0;
if (type == 'a' || type == 'A')
{
cout << "Insert new age: ";
cin << hospital.age[i];
}
if (type == 'd' || type == 'D')
{
cout << "Insert new department: ";
cin << hospital.dept[i];
}
}
}
}
SO essientially my problem right now is the one loop and 3 cin.getlines.