0
//#include conio.h   
//#include iomanip    
//#include iostream    
//#include string    

using namespace std;

const string Zo;
double Sp;
double Li;
double Ti;

void main()
{
    cout<<setiosflags(ios::fixed);
    cout<<setprecision (2);
    cout<<setw(22)<<"Speeding Ticket"<<endl;
    cout<<"Please Enter Your Speed :";
    cin>>Sp;
    cout<<"Speed Limit:";
    cin>>Li;
    cout<<"IF School Zone Enter (Yes/No):";
    cin>>"Yes"||"No";

    if(Zo=="Yes")
        Ti=30+6*(Sp-Li);
    else  
        Ti=30+3*(Sp-Li);
    if (Sp>=Li+30)
        Ti=Ti+100;

    cout<<"Your Speeding Ticket Is:"<<"$"<<Ti<<endl;
    getch();
}

This wasn't my first trial on the first one it was cin>>Zo but the teacher said find a better way so if the user input is wrong it would know. I am a beginner so I did as simply as I could.

greatwolf
  • 20,287
  • 13
  • 71
  • 105
  • 6
    `void main` isn't legal C++. You also have no reason to make those global variables. – chris Oct 23 '13 at 00:08
  • 1
    Your teacher is dreaming, in my opinion. This application is console-bound, not CPU-bound, so there are no significant efficiency gains to be made by recoding it. I probably wouldn't quite write it that way but I don't see anything 'inefficient' about it. But your teacher may have more experience than me, I only have 42 years. Ask him/her what he/she means. – user207421 Oct 23 '13 at 00:09
  • that what the teacher wants us to use – Frezer Diress Oct 23 '13 at 00:09
  • try writing out your if else if logic on paper and see if you can use some algebra to simplify that – pyCthon Oct 23 '13 at 00:09
  • 4
    well, for one thing, `cin>>"Yes"||"No";` doesn't do what you want it to... – zwol Oct 23 '13 at 00:15
  • the first time i wrote the program it was, cin>>Zo; – Frezer Diress Oct 23 '13 at 00:18
  • Use int's instead of doubles. – jon_shep Oct 23 '13 at 00:19
  • 1
    `const string Zo;` is weird; it is a constant empty string. Drop the `const`. Also make it local to `main()` like all the other variables; only use global variables when absolutely necessary. – Jonathan Leffler Oct 23 '13 at 00:23
  • 1
    If you want to get your own back, give all your variables meaningful names. If they try to give you a reason not to do that, get another teacher. – Tony Hopkinson Oct 23 '13 at 00:27
  • `const double fine = 30.0+(isSchoolZone ? 6 : 3)*(speed-limit)+(speed >= limit + 30 ? 100 : 0);` – user207421 Oct 23 '13 at 01:13
  • 2
    There is no conceivable reason you should be trying to performance optimize code like this. It has substantial correctness issues that need to be addressed first. Are you sure your teacher didn't say 'more effective'? – Phil Miller Oct 23 '13 at 02:44
  • So if the speeding ticket is negative does that mean the police patrol pays you? – greatwolf Oct 23 '13 at 07:44

3 Answers3

1

I suppose your teacher wants better math (which implies better logic our your app), one of possible variants is:

const double additionalTicket = (Sp >= Li+30) ? 100 : 0;
const double schoolZoneMultiplier = (Zo == "Yes") ? 6 : 3; // be sure that you understand when to use strcmp and when ==
Ti = 30 + schoolZoneMultiplier*(Sp-Li) + additionalTicket;

this variant do the same, but you see actual formula, so usually such code is better for understanding, also parts of this code can be executed in parallel by CPU

NOTE: you have problem with cin>>"Yes"||"No"; - this just doesn't make sense

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
0

Using the ternary operator is not better math, just different syntax that can make the code look better (or worse if not used wisely).

This previous post shows that it indeed does not bring any performance improvement.

Ternary operator ?: vs if...else

(Sorry can't put that as a comment yet)

Community
  • 1
  • 1
vincentB
  • 128
  • 1
  • 8
0

@vincentB (sorry I can`t comment directly on their post due to my rep)

The ternary operator can be more efficient than an if-else in terms of branchless code produced by the compiler

http://www.altdevblogaday.com/2012/04/10/cc-low-level-curriculum-part-7-more-conditionals/

Lloyd Crawley
  • 606
  • 3
  • 13