jeudi 27 juillet 2017

Output is either 6.9531e-308 or 0 (noob)

I'm trying to write a program that will give the cost of a phone call based on when it starts/ends and how long it takes. Everything works fine, except for when it tries to actually display the cost of each call and the total cost at the end.

Every cost is listed as $6.9531e-308. Someone with a similar issue resolved their issue by initializing the variable in question to zero but when I do that the program just starts listing every cost as $0.

The way I'm calculating the total doesn't seem to be working either, since when the cost is $6.9531e-308, the total comes out to $7.64841e-307 instead of $4.17186e-307. It's either multiplying one of the costs by 11 or it thinks there's 11 costs that need to be added up.

Here is the code:

#include <fstream>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>

using namespace std;

ifstream fin("call_history.txt"); //read from a file

//int main
int main() {

    string temp;
    double total = 0.0;

    while (getline(fin, temp)) {
        istringstream s(temp);
        string token[3];
        int i = 0;

        while(getline(s, token[i], ' ')) { //split the string by spaces and store into token[0], token[1], etc.
            i++;
        }
        //token[0] stores the day of week
        //token[1] stores the time (i.e "14:32")
        //token[2] stores duration

        istringstream s2(token[1]);
        string time_values[2];
        i = 0;

        while(getline(s2, time_values[i], ':')) { //split token[1] by ':'
            i++;
        }
        //time_values[0] stores hour
        //time_values[1] stores minute

        //rename variables for readibility
        string day = token[0];
        int hr = atoi(time_values[0].c_str()); //atoi to convert string to integer

        int minute = atoi(time_values[1].c_str());
        int duration = atoi(token[2].c_str());
        double cost=0; //accumulator
        int start_time = (hr*60)+minute;
        int end_time = start_time+duration;
        int startzone, endzone;

        if (day == "Mo" || day == "Tu" || day == "We" || day == "Th")
            {
                day == "weekday";
            }
            if (day == "Sa" || day == "Su")
            {
                day == "weekend";
            }
    //assigning each starting time to a zone
            if ((day == "weekday" || day == "Fr") && (start_time >= 480 && start_time < 1080))
            {
                startzone = 1; //call starts on hours
            }
            if ((day == "weekday" || day == "Fr") && (start_time <= 480))
            {
                startzone = 2; //call starts before 8 am
            }
            if ((day == "weekday" || day == "Fr") && (start_time >= 1080))
            {
                startzone = 3; //call starts after 6 pm
            }
            if (day == "weekend")
            {
                startzone = 4; //call starts on the weekend
            }
    //assigning each ending time to a zone
    //this program will handle at most 3 zone changes in one call.

            if (day == "weekday" && (end_time >= 480 && end_time <= 1080))
            {
                endzone = 1; //call ends on-hours the same day
            }
            if (day == "weekday" && (end_time >= 1080 && end_time <= 1920))
            {
                endzone = 2; //call ends after 6 pm on the same day, before 8 am on the next day
            }
            if (day == "weekday" && (end_time > 1920 && end_time <= 2520))
            {
                endzone = 3; //call ends on-hours on the next day
            }
            if (day == "weekday" && (end_time > 2520))
            {
                endzone = 4; //call ends after 6 pm the next day
            }
            if (day == "weekend" && (end_time <= 2880))
            {
                endzone = 5; //call starts and ends on the weekend
            }
            if (day == "Fr" && (end_time >= 1440 && end_time <= 2880))
            {
                endzone = 6; //call goes from weekday to weekend
            }
            if (day == "Su" && (end_time >= 1440 && end_time <= 1920))
            {
                endzone = 7; //call goes from weekend to weekday off-hours
            }

        //Cost calculations
        //CALL STARTS ON HOURS
                if (startzone == 1 && endzone == 1) //call is entirely within on-hours
                {
                    cost = duration*0.4;
                }
                if (startzone == 1 && endzone == 2) //call starts on-hours and ends before 8 am the next day
                {
                    cost = ((1080-start_time)*0.4) + ((end_time-1080)*0.25);
                }
                if (startzone == 1 && endzone == 3) //call starts on-hours and ends on-hours the next day
                {
                    cost = ((1080-start_time)*0.4) + (840*0.25) + ((end_time-1920)*0.4);
                }
                if (startzone == 1 && endzone == 4) //call starts on-hours and ends after 6 pm the next day
                {
                    cost = ((1080-start_time)*0.4) + (840*0.25) + (600*0.4) + ((end_time-2520)*0.25);
                }
                if ((startzone == 1 && endzone == 6)) //call starts on hours friday and ends on the weekend
                {
                    cost = ((1080-start_time)*0.4) + ((360)*0.25) + ((end_time-1440)*0.15);
                }

        //CALL STARTS OFF HOURS
                if (startzone == 2 && endzone == 2) //call starts before 8 am and ends before 8 AM the next day
                {
                    cost = ((480-start_time)*0.25) + (600*0.4) + ((end_time-1440)*0.25);
                }
                if (startzone == 2 && endzone == 3) //call starts before 8 am and ends on-hours the next day
                {
                    cost = ((480-start_time)*0.25) + ((end_time-1920)*0.25);
                }
                if (startzone == 2 && endzone == 6) //call starts before 8 AM friday and ends on the weekend
                {
                    cost = ((480 - start_time)*0.25) + (600*0.4) + (360*0.25) + ((end_time-1440)*0.15);
                }
                if (startzone == 3 && endzone ==6) //call starts after 6 PM friday and ends on the weekend
                {
                    cost = ((1440-start_time)*0.25) + ((end_time-1440)*0.15);
                }
                if ((startzone == 3 && endzone == 2) || (startzone == 2 && end_time <= 480)) //call is entirely within off-hours
                {
                    cost = duration*0.25;
                }
                if ((startzone == 3 && endzone == 3)) //call starts after 6 pm and ends on-hours the next day
                {
                    cost = ((1920-start_time)*0.25) + ((end_time-1920)*0.4);
                }
                if ((startzone == 3 && endzone == 4)) //call starts after 6 pm and ends after 6 pm the next day
                {
                    cost = ((1920-start_time)*0.25) + (600*0.4) + ((end_time-2520)*0.25);
                }

        //CALL STARTS ON WEEKEND
                if (startzone == 4 && endzone == 5) //call is entirely within weekends
                {
                    cost = duration*0.15;
                }
                if (startzone == 4 && endzone == 7) //call starts on sunday and ends before 8 am monday
                {
                    cost = ((1440-start_time)*0.15) + ((end_time-1440)*0.25);
                }

        cout << setw(4)<< endl;
        cout << day << " " << hr << ":" << minute << " " << duration << " $" << cost << "\n";
        total += cost;

    }
    cout << setw(4) << endl;
    cout << "\tTotal $" << total << "\n";

}

Aucun commentaire:

Enregistrer un commentaire