Output of If-else ladder not functioning

This is a discussion on Output of If-else ladder not functioning within the C++ Programming forums, part of the General Programming Boards category; For some reason the output will not print the any charge. I don't understand why. I know its a stupid ...

  1. #1
    IRLeeb
    Join Date
    Feb 2005
    Location
    Amston, CT
    Posts
    7

    Output of If-else ladder not functioning

    For some reason the output will not print the any charge. I don't understand why. I know its a stupid little error, but I am a stupid little programmer. :^)

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <windows.h>
    #include <math.h>
    
    void input(int &group, double &time);
    void computation(int &group, double &time, double &charge);
    void output(int &group, double &time, double &charge);
    
    int main()
    {
    	int group; // type of vehical array
    	double time; // time in lot variable
    	double charge; // charge variable
    	
    	input(group, time);	
    	
    	computation(group, time, charge);
    	
    	output(group, time, charge);
    	
    	return 0;
    }
    
    // definition of input function
    
    void input(int &group, double &time)
    {
    	cout << "Please enter the type of vehical to be charged." << endl;
    	do
    	{
    		cout << "(Enter 1 for Car, 2 for Truck, or 3 for Senior Citizen)" << endl;
    		cin >> group;
    		
    		if(group != 1 && group != 2 && group != 3)
    		{
    			cout << "Please enter a correct charge group";
    		}
    	}
    	while(group != 1 && group != 2 && group != 3);
    	
    	do
    	{
    		cout << "Please enter the length of time stayed." << endl;
    		cin >> time;
    		
    		if(time <= 0)
    		{
    			cout << "Please enter a positive time";
    		}
    	} while(time <= 0);
    	
    }
    
    // definition of computation function
    
    void computation(int &group, double &time, double &charge)
    {
    	if(group == 1)
    	{
    		if(time > 5)
    		{
    			charge = (time - 5) * .25 + 2; 
    		}
    		else if(time > 1)
    		{
    			charge = (time - 1) * .5; 
    		}
    	}
    	
    	if(group == 2)
    	{
    		if(time > 6)
    		{
    			charge = (time - 6) * .75 + 4;
    		}
    		else if(time > 2)
    		{
    			charge = time - 2;
    		}
    	}
    	else
    	{
    		charge = 0;
    	}
    }
    
    // definition of output function
    
    void output(int &group, double &time, double &charge)
    {
    	cout << endl << "Parking Lot Bill" << endl;
    	if(group == 1)
    	{
    		cout << "Charge Group: "<< "Car" << endl;
    	}
    	else if(group == 2)
    	{
    		cout << "Charge Group: " << "Truck"  << endl;
    	}
    	else if(group == 3)
    	{
    		cout << "Charge Group: " << "Senior Citizen"  << endl;
    	}
    	cout << "Time In Lot : " << time << " Hours" << endl;
    	
    	if(charge = 0)
    	{
    		cout << "Total Charge: " << "No Charge" << endl;
    	}
    	
    	if(charge != 0) 
    	{
    		cout << "Total Charge: $" << charge << endl;
    	}
    	cout << endl << "Thank You, Have A Nice Day" << endl;
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Code:
    if(charge == 0)

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    if(charge != 0)

    Is also redundent. You check, (or will be checking), to see if it is equal to zero, so all you need is an else there.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 02:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 03:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

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