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;
}

Dani AI

Generated

Nice progress. The core issues folks pointed out are: the thing you switch on must be initialized (as and noted), you must write to the specific element in the array (use faculty[i].rankPay, not faculty.rankPay), and your I/O loop should not rely on peek() or an ignore(1) that can skip good data. Also, you do not need “two loops in the switch.” You need one outer loop over employees; inside that loop you decide the rate based on the current employee’s rank.

A clean pattern is to separate “rank -> rate” from the loop. Then your loop simply multiplies rate by hours and stores it.

constexpr double F_RATE = 990.0, S_RATE = 895.0, A_RATE = 780.0, OTHER_RATE = 655.0;

double pay_rate(char r) {
    switch (std::tolower(static_cast<unsigned char>(r))) {
        case 'f': return F_RATE;
        case 's': return S_RATE;
        case 'a': return A_RATE;
        default:  return OTHER_RATE;
    }
}

Use that helper inside your processing pass:

for (int i = 0; i < numFaculty; ++i) {
    faculty[i].rankPay = pay_rate(faculty[i].rank) * faculty[i].hours;
}

Small refinements that help avoid future bugs:

  • Read until extraction fails: while (count < MAX_FACULTY && (in >> e.name >> e.rank >> e.hours)) ++count; (drop the ignore(1)).
  • If you need a grand total, accumulate it in the same loop and return it; otherwise keep calcOverload as void and just fill rankPay.
  • Print with separators so output is readable, e.g., name << ' ' << rank << ' ' << hours << ' ' << rankPay.
  • Compile with warnings on: -Wall -Wextra will catch uninitialized variables like that stray rank.

This keeps the data flow simple: read -> compute -> print.

Recommended Answers

All 8 Replies

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

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

Much better. I assume it's working fine now?

Like a charm!

Great! You should have said so and mark the thread SOLVED...

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.