Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My question is how to pull data from the file given (data.txt) word by word to g

ID: 3818618 • Letter: M

Question

My question is how to pull data from the file given (data.txt) word by word to get the desired output? I was thinking "getline" , but that pulls the whole 1st line right?

Once I get the data from the file I am good to go. I know the basics of pulling data from a file normally, just not how to do it word by word and insert each of those words into different parts of the code.

I don't want a full solution, I am trying to learn it, but my textbook doesn't put both of these concepts together, so I am totally lost.

Program description (Phone Bill Calculator) Write a complete C++ program for a mobile phone operator to compute the monthly phone bill. Mobile phone operator provide two types of service: Regular (r) and Premium (p) The company has different bill plan for these two types of customers, see the following: Phone Plan for Premium Service Cost Monthly base payment $59.99 First 500 minutes of DayTime talk $0.00 (free) First 100 minutes of NighTime talk $0.00 (free) Remaining DayTime talk minutes $0.30/per minute Remaining DayTime talk minutes $0.45/per minute Phone Plan for Regular Service Cost Monthly base payment $24.99 $0.00 (free) First 250 minutes of AllTime talk Remaining AllTime talk minutes $0.25/per minute In this assignment, you will need to develop a program that reads customer phone usage information from a text file named data.txt, and produces the phone bill per the criteria given above The format of the data in the file is: service Type accountNum DayTimeMinutes NightTimeMinutes The serviceType field is either p (for Premium service) or r (for Regular service) accountNum is a unique customer identification number

Explanation / Answer

//C++ code
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;


double calculateRegularTotalBill(int totalMinutes)
{
double TotalBill = 24.99;
int extra = 0;
if(totalMinutes > 250)
{
extra = totalMinutes - 250;
TotalBill =TotalBill+ extra * .25;
}
return TotalBill;
}

double calculatePremiumTotalBill (int minutesDay, int minutesNight)
{
double TotalBill=59.99;
int extra = 0;

if(minutesDay > 500)
{
extra = minutesDay - 500;
TotalBill =TotalBill+ extra *0.30;
}

if(minutesNight > 100)
{
extra = minutesNight - 100;
TotalBill =TotalBill+ extra *0.45;
}

return TotalBill;
}

void printResults (string accNumber, char scode, int minutes, double TotalBill, double monthlyBase)
{
cout<<" Account Number: " << accNumber;

if (scode == 'P'|| scode=='p')
{
cout<<" Service Type: Premium" ;
} else
{
cout<<" Service Type: Regular";
}

cout<<" Total Minutes used: " << minutes;
cout <<" Monthly base payment: $" << monthlyBase;
cout <<" Charges on minutes: $" << (TotalBill-monthlyBase);
cout<<" Total Amount Due: $" << TotalBill;
cout << endl;
}

int main()
{

string accNumber = "";
char scode = ' ';
int numMinUsed = 0;
int minutesDay = 0, minutesNight = 0;
double TotalBill = 0.0;
double monthlyBaseRegular = 24.99;
double monthlyBasePremium = 59.99;

  

   ifstream inFile ("1.txt");
   if (inFile.is_open())
   {
   while (true )
   {
       inFile >> scode >> accNumber >> minutesDay >> minutesNight;
       double totalMinutes = minutesDay + minutesNight;
       if(scode == 'R' || scode == 'r')
       {
           TotalBill = calculateRegularTotalBill(totalMinutes);
           printResults(accNumber,scode,totalMinutes, TotalBill, monthlyBaseRegular);
       }
       else if(scode == 'P' || scode == 'p')
       {
           TotalBill = calculatePremiumTotalBill(minutesDay,minutesNight);
           printResults(accNumber,scode,totalMinutes, TotalBill, monthlyBasePremium);
       }

       if(inFile.eof())
           break;
   }
   inFile.close();
   }

   else cout << "Unable to open file";

return 0;

}

/*
data.txt
r 125555 223 674
p 678953 334 34

output:

Account Number:   125555
Service Type:   Regular
Total Minutes used:   897
Monthly base payment: $24.99
Charges on minutes: $161.75
Total Amount Due: $186.74


Account Number:   678953
Service Type:   Premium
Total Minutes used:   368
Monthly base payment: $59.99
Charges on minutes: $0
Total Amount Due: $59.99


*/

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote