I tried to write the function myself for a switch statement that would take a char and multiply it by a certain amount and then return the total. I'm a little confused on how the object gets passed to the case function. But here's what I got.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int MAX_FACULTY = 500;
ifstream inFile;

struct Employee
{
	string name;
	char rank;
	int hours;
	double rankPay;
	
};


int readData(Employee faculty[]);
double calcOverload(Employee faculty[], int numFaculty);
void print(Employee faculty[], int numFaculty);


int main() 
{
	
   Employee faculty[MAX_FACULTY];
  
   int numFaculty;
   double rankPay;

   numFaculty = readData(faculty);
   rankPay = calcOverload(faculty, numFaculty);
   print(faculty, numFaculty);

}

int readData(Employee faculty[])
{
	
	inFile.open("overload.txt");
	
	int count = 0;
	
	while(inFile.peek() !=EOF)
	{
		inFile >> faculty[count].name >> faculty[count].rank >> faculty[count].hours;
		count++;
		
		inFile.ignore(1);
	}
	inFile.close();
	return count;
}

double calcOverload(Employee faculty[], int numFaculty)
{
	
	char rank;
	
	for (int i=0; i<numFaculty; i++)
    switch(rank)
	{
		case 'F':
		case 'f':
		    faculty.rankPay = faculty[i].hours * 990;
		break;
		
		case 'S':
		case 's':
		   faculty.rankPay = faculty[i].hours * 895;
		break;
		
		case 'A':
		case 'a':
		   faculty.rankPay = faculty[i].hours * 780;
		break;
		
		default:
		   faculty.rankPay = faculty[i].hours * 655;
	}
	return rankPay;
}

void print(Employee faculty[], int numFaculty, double rankPay)
{
	for (int i=0; i<numFaculty; i++)
	cout << faculty[i].name << faculty[i].rank << faculty[i].hours << rankPay[i].hours << endl;
}

1) Your switch function has no value at all for rank
2) You return a nonexistent variable named rankPay

Think it through again. What values do you have available in the function? What values do you need to have for the calculations? If you don't have them in the function, how can you get them there?

I need to two loops in the switch correct? one to read in the information and one to put the output in the struct. And that's where I get confused.

I need to two loops in the switch correct? one to read in the information and one to put the output in the struct.

Why? What's the switch supposed to do?

You aren't thinking this through. You're taking a poorly designed program and trying to shoehorn in fixes that may or may not work. You'll never get it this way.

You need to take your problem statement and sit at a desk (no computer) and start writing down the individual steps you need to do to accomplish the task. Smallest steps possible.

Then order those steps in a logical order.

When -- on paper -- your algorithm (the list) looks like it will do the job, now go to the computer and code it. You can do it the hard way and plagiarize what you currently have, or start over.

Look what Walt is trying to say is:

char rank;

 

	for (int i=0; i<numFaculty; i++)

    switch(rank)

^
this code makes no sense whatsoever... you are creating a new variable "rank" then you are checking its value,

but is has not been assigned any value? the case will never find what you are looking for

We have no idea what you are checking for so we cant tell you what to do with it or how to do it, and to be honest even if we did we aren't going to give you all the answers on a silver platter

but you need to fill a variable before you check it otherwise it is completely useless?


It would seem as though you dont have a clearly defined plan of your program, its flow or variables, and planning it out like Walt suggested will give you a much better idea of what is going on

Edited 4 Years Ago by Eagletalon: n/a

Ok, this is all from a text file. it has a name, rank, and hours. I read those In with the while loop. the rank is a character, so each character has a different dollar amount associated with it. So the switch statement was to loop through the ranks in the struct and depending on the character multiply the hours by the rank amount and then put that number into the rankPay object in the struct.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int MAX_FACULTY = 500;
ifstream inFile;

struct Employee
{
	string name;
	char rank;
	int hours;
	double rankPay; //added this when I added the calcOverload function
	
};


int readData(Employee faculty[]);
void calcOverload(Employee faculty[], int numFaculty);
void print(Employee faculty[], int numFaculty);


int main() 
{
	
   Employee faculty[MAX_FACULTY];
  
   int numFaculty;

   numFaculty = readData(faculty);
   calcOverload(faculty, numFaculty)
   print(faculty, numFaculty);

}

int readData(Employee faculty[])
{
	
	inFile.open("overload.txt");
	
	int count = 0;
	
	while(inFile.peek() !=EOF)
	{
		inFile >> faculty[count].name >> faculty[count].rank >> faculty[count].hours;
		count++;
		
		inFile.ignore(1);
	}
	inFile.close();
	return count;
}

void calcOverload(Employee faculty[], int numFaculty)
{
	
	for (int i=0; i<numFaculty; i++)
	   
    switch(faculty[i].rank)
	{
		case 'F':
		case 'f':
		    faculty.rankPay = faculty[i].hours * 990;
		break;
		
		case 'S':
		case 's':
		   faculty.rankPay = faculty[i].hours * 895;
		break;
		
		case 'A':
		case 'a':
		   faculty.rankPay = faculty[i].hours * 780;
		break;
		
		default:
		   faculty.rankPay = faculty[i].hours * 655;
	}
}

void print(Employee faculty[], int numFaculty)
{
	for (int i=0; i<numFaculty; i++)
	cout << faculty[i].name << faculty[i].rank << faculty[i].hours << faculty[i].rankPay << endl;
}

text file

Shammo F 3
Warner S 4
Moore I 8
Collins A 7
Kintner S 1
Kistler F 2
Wagner I 2
Tressel S 4
Ridenour A 1
Furst X 1
Gilkerson I 1

This question has already been answered. Start a new discussion instead.