mercredi 28 octobre 2015

Processing values in each text file one at a time c++

I'm trying to processes values from two text files.One holds a set of chars and the other holds a set of integers.each in their respective places in the text file will amount to one test case.For example the first value that is read in each text file is 1 test case.The values are then used to be checked for variability then Which will then be used as variables through out my code.then in the end prints out a double. The issue is I do not know how to reach that second iteration of test cases. All my code does is read the first values of each text file and prints out the double i need but for only the first test case. As i was typing this out i thought of maybe deleting the values once they are done being used then rerunning the program by having my main in a for loop? suggestions?

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

//function prototypes
char getPackage();
bool vaildPackage(char);
int getHours();
bool validHours(int);
double calculatePakg_A(int);
double calculatePakg_B(int);
double calculatePakg_C(int);
void showBill(double);
int main()
{
    char Package = getPackage();
    int Hours = getHours();
    double bill;
    switch (Package)
        {
    case 'A':bill = calculatePakg_A(Hours);
        cout << bill << endl;
        break;
    case 'B':bill =  calculatePakg_B(Hours);
        cout << bill << endl;
        break;
    case 'C':bill = calculatePakg_C(Hours);
        cout << bill << endl;
        break;
    case 'a':bill = calculatePakg_A(Hours);
        cout << bill << endl;
        break;
    case 'b':bill = calculatePakg_B(Hours);
        cout << bill << endl;
        break;
    case 'c':bill = calculatePakg_C(Hours);
        cout << bill << endl;
        break;
    default: cout << "you did not enter a valid Service Plan. \n";
        break;
        }

    return 0;
}
char getPackage()
{
    ifstream inputFile;
    char a;
    inputFile.open("Packages.txt");
  do
  {
    inputFile >> a;
  } while (! vaildPackage(a));
  return a;

}

 bool vaildPackage(char a)
{

    return a == 'a' || a == 'A'|| a == 'B' || a == 'b'|| a == 'C' || a ==      'c';
}
int getHours()
{
    ifstream inFile;
    int n;
    inFile.open("Hours.txt");
do
{
  inFile >> n;
} while (! validHours(n));
return n;
}

bool validHours(int n)
{
    return n>=0 && n<= 720;
}

double calculatePakg_A(int hrs)
{
    if(hrs <=50)
   {

       return 15.00;
   }
   else
   {
       return (hrs-50) * 2.00 + 15;
   }
}
double calculatePakg_B(int hrs)
{
    if(hrs <=100)
    {
        return 20.00;
    }
    else
    {
        return (hrs-100) * 1.50 + 20.00;
    }

}
double calculatePakg_C(int hrs)
{
    if(hrs <=150)
    {
       return 25.00;
    }
    else
    {
       return (hrs-150) * 1.00 + 25.00;
    }

}


Package.txt

A a B b C c e c

Hours.txt

50 51 100 101 149 251 750 722

Aucun commentaire:

Enregistrer un commentaire