help with program using switch statement

This is a discussion on help with program using switch statement within the C++ Programming forums, part of the General Programming Boards category; This is the code I have for woring out a phonebill, it isn't outputting the correct results however and I'm ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    help with program using switch statement

    This is the code I have for woring out a phonebill, it isn't outputting the correct results however and I'm not sure what to do next.

    The program involves computing the cost of a long distance call. The cost being dependent on the time of day and the day of the week. The data file uses the header card technique and after the program reads in the header card value, it should use a loop to read in and process that number of phone call records. The program should also calculate and report a total.

    prog6dta.txt
    6
    M 13:30 5
    M 22:15 6
    T 7:30 8
    R 8:45 10
    S 13:30 5
    U 22:15 6

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
    	ifstream in_stream;
    	int count=1;
    	int i=0;
    	char day;
    	int hour=0, min=0, totalTime=0;
    	char col;
    	double cost=0.0, total=0.0;
    	double rate=0.0;
    
    	in_stream.open("A:\\prog6dta.txt");
    
    	in_stream >> count;
    	cout << "Call" << setw(10) << "Cost" << endl;
    
    	if (count > 0)
    	{
    		in_stream >> day >> hour >> col >>  min >> totalTime;
    
    		for(i=0; i<count; i++)
    		{
    
    		switch (day)
    		{
    		case 'M':
    		case 'T':
    		case 'W':
    		case 'R':
    		case 'F':
    			if(hour>=8 && hour <=17)
    				rate = 0.4;
    			else
    				rate = 0.25;
    		default:
    				rate = 0.15;
    		}
    
    		if (totalTime > 0)
    			total = rate * totalTime;
    		else
    			total = 0.0;
    
    		cost+= total;
    
    		cout << fixed << showpoint << setprecision(2);
    		cout << setw(4) << i++ << setw(9) << total << endl;
    		in_stream >> day >> hour >> col >> min >> totalTime;
    		}
    	}
    		else;
    		cout << "total= " << setw(7) << cost << endl;
    
    	in_stream.close();
    	return 0;
    	}
    
    
    Sample Output
    */ 
    Call          Cost
    1              2.00
    2              1.50
    3              2.00
    4              4.00
    5              0.75
    6              0.90
    Total=     11.15
    */

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    You forgot the break;
    Without the break, you fall through into the default case


    case 'M':
    case 'T':
    case 'W':
    case 'R':
    case 'F':
    if(hour>=8 && hour <=17)
    rate = 0.4;
    else
    rate = 0.25;
    break;

  3. #3
    Unregistered
    Guest
    is 8:45 8:45 AM or is it 8 AM and 45 minutes of call time? Makes more sense to me to be part of the time, not length of the call. I suspect the last integer is the length of call placed at indicated time on indicated day with the total time of the six calls placed being 40 minutes. If you agree then you need to redo the variables and file reading protocol as well.

    I would be concerned that using >> to sequentially read in the variables will cause program to fail as using cin >> hour >> col >> min to read in 8:45 will cause >> to try to put all 4 char of input into hour which is an int and therefore will fail when it encounters the : char.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    2
    Thanks for the help, I couldn't see that missing break for the life of me
    Hopefully it will run properly when I get home.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  3. Getting the switch statement to work.
    By mtymightymike in forum C Programming
    Replies: 7
    Last Post: 10-15-2008, 06:32 PM
  4. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 01:12 PM
  5. Replies: 1
    Last Post: 08-31-2004, 04:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21