guys pls help. if else statement

here's a sample problem. can anyone pls help?

A program that will accept the following:
1. Plate Number of a car.
2. Parking Level
3. Number of Hours Parked

Compute and display the Parking Fee based on the following:
1. First 2 hours = 30 pesos
2. Succeeding Hours = 20 pesos / hour
3. Parking Level A -> No Discount
4. Parking Level B -> 5% Discount
5. Parking Level C -> 10% Discount
6. Other parking Level -> "Invalid Input"
We don't help you do your homework, why don't you post what you have done so far and we'll help you from there.
Do you wish to know how to use if, else if, and else statements? If so, if statements test if a variable matches a certain value.
Example:


int num;
cout<<"Enter a number.\n";
cin>>num;
if (num="1")
{
cout<<"Your number is 1.\n";
}
else if (num="2")
{
cout<<"Your number is 2.\n";
}
else
{
cout<<"Your number is another number.\n";
}

I hope I helped! Also, remember to never put a semicolon after the parentheses of an if or else if statement!
here's what ive done so far. im kinda new in programming. pls do comment. thanks

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

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


int main()

{
// declarations
std::string plateNum;
int hoursParked;
int parkformula;
char floorLevel;
int totalhours;
//inputs
	cout<<"Welcome to my first parking fee program."<<endl;
	cout<<endl<<endl;
    cout<< "Please Input the plate number of your Vehicle: ";
    cin>> plateNum;
	cout<<endl;
	cout<<"Parking Fee : 30php for the first 2 hours,"<<endl;
	cout<<"and additional 20php per succeeding hour."<<endl;
	cout<<endl;
	cout<<"Please Enter Hours Parked: ";
	cin>> hoursParked;
	cout<<endl<<endl;
	cout<<"Floor level discounts"<<endl;
	cout<<"a=no discount, b=5% discount, c=10% discount"<<endl;
	cout<<endl<<"Please Enter Floor Level: ";
	cin>>floorLevel;
	cout<<endl;
//formula of discount
	totalhours=30+(hoursParked*20-40);
	{
   if (hoursParked <=2)
	   if (floorLevel == 'a')
		{
		   cout<<"Floor Level A Price is 30 php "<<endl;
	   }
	   else if (floorLevel == 'b')
		{
		cout<<"Floor Level B Discounted Price: "<<totalhours-(totalhours*.05)<<"php"<<endl;
	   }

	   else if (floorLevel == 'c')
	   {
		   cout<<"Floor Level C Discounted Price: "<<totalhours-(totalhours*.10)<<"php"<<endl;
	   }
	   else
		   cout<<"invalid input";
	}
	{
	if (hoursParked >=3)
		 if (floorLevel == 'a')
		{
		   cout<<"Floor Level A Price (No Discount): "<<totalhours<<"php"<<endl;
	   }
	   else if (floorLevel == 'b')
		{
		cout<<"Floor Level B Discounted Price: "<<totalhours-(totalhours*.05)<<"php"<<endl;
	   }

	   else if (floorLevel == 'c')
	   {
		   cout<<"Floor Level C Discounted Price: "<<totalhours-(totalhours*.10)<<"php"<<endl;
	   }
	   else
		   cout<<"invalid input";
	}
getch();
return 0;

}


closed account (3qX21hU5)
1
2
3
4
	   else
		   cout<<"invalid input";
	}
	{

Your code is not in the braces here. You dont need the braces if the IF has only 1 statement, but its a good idea to use braces anyways incase you add onto the program later.

It is really hard to determine what braces match to what braces. So I might be wrong but it looks like you dont have any opening braces for your if statements.

So be consistent with your braces. What I mean is this, here are 2 examples where you place your braces in different positions.

1
2
3
4
5
6
7
8
9
else if (floorLevel == 'b')
		{
		cout<<"Floor Level B Discounted Price: "<<totalhours-(totalhours*.05)<<"php"<<endl;
	   }

	   else if (floorLevel == 'c')
	   {
		   cout<<"Floor Level C Discounted Price: "<<totalhours-(totalhours*.10)<<"php"<<endl;
	   }

What ever style you use is up to you just be consistent with whatever style you use.

Just a few pointers
Last edited on
Topic archived. No new replies allowed.