-
June 30th, 2017, 09:41 AM
#1
Issue with code
I have a tiny prob with this piece of code . I want the program to compute fees for each car separately store it somewhere and then display the table will all result . the problem is that I don't now were to put the charges each time then display all of them together . the prog is displaying on each loop how can I modify that to work as I expected .
Code:
double calculate_charges ( double h ) {
if ( h == 24 ) {
return 10.00;
}
else if ( h <= 3 ) {
return 2.00;
}
else {
double chg = 3 + (( h - 3 ) *0.50);
return chg;
}
}
int main () {
int x; double hours;
do {
cout << "Please enter the number of cars :\n" << endl;
cin >> x;
}while ( x <= 0 && cout <<"Wrong choice!" );
for ( int i = 1 ; i <= x ; ++i ) {
do {
cout << "\nPlease enter the number of hours parked by car " << i << " today :\n" << endl;
cin >> hours;
}while ( hours > 24 && cout << "Wrong choicde! " );
for ( int j = 1 ; j <= i ; j++ )
cout <<"\n"; added this in order to separate them so you can see what the problem try to compute it like this
cout << "Car\t\tHours\t\tCharge\n"
<< "----\t\t------\t\t-------" << endl
<< i <<"\t\t"<< hours << "\t\t" << calculate_charges ( hours ); // this function is out the main .
}
system ( " pause " );
return 0;
}
thanks
Last edited by david16; June 30th, 2017 at 09:51 AM.
-
June 30th, 2017, 11:18 AM
#2
Re: Issue with code
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++17 Compiler: Microsoft VS2017 (15.6.6)
-
June 30th, 2017, 11:25 AM
#3
Re: Issue with code
Code:
double chg = 3 + (( h - 3 ) *0.50);
return chg;
Why not just
Code:
return 3 + (( h - 3 ) * 0.50);
compute fees for each car separately store it somewhere and then display the table will all result .
Use a vector to store the hours for each car. Then once all the data has been entered, iterate the vector to compute the charges and display the info for each car. Have you covered vectors yet?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++17 Compiler: Microsoft VS2017 (15.6.6)
-
June 30th, 2017, 11:37 AM
#4
Re: Issue with code
Have you come across the ternary ? operator yet?
Code:
double calculate_charges ( double h ) {
if ( h == 24 ) {
return 10.00;
}
else if ( h <= 3 ) {
return 2.00;
}
else {
double chg = 3 + (( h - 3 ) *0.50);
return chg;
}
}
could be coded as
Code:
double calculate_charges(double h)
{
return h == 24 ? 10.0 : (h <= 3 ? 2.0 : 3 + ((h - 3) * 0.50));
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++17 Compiler: Microsoft VS2017 (15.6.6)
-
June 30th, 2017, 12:01 PM
#5
Re: Issue with code
 Originally Posted by 2kaud
Have you come across the ternary ? operator yet?
Code:
double calculate_charges ( double h ) {
if ( h == 24 ) {
return 10.00;
}
else if ( h <= 3 ) {
return 2.00;
}
else {
double chg = 3 + (( h - 3 ) *0.50);
return chg;
}
}
could be coded as
Code:
double calculate_charges(double h)
{
return h == 24 ? 10.0 : (h <= 3 ? 2.0 : 3 + ((h - 3) * 0.50));
}
For a beginner, it's probably easier to read and debug if it's broken out the way he did it.
-
June 30th, 2017, 01:21 PM
#6
Re: Issue with code
Actually the program ask user how many car it should compute charges for . then take hours parked by each car and get the final charges which is stored is this function calculate_charges ( hours )
All I need to do is to be able to store display the table all result together . I'm not familiar with ternary operator you mentioned . Is that possible to display all result at once and not the charges of each car separately as above part is doing ?? thanks
Last edited by david16; June 30th, 2017 at 01:40 PM.
-
June 30th, 2017, 02:10 PM
#7
Re: Issue with code
 Originally Posted by david16
Actually the program ask user how many car it should compute charges for . then take hours parked by each car and get the final charges which is stored is this function calculate_charges ( hours )
All I need to do is to be able to store display the table all result together . I'm not familiar with ternary operator you mentioned . Is that possible to display all result at once and not the charges of each car separately as above part is doing ?? thanks
See the reply in post #3 re use of a vector.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++17 Compiler: Microsoft VS2017 (15.6.6)
-
June 30th, 2017, 02:17 PM
#8
Re: Issue with code
Sadly still didn't cover vectors or arrays . is it possible without using these ?? if not, can you tell me how to do it with arrays . I have a bit of infos about it maybe I'll understand it thx
Last edited by david16; June 30th, 2017 at 02:28 PM.
-
June 30th, 2017, 03:15 PM
#9
Re: Issue with code
The issue with arrays is that you have to declare their size at compile time - and with vectors you don't. So using an array you need to specify the maximum size as part of the code. In this case the number of cars can't be greater than this value. So using an array, consider
Code:
#include <iostream>
using namespace std;
double calculate_charges(double h)
{
return h == 24 ? 10.0 : (h <= 3 ? 2.0 : 3 + ((h - 3) * 0.50));
}
int main()
{
const int maxcar = 20;
int x = 0;
do {
cout << "Please enter the number of cars (max " << maxcar << ") :";
cin >> x;
} while ((x <= 0 || x > maxcar) && cout << "Wrong choice!" << endl);
double hours = 0.0;
double carhours[maxcar] = { 0.0 };
for (int i = 0; i < x; ++i) {
do {
cout << "Please enter the number of hours parked by car " << i + 1 << " today :";
cin >> hours;
} while ((hours <= 0 || hours > 24) && cout << "Wrong choice!" << endl);
carhours[i] = hours;
}
cout << "\nCar\t\tHours\t\tCharge";
cout << "\n---\t\t-----\t\t------" << endl;
for (int j = 0; j < x; ++j)
cout << j + 1 << "\t\t" << carhours[j] << "\t\t" << calculate_charges(carhours[j]) << endl;
//system(" pause ");
return 0;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++17 Compiler: Microsoft VS2017 (15.6.6)
-
June 30th, 2017, 03:37 PM
#10
Re: Issue with code
But here the number of car is fixed to 20 right ?? I asked to user to enter the number of car so it should store as many as user want can I just put it with empty brakes double car Hours [] = { 0.0 }; or is that considered to be wrong ??
or maybe I can create an array with a fixed number but instead of 20 I put 200 like this const int maxCar = 200; double [maxCar] = {0.0} which I know is bigger than and do the what the user actually want and then do the rest normally ??
Last edited by david16; June 30th, 2017 at 03:43 PM.
-
June 30th, 2017, 03:46 PM
#11
Re: Issue with code
But here the number of car is fixed to 20 right
No. The maximum number is fixed at 20 - but the user enters the required number not greater than this. If the maximum is required to be more, then 20 can be changed to the required maximum number.
double car Hours [] = { 0.0 };
No. That's not allowed. The number of elements in an array has to be specified at compile time. If you can't specify the number of elements in the array at compile time, then you need to use a vector.
Last edited by 2kaud; June 30th, 2017 at 04:07 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++17 Compiler: Microsoft VS2017 (15.6.6)
-
June 30th, 2017, 03:55 PM
#12
Re: Issue with code
ok thanks you I'll try that on my side then
-
June 30th, 2017, 04:09 PM
#13
Re: Issue with code
Just for info. The code does have a problem. Try entering a non-numeric value for the number of cars.
Have you covered stream errors yet?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++17 Compiler: Microsoft VS2017 (15.6.6)
-
June 30th, 2017, 04:18 PM
#14
Re: Issue with code
no problem everything is working fine . thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
On-Demand Webinars (sponsored)
|