Turbo c++ misplaced else

brianjay0914

Reputable
May 5, 2014
134
1
4,690
I dunno if its allowed to post some programming thread here but

pls help me with this . its kinda frustrating . so many misplaced else. an hour ago working perfectly fine but know hmm.

#include<conio.h>
#include<stdio.h>
#include<iostream.h>

int m1,m2,nd,m,d,y,mm=0,dd=0,yy=0,mmm=0,ddd=0,yyy=0;
//int 01=31,02=28,03=31;
main()
{

clrscr();
printf("Enter Date Borrowed\n");
scanf("%d",&mm);
scanf("%d",&dd);
scanf("%d",&yy);
printf("First Date:%d/%d/%d\n\n\n\n",mm,dd,yy);

printf("Enter Date Returned\n");
scanf("%d",&mmm);
scanf("%d",&ddd);
scanf("%d",&yyy);
printf("Second Date: %d/%d/%d\n",mmm,ddd,yyy);
m1=30-dd;
m2=30-ddd;
nd=m1+m2;
d=ddd-dd;



if (d>=1&&d<=9);
printf("fine is 1 peso");
else if (d>=10&&d<=29)
printf("Fine is 10 peso");
else if (dd==ddd)
(nd>=1&&nd<=9);
printf("Fine is 1 peso");
else if (nd>=10&&nd<=29)
printf("fine is 10 peso");
else
printf("INVALID");

getch();
return 0;
}







and it it does work. they test and print all of it at once . tried puting braces each ot if or else DOESNT WORK
 
Solution
you have a semicolon after the first if statement, then you have a printf() ending in a semi colon. so the first else if isn't related to the first if statement? because the printf isn't tied to the if statement.

C++:
if (d>=1&&d<=9);//you are ending the if here why?
 printf("fine is 1 peso");//then you have a print statement instead of an else if
 else if (d>=10&&d<=29)
 printf("Fine is 10 peso");
 else if (dd==ddd)
 (nd>=1&&nd<=9);
 printf("Fine is 1 peso");
 else if (nd>=10&&nd<=29)
 printf("fine is 10 peso");
 else
 printf("INVALID");

I would think you want
if (d>=1&&d<=9){
 printf("fine is 1 peso");}
 else if (d>=10&&d<=29){
 printf("Fine is 10 peso");}
 else if (dd==ddd)
 (nd>=1&&nd<=9);/*I don't understand what you are trying to...

DeadlyDays

Honorable
Mar 29, 2013
379
0
10,960
you have a semicolon after the first if statement, then you have a printf() ending in a semi colon. so the first else if isn't related to the first if statement? because the printf isn't tied to the if statement.

C++:
if (d>=1&&d<=9);//you are ending the if here why?
 printf("fine is 1 peso");//then you have a print statement instead of an else if
 else if (d>=10&&d<=29)
 printf("Fine is 10 peso");
 else if (dd==ddd)
 (nd>=1&&nd<=9);
 printf("Fine is 1 peso");
 else if (nd>=10&&nd<=29)
 printf("fine is 10 peso");
 else
 printf("INVALID");

I would think you want
if (d>=1&&d<=9){
 printf("fine is 1 peso");}
 else if (d>=10&&d<=29){
 printf("Fine is 10 peso");}
 else if (dd==ddd)
 (nd>=1&&nd<=9);/*I don't understand what you are trying to do here, why is there 2 conditional statements separated by paren, and then you have a semicolon ending it here is the printf is not tied to it.*/
{ printf("Fine is 1 peso");}
 else if (nd>=10&&nd<=29){
 printf("fine is 10 peso");}
 else {
 printf("INVALID");}

but maybe I am wrong, it has been awhile since I've coded anything in c++
 
Solution

brianjay0914

Reputable
May 5, 2014
134
1
4,690




thank you for helping me .

im studying programming and our professor teaching us nothing .
so im kinda alone here.

she give us a problem that needs to be fixed


here it s
"give two dates . "date borrowed " and "date returned " compare two date . if you return the book. in less than 9 days the fine is 1 dollar (in your currency i think).... if you return the book in 10th day up to 30th day the fine is 10 dollar. but if it exceed to 30 the statement will be "MEMBERSHIP CANCELLED" so "
 
@brianjay0914,

Two things:
- TurboC is dead for like twenty years. It is not the best choice to start learning C. If this is what your teacher recommended, get away from this school (unless you need this class just to pass it and will never again do software)
- Before jumping on C, get a little reading / learning about algorithms, and what follows what.
 

DeadlyDays

Honorable
Mar 29, 2013
379
0
10,960
just realize a ; ends a statement. when you do an "if" it expects either 1 statement or {}. If you put a ; before adding a statement or {}, you end the if before it does anything, and the statement afterwards will execute regardless because it is not linked to the if.
an "if" expects either TRUE or FALSE, which are Booleans(also 1 or 0), you had a part that was basically if (BOOLEAN)(BOOLEAN) ; It is possible it might work, but I doubt it was intentional? I don't know turboC, but I know OOP and C++ to a degree, and you want it to read better so you want it to always be if (BOOLEAN) { } for readability.
If you need to have multiple conditional statements include them in one set of paren ( Conditional statement && Conditional statement)
You link conditional statements with &&(AND) ||(OR) typically
if it is &&(AND), both have to be true or it returns false
for ||(OR), if either are true it will return true

your conditional statements look good until the middle where you combine two but don't enclose them in a single paren or put any sort of Boolean operator in between them such as && ||.
the only bad part is you don't seem to grasp ending statements yet with a semicolon so you are exiting your logic too early.