Issue with code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Issue with code

  1. #1
    Join Date
    May 2017
    Posts
    175

    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 10:51 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,834

    Re: Issue with code

    [Split into new thread]
    All advice is offered in good faith only. 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/

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,834

    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. 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/

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,834

    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. 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/

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,507

    Re: Issue with code

    Quote Originally Posted by 2kaud View Post
    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.

  6. #6
    Join Date
    May 2017
    Posts
    175

    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 02:40 PM.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,834

    Re: Issue with code

    Quote Originally Posted by david16 View Post
    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. 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/

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  8. #8
    Join Date
    May 2017
    Posts
    175

    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 03:28 PM.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,834

    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. 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/

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  10. #10
    Join Date
    May 2017
    Posts
    175

    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 04:43 PM.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,834

    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 05:07 PM.
    All advice is offered in good faith only. 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/

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  12. #12
    Join Date
    May 2017
    Posts
    175

    Re: Issue with code

    ok thanks you I'll try that on my side then

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,834

    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. 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/

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  14. #14
    Join Date
    May 2017
    Posts
    175

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)