vendredi 5 mars 2021

error: ‘else’ without a previous ‘if’ but I think I have use a correct syntax

I'm new to C language and have been staring at my (probably abysmal) code for a while and can't figure out what's off about it.

Can anyone check my mistake :(
On compiler in said that error: ‘else’ without a previous ‘if’ else //[HIGHLIGHTED] ^~~~ , I have highlighted in comment [HIGHLIGHTED] the error lines to help you searching the line

#include <stdio.h>
#include <string.h>

void main()
{
    int option;
    option  = 0;

  while (option != 10)//For option purpose
  {
    char blood_type[5],next;// blood type purpose
    char name[20];//name purpose
    int count = 1;//turn on loop(True)
    float discount, amountToBePaid, bill;
    int amount;
    int trial = 0, PIN, age;
    float mass, generate;

    printf("\n\n\t\t\t\tNational Blood Center x Tenaga Nasional Berhad Campaign");
    printf("\n\n--------------------------------------------------------------------------------------------------------------------------");
    printf("\n\t\t\t\tThank you for joining our campaign!");
    printf("\n\t\t\t\tIf you donate :");
    printf("\n\t\t\t\t ____________________________________ ");
    printf("\n\t\t\t\t|     Volume      |  Discount given  |");
    printf("\n\t\t\t\t ____________________________________");
    printf("\n\t\t\t\t| 150 ml - 300 ml |        16        |");
    printf("\n\t\t\t\t| 301 ml - 400 ml |        20        |");
    printf("\n\t\t\t\t| 401 ml - higher |        25        |");
    printf("\n\t\t\t\t ____________________________________");
    printf("\n\n ! All contributor are advise to NOT donate your blood for over 470ml");
    printf("\n\nDonator Condition:");
    printf("\n1. Aged between 18 and 60 years old (for those less than 18, written consent from parents or legal guardian is required)");
    printf("\n2. Body weight of at least 45 kg.");
    printf("\n3. In good physical and mental health with no chronic medical illness.");
    printf("\n4. Not on long term medications and has not been intoxicated by alcohol within 24 hours prior to donation.");
    
    printf("\n\nDo you wish to continue?\n"); // this part ask user if they want to continue or not
    printf("1.Yes\n");
    printf("2.Cancel");

    printf("\n\nEnter one of the above : "); //option, user are ask whether he/she wants to continue or not
    scanf("%d", &option);
    
    switch (option)// if the user wants to continue
    {

    case 1:
    printf("Enter age:"); // security measurement, the user is ask to enter the his/her age/mass for confirmed that he/she fulfill all of the recuirment
    scanf("%d", &age);
    printf("Enter mass [in two decimal place]:");
    scanf("%f", &mass);
    
     if( age >=18 && age <=60)
     if(mass > 60.00)
     {
     generate = age*mass; //a generated security code that derived from multiplication of age & mass (for increase the security measurement)
     printf("Your code is %.2f", generate); //display generated code
     }
    else 
     {
        printf("You may not fulfill all the conditions, try again later."); // if the user not fulfill all the conditions, the program will end
        return ;
     }

do
{
    printf("\nInsert the generate code:");
    scanf("%d", &PIN);
    trial++;
}while (PIN != generate && trial != 3);

if (PIN == generate) //[HIGHLIGHTED]
 
 
    printf("\nEnter your name: ");// next step to start  the program
    scanf("%s", name);
    printf("\nPlease Enter Your Blood Type: ");
    scanf("%s", blood_type);
    printf("Please enter amount of your total blood donation (in ml) :");
    scanf("%d", &amount);
    printf("Enter your current electricity bill: Rm");
    scanf("%f", &bill);
    printf("\n");
    
    printf("loading");//just a loading (no animation)
    
    int value = 5;
    
    while (value > 0)
    {
        
        printf(".");
        value--;
        
    }
    
    
   while (count == 1 )
   {
       if ((strcmp(blood_type,"A")==0)||(strcmp(blood_type,"a")==0))
       {
            printf("\nYour Blood Type is %s", blood_type);
            count = 0 ; //turn off loop (False)
       }
       else if((strcmp(blood_type,"B")==0)||(strcmp(blood_type,"b")==0))
       {
            printf("\nYour Blood Type is %s", blood_type);
            count = 0 ; //turn off loop (False)
       }
       else if((strcmp(blood_type,"AB")==0)||(strcmp(blood_type,"ab")==0))
       {
            printf("\nYour Blood Type is %s", blood_type);
            count = 0 ; //turn off loop (False)
       }
       else if((strcmp(blood_type,"o")==0)||(strcmp(blood_type,"O")==0))
       {
            printf("\nYour Blood Type is %s", blood_type);
            count = 0 ; //turn off loop (False)
       }
    else
       {
            printf("\n-------------\n");
            printf("Invalid input\n");
            printf("-------------\n");
            count = 0;//turn off loop (False)
            main();
           
        if(amount<150)
    {
            printf("\nYou have to paid: Rm%0.2f", bill);
            printf("\nDonate for over 150 ml to enjoy a discount cut!");
    }
    else
    {
        if(amount>=150 && amount<=300)
        {
        
            discount = (bill*16)/100;
            amountToBePaid = bill-discount;
            printf("\nAfter applying the discount, you have to paid: Rm%0.2f", amountToBePaid);
            
        }
        else if(amount>300 && amount<=400)
        {
           
            discount = (bill*20)/100;
            amountToBePaid = bill-discount;
            printf("\nAfter applying the discount, you have to paid: Rm%0.2f", amountToBePaid);
            
        }
        else if(amount>400 && amount<600)
        {
          
            discount = (bill*25)/100;
            amountToBePaid = bill-discount;
            printf("\nAfter applying the discount, you have to paid: Rm%0.2f", amountToBePaid);
            
        }
        printf("\nThank you %s for your contribution!", name);
     break; 


else  //[HIGHLIGHTED]
printf("\nSorry, Maximum attempt has been exceded. Please refer officer on duty." );



case 2:
     return ;

     
    default:
        printf("Invalid input\n");
        
    }
  }
}
}
}
}

Aucun commentaire:

Enregistrer un commentaire