Thread: I can not seem to get this program to work. It compiles but not as it should.

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    7

    I can not seem to get this program to work. It compiles but not as it should.

    [CODE]#include<iostream>
    #include<iomanip>

    using namespace std;

    int main ()

    {

    char ans;
    double speed_limit = 0;
    double driver_speed = 0;
    double ticket = 70.25;
    double fine = 0;
    double miles_over = 0;

    do
    {

    cout<< "Enter the speed limit:";
    cin>> speed_limit;

    cout<< "Enter the driver's speed:";
    cin>> driver_speed;


    miles_over = driver_speed - speed_limit ;

    if (miles_over <= 10){

    fine = (ticket + 0);
    }

    if (miles_over > 10){

    fine = (ticket + 0.10);
    }
    if (miles_over > 15){

    fine = (ticket + 0.20);
    }
    if (miles_over > 20){

    fine = (ticket + 0.30);
    }
    if (miles_over >= 25){

    fine = (ticket + 0.40);
    }
    else
    {
    cout<< "The driver was not speeding"<<endl;
    }

    cout<< "You were driving"<<setw(10)<<fixed<<setprecision(2)<<driver _speed<< "in a" << speed_limit<< "mph zone." <<endl;

    cout<< "Enter Y to continue, anything else to stop:";
    cin>> ans;

    } while ( ans == 'Y' || ans == 'y');


    return 0;
    }

    Code:
    #include<iostream>
    #include<iomanip>
    
    using namespace std;
    
    int main ()
    
    {
    
    char ans;
    double speed_limit = 0;
    double driver_speed = 0;
    double ticket = 70.25;
    double fine = 0;
    double miles_over = 0;
    
    do
    {
    
       cout<< "Enter the speed limit:";
       cin>> speed_limit;
       
       cout<< "Enter the driver's speed:";
       cin>> driver_speed;
       
          
       miles_over =  driver_speed - speed_limit ;
       
    if (miles_over <= 10){
       
       fine = (ticket + 0);
       }
       
    if (miles_over > 10){
       
       fine = (ticket + 0.10);
       }
    if (miles_over > 15){
       
       fine = (ticket + 0.20);
       }
    if (miles_over > 20){
       
       fine = (ticket + 0.30);
       }
    if (miles_over >= 25){
       
       fine = (ticket + 0.40);
       }
    else 
        {
         cout<< "The driver was not speeding"<<endl;  
     }
         
    cout<< "You were driving"<<setw(10)<<fixed<<setprecision(2)<<driver_speed<< "in a" << speed_limit<< "mph zone." <<endl;    
       
    cout<< "Enter Y to continue, anything else to stop:";
       cin>> ans;
     
    } while ( ans == 'Y' || ans == 'y');
     
    
    return 0;
    }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    What's not working specifically?

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    7
    It is not doing any of the calculations and the loop is not working.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Write code that does the calculations first thentry work on the loop.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You really need to indent your code properly:
    Code:
    #include<iostream>
    #include<iomanip>
    
    using namespace std;
    
    int main()
    {
        char ans;
        double speed_limit = 0;
        double driver_speed = 0;
        double ticket = 70.25;
        double fine = 0;
        double miles_over = 0;
    
        do
        {
            cout << "Enter the speed limit:";
            cin >> speed_limit;
    
            cout << "Enter the driver's speed:";
            cin >> driver_speed;
    
            miles_over =  driver_speed - speed_limit;
    
            if (miles_over <= 10)
            {
                fine = (ticket + 0);
            }
    
            if (miles_over > 10)
            {
                fine = (ticket + 0.10);
            }
    
            if (miles_over > 15)
            {
                fine = (ticket + 0.20);
            }
    
            if (miles_over > 20)
            {
                fine = (ticket + 0.30);
            }
    
            if (miles_over >= 25)
            {
                fine = (ticket + 0.40);
            }
            else
            {
                cout << "The driver was not speeding" << endl;
            }
    
            cout << "You were driving" << setw(10) << fixed << setprecision(2) << driver_speed << "in a" << speed_limit << "mph zone." << endl;
    
            cout << "Enter Y to continue, anything else to stop:";
            cin >> ans;
    
        } while (ans == 'Y' || ans == 'y');
    
        return 0;
    }
    As I have indicated by the use of blank lines, the problem is that your if-else chain isn't: you have a bunch of separate if statements.

    Also, instead of printing "The driver was not speeding" in the final else, you probably want to check if fine > 0, and if so, print the fine, otherwise print "The driver was not speeding".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Square root function compiles but doesn't work
    By SupermanC in forum C Programming
    Replies: 1
    Last Post: 12-05-2013, 07:45 PM
  2. program compiles but dont work??
    By youareafever in forum C Programming
    Replies: 11
    Last Post: 12-08-2008, 07:50 AM
  3. Generating Dates! Compiles but does not work...
    By ottomated in forum C Programming
    Replies: 11
    Last Post: 04-22-2008, 03:58 AM
  4. this code COMPILES, but DOESNT WORK
    By Leeman_s in forum C++ Programming
    Replies: 14
    Last Post: 11-01-2002, 06:54 PM
  5. this code compiles, but doesn't work how it should
    By Leeman_s in forum C++ Programming
    Replies: 10
    Last Post: 09-10-2002, 05:31 PM