Help the beginner please~

Hi! i am new in programming, i now trying to make a simple calculation for the parking charge, my teacher giving me a question with input, the question that she give me is like this,

"people park car in the area, and each minutes will be charge by 0.50, so the user will put how many hours and minute they park there, and charge will be calculated, then the program will show how much charge that user get, if the charge is >80, user will get discount by 10%."

so i managed to do in the c++, but i stuck at if else statement.

this is the program that i made

................................................................................
#include <iostream>
#include "conio.h"
using namespace std;

void main(){
int hpark;
int mpark;
double feepark;
int totaltime;
int discount;
double disfee;
cout << " How many hour you park here ? ";
cin >> hpark;
cout << endl;
cout << " How many minutes? ";
cin >> mpark;
cout << endl;
totaltime = (hpark * 60) + mpark;
feepark = totaltime * 0.50;
if (feepark > 80);
discount = (feepark / 100) * 10;
disfee = feepark - discount;
cout << disfee;
if else < 80;
cout << feepark;





_getch();
}
................................................................................

can any experts tell me why "if else" is error? because in my VS10 the "if else" are been underlined with red line. your help much appreaciate, be nice to me please because im totally new in c++, and i only learn based on youtube, thanks for help :)
A couple of things wrong.
This line should not end with a semicolon.
if (feepark > 80);

This line I think should simply read else, again without a semicolon.
if else < 80;

Something like this:
1
2
3
4
5
6
7
8
9
10
if (feepark > 80)
{
    discount = (feepark / 100) * 10;
    disfee = feepark - discount;
    cout << disfee;
}
else
{
    cout << feepark;
}

at least, that's the style, I've not checked whether it achieves the required result.
Like Chervil said, the right formats to use if, if else and else are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if ( bool expression)
{
    ...
}

else if ( bool expression )
{
    ...
}
//else if can be used multiple times

else
{
   ...
}
//has to be the last one, cannot have a regular expression 


Keep in mind that there cannot be a statement (expression ending with a ;) between the ()'s following 'if' or 'else if'.
Also, there cannot be any other code between if, else if or else, otherwise the 'chain' is broken, and you have to begin with if again. You cannot begin with 'if else' or 'else'. Only comments and whitespace can be in between.


Also, you don't have to use {}'s after every if, else if or else, if there's only a single statement following it, but it's better to do so (code wrapped in {}'s count as one statement). If you're adding code later, you might forget to add the {}'s and unwanted things happen.
Last edited on
Your code:---(Erroneous)

1
2
if (feepark > 80);
if else < 80;


Hey, you seriously need to consider revising the concept of the if else structure.

The if-condition has to be enclosed by rounded brackets.

Also avoid putting semi colons after the condition, because a semi colon denotes the end of a statement. If You place a semicolon after the condition, it simply assumes that your if-else statement has terminated the what follows is simply sequential code.

Don't lose hope though, simply revise it and understand the importance of details in the code. Understand what each thing means.

Hope this helps.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void main ()
{
    int hour, min, tot_time;
    double charge;
    cout <<"Please enter the number of hours....";
    cin >>hour;
    cout <<"Please enter the number of minutes...";
    cin >>min;

    tot_time = (hour * 60) + min;
    charge = tot_time * 0.50;
    
    if (charge > 80)
    {
         charge = charge * 10/100;
         cout <<"Your total charge is "<<charge;
     }
     else 
     { 
          cout <<"Your total charge is "<<charge;
      }   
     
Aquos, if you like basic mathematics, you can shorten your code a bit.

This works well on my old computer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>            // #include "conio.h" is not needed.
using namespace std;

main()
{
int hpark;
int mpark;
double feepark;  //Only these three variables are actually needed. 

cout <<"How many hours did you park here? ";
cin >> hpark;
cin.get();
cout<<endl;
cout <<"How many minutes? ";
cin >> mpark;
cin.get();
cout<< endl;
cout<<"That is a total of " <<hpark*60 + mpark<<" minutes"<<endl;
feepark = (hpark*60 + mpark)*0.50;

if (feepark >= 80)
{cout << "\nYour parking fee is $"<< feepark*0.9 <<endl;}
else  {cout<<"\nYour parking fee is: $ " <<feepark<<endl;}
cin.get();
return 0;
}


You can see above there is no actual need to calculate the discount itself, as the discounted fee is simply feepark*0.9
Same for total time - you can leave it as hpark*60 + mpark. Less work!

Once you get a handle on the syntax of if and if-else statements, as well explained above, they are pretty useful!
Last edited on
Thanks to those who replying me!! and sorry for late respond, been busy with homeworks, thanks again, this site is so friendly, nice to have community that help newbie like me to learn about the basic, thanks again to all!!
Topic archived. No new replies allowed.