Question

In: Computer Science

The program runs but the math is not correct ( output for amount ) #include <iostream>...

The program runs but the math is not correct ( output for amount )

#include <iostream>

using namespace std;
int main()
{

int hoursParked;
double total;
double Car = 2.50 ;
double Truck = 5.50;
double Bus = 19.00;
double rateC = 1.50;
double rateT = 3.75;
double rateB = 6.75;
char type;


cout << "Please Enter number of hours parked and the type of car (Type: (C)ar or (T)ruck or (B)us): " << endl;
cin >> type >> hoursParked;


switch (type)
{
case 'C':
case 'c':
{
if (hoursParked <= 2.00)
total = 2 * 1.25;
else
total = (( hoursParked - 2.00 )* rateC)+ Car ;
}
case 'T':
case 't':
{
if (hoursParked <= 2.00)
total = Truck;
else
total = ((hoursParked - 2.00) * rateT) + Truck;
break;
}
case 'B':
case 'b':
{
  
if (hoursParked <= 2.00)
total = Bus;
else
total = ((hoursParked - 2.00)*rateB) + Bus;
break;
}
}
cout << "Vehicle type: ";
if(type == 'C')
cout << "Car" << endl;
else if(type == 'T')
cout << "Truck" << endl;
else
cout << "Bus" << endl;
cout << "Time:" << hoursParked << endl;
cout << "Amount Due:$" << total <<endl;

return 0;
}

Solutions

Expert Solution

I made slight changes to your program so that it will perform accurately. You have not given any input conditions, so that I have taken like parking charges are made minimum for 2 hours. The program had compiled and running accordingly

#include <iostream>
using namespace std;
int main()
{
int hoursParked;
double total;
double Car = 2.50 ;
double Truck = 5.50;
double Bus = 19.00;
double rateC = 1.50;
double rateT = 3.75;
double rateB = 6.75;
char type;

cout << "Please Enter number of hours parked and the type of car (Type: (C)ar or (T)ruck or (B)us): " << endl;
cin >> type >> hoursParked;

switch (type)
{
case 'C':
case 'c':
{
if (hoursParked <= 2.00){
total = 2 * 1.25;
}
else{
total = (( hoursParked - 2.00 )* rateC)+ Car ;
}
cout << "Vehicle type: " << "Car"<<endl << "Time:" << hoursParked <<endl <<"Parking Charges $"<<total<<endl;
break;
}
case 'T':
case 't':
{
if (hoursParked <= 2.00)
total = Truck;
else
total = ((hoursParked - 2.00) * rateT) + Truck;

cout << "Vehicle type: " << "Truck "<<endl << "Time:" << hoursParked <<endl <<"Parking Charges $"<<total<<endl;
break;
}
case 'B':
case 'b':
{
if (hoursParked <= 2.00)
total = Bus;
else
total = ((hoursParked - 2.00)*rateB) + Bus;
cout << "Vehicle type: " << "Bus "<<endl << "Time:" << hoursParked <<endl <<"Parking Charges $"<<total<<endl;
break;
}
}
return 0;
}

Outputs:


Related Solutions

What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
C++ The txt file is not showing up in my output #include <iostream> #include <fstream> #include...
C++ The txt file is not showing up in my output #include <iostream> #include <fstream> #include <string> #include "Classroom.h" using namespace std; Classroom myLib; void loadDataset(string fileName) { ifstream myFile; myFile.open(fileName.c_str()); } void continueMessage(string message) { cout << message << endl; cout << "Press Enter to continue.." << endl; cin.get(); } int main() { string fileName = "dataset.txt"; loadDataset(fileName); myLib.print(); continueMessage; string ID = "Janet Newman"; myLib.removeStudent (ID); continueMessage("Janet Newman has been removed."); //--------------------------------------------------------------------------- myLib.print(); continueMessage("All students are listed!"); return...
Are my codes correct? #include <iostream> #include <string> #include <cassert> #include <cctype> #include <cstring> #include <stdio.h>...
Are my codes correct? #include <iostream> #include <string> #include <cassert> #include <cctype> #include <cstring> #include <stdio.h> #include <ctype.h> #include<fstream> int locateMinimum( const std::string array[ ], int n ){    if(n <= 0){    return -1;    }    else{    int minInd = 0;    for(int i = 0; i < n; ++i){    if(array[i] < array[minInd]){    minInd = i;    }    }    return minInd;    } } int countPunctuation( const std::string array[], int n) { int...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha = 'A'; for(int i = 0; i < 13; i++){ for(int j = 0; j < 2; j++){ cout << alpha; alpha++; } } cout << endl; return 0; }
this is cahce memory related c program....and not working or running properly??????? #include <iostream> #include <string>...
this is cahce memory related c program....and not working or running properly??????? #include <iostream> #include <string> #include <vector> #include <iomanip> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; int cash_type, block_size, cash_size,number_of_blocks=0;; int compulsry_misses=0, capcity_misses=0, conflict_misses=0; #define DRAM_SIZE (64*1024*1024) unsigned int m_w = 0xABABAB55; /* must not be zero, nor 0x464fffff */ unsigned int m_z = 0x05080902; /* must not be zero, nor 0x9068ffff */ unsigned int rand_() { m_z = 36969 * (m_z & 65535) + (m_z >>...
modify this program to either simulate a teacher or used car salesperson. #include <iostream> #include <string>...
modify this program to either simulate a teacher or used car salesperson. #include <iostream> #include <string> using namespace std; int Menu(string a, string b, string c) { int choice =-1; cout<<" 1)"<<a<<endl; cout<<" 2)"<<b<<endl; cout<<" 3)"<<c<<endl; cout<<" 4) I don't want to talk to you!!"<<endl; cin>>choice; return choice; } int Money(int x) { int c=-1; cout<<"\n Are you that materialistic? \n"; c = Menu(" No.", " Yes.", " Can we talk about something else?"); if(c == 1|| c ==2 )...
Modify the program to double each number in the vector. #include <iostream> #include <vector> using namespace...
Modify the program to double each number in the vector. #include <iostream> #include <vector> using namespace std; int main() { const int N=8; vector<int> nums(N); // User numbers int i=0; // Loop index cout << "\nEnter " << N << " numbers...\n"; for (i = 0; i < N; ++i) { cout << i+1 << ": "; cin >> nums.at(i); } // Convert negatives to 0 for (i = 0; i < N; ++i) { if (nums.at(i) < 0) {...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I -...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I - Declaring a five by five array /* II - Read data from data.txt and use them to create the matrix in the previous step*/    // III - Count and print the number of even integers in the matrix. /* IV - Calculate and print the sum of all integers in the columns with an even index value. Please note the column index begins...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT