What's new

Closed C programming beginner

Status
Not open for further replies.
Kinulang ka lang po ng equals. Kung titingnan mo kung saan magfo-fall ang 5.5, makikita mo ’yung mali. Hindi siya pwede sa SOME DAMAGE o sa SERIOUS DAMAGE. Kaya nako-confuse ang entire program.
 
Kinulang ka lang po ng equals. Kung titingnan mo kung saan magfo-fall ang 5.5, makikita mo ’yung mali. Hindi siya pwede sa SOME DAMAGE o sa SERIOUS DAMAGE. Kaya nako-confuse ang entire program.
thanks for the info paps. appreciated much
 
Hindi ba pwede nang ganito yan? bat pinapahaba mo pa?
#include <stdio.h>

int main()
{
float richterno;

printf("ENTER RICHTER NUMBER: ");
scanf("%f",&richterno);

if (richterno < 5)
{
printf("LITTLE OR NO DAMAGE \n");
}
else if (richterno >=5)
{
printf("SOME DAMAGE \n");
}
else if (richterno >=5.5 )
{
printf("SERIOUS DAMAGE \n");
}
else if (richterno >= 6.5)
{
printf("DISASTER \n");
}
else
{
printf("CATASTROPHE \n");
}

return 0;

}
 
Hindi ba pwede nang ganito yan? bat pinapahaba mo pa?
#include <stdio.h>

int main()
{
float richterno;

printf("ENTER RICHTER NUMBER: ");
scanf("%f",&richterno);

if (richterno < 5)
{
printf("LITTLE OR NO DAMAGE \n");
}
else if (richterno >=5)
{
printf("SOME DAMAGE \n");
}
else if (richterno >=5.5 )
{
printf("SERIOUS DAMAGE \n");
}
else if (richterno >= 6.5)
{
printf("DISASTER \n");
}
else
{
printf("CATASTROPHE \n");
}

return 0;

}


Hindi kasi sa expression na "
Code:
else if (richter >= 5)
"

Kasi lalabas na lahat ng value simula sa "5", "5.5", "6", "7", "8" hanggang walang katapusan o infinity ay palaging mag re return as true. Kaya hindi na yan aabot sa mga sumusunod na "else if" statements.
 
Hindi ba pwede nang ganito yan? bat pinapahaba mo pa?
#include <stdio.h>

int main()
{
float richterno;

printf("ENTER RICHTER NUMBER: ");
scanf("%f",&richterno);

if (richterno < 5)
{
printf("LITTLE OR NO DAMAGE \n");
}
else if (richterno >=5)
{
printf("SOME DAMAGE \n");
}
else if (richterno >=5.5 )
{
printf("SERIOUS DAMAGE \n");
}
else if (richterno >= 6.5)
{
printf("DISASTER \n");
}
else
{
printf("CATASTROPHE \n");
}

return 0;

}
mali ang pag code
 
tama naman code ni TS.

try tanggalin mo curly braces

if (richter<5)
//message goes here
else if(richter >= 5 || richter <= 5.5)
//message goes here
else if(richter >= 5.6 || richter <= 6)
//message goes here
else
//message goes here

yan try mo
 
Hi mga paps, I am new at c laguage, pahelp naman po kung anong mali dito sa ginawa koooo, hahahah I can't seem to find where did I mistaken
View attachment 656820

Matapos ko syang i-compile and run, once na nag input ako ng number di nya binabasa yung sumunod na if else statement, hanggang dun lang sya sa "SOME DAMAGE"
pa help naman po jan sa mga experts or my knowledge about c language
Thanks in advance mga paps

Pwede mo rin yan gawin ganito na puro "if" statements lang at walang "else if" statements. Exploiting the order of the statements and nature of the value that is always in an increasing order. For example.

Code:
if (richterno >= 7.5)
    printf("CATASTROPHY \n");
if (richterno >= 6.5 && richterno <= 7.4)
    printf("DISASTER \n");
if (richterno >=5.5 && richterno <= 6.4)
    printf("SERIOUS DAMAGE \n");
if (richterno >=5 && richterno <= 5.4)
    printf("SOME DAMAGE \n");
if (richterno < 5)
    printf("LITTLE OR NO DAMAGE \n");
 
Last edited:
Hindi ba pwede nang ganito yan? bat pinapahaba mo pa?
#include <stdio.h>

int main()
{
float richterno;

printf("ENTER RICHTER NUMBER: ");
scanf("%f",&richterno);

if (richterno < 5)
{
printf("LITTLE OR NO DAMAGE \n");
}
else if (richterno >=5)
{
printf("SOME DAMAGE \n");
}
else if (richterno >=5.5 )
{
printf("SERIOUS DAMAGE \n");
}
else if (richterno >= 6.5)
{
printf("DISASTER \n");
}
else
{
printf("CATASTROPHE \n");
}

return 0;

}

Hanggang Some Damage lang ang magiging output niyan.

Dapat:

#include<stdio.h>
int main()
{float richterno;
printf("ENTER RICHTER NUMBER: ");
scanf("%f",&richterno);
if(richterno<5)
printf("LITTLE OR NO DAMAGE \n");
else if(richterno>=5&&richterno<5.5)
printf("SOME DAMAGE \n");
else if(richterno>=5.5&&richterno<6.5)
printf("SERIOUS DAMAGE \n");
else if(richterno>=6.5&&richterno<7.5)
printf("DISASTER \n");
else
printf("CATASTROPHE \n");
return 0;}
 
Actually, kung walang pre-processing ng input galing sa scanf, the value contains a precision of 6 decimal places (ie, 5.000000) such that it won't match any of your original if-else conditions. This one should work:

Code:
#include <stdio.h>

int main(int argc, char* argv[]) {
    float r;
    printf("ENTER RICHTER NUMBER: ");
    scanf("%f", &r);
    printf("You entered: %f\n", r);

    if (r<5.000000) {
        printf("LITTLE TO NO DAMAGE\n");
    }
    else if (r>=5.000000 && r<5.600000) {
        printf("SOME DAMAGE\n");
    }
    else if (r>=5.600000 && r<=6.500000) {
        printf("SERIOUS DAMAGE\n");
    }
    else if (r>=6.600000 && r<=7.500000) {
        printf("DISASTER\n");
    }
    else {
        printf("CATASTROPHE\n");
    }
}

Pero kung gusto mo lang ng 2-decimal places (which makes a lot of sense), pre-process the input prior to testing.
 
Last edited:
Actually, kung walang pre-processing ng input galing sa scanf, the value contains a precision of 6 decimal places (ie, 5.000000) such that it won't match any of your original if-else conditions. This one should work:

Code:
#include <stdio.h>

int main(int argc, char* argv[]) {
    float r;
    printf("ENTER RICHTER NUMBER: ");
    scanf("%f", &r);
    printf("You entered: %f\n", r);

    if (r<5.000000) {
        printf("LITTLE TO NO DAMAGE\n");
    }
    else if (r>=5.000000 && r<5.600000) {
        printf("SOME DAMAGE\n");
    }
    else if (r>=5.600000 && r<=6.500000) {
        printf("SERIOUS DAMAGE\n");
    }
    else if (r>=6.600000 && r<=7.500000) {
        printf("DISASTER\n");
    }
    else {
        printf("CATASTROPHE\n");
    }
}

Pero kung gusto mo lang ng 2-decimal places (which makes a lot of sense), pre-process the input prior to testing.

Wow! You are actually right. Tested some similar scenarios and I'm actually surprised.

In all these years I never thought I would learn something new and an important thing like this.

Figures out I don't really use much floating point comparisons in my projects. LOL.
 
pano to paps paturo nto? pwede ba to sa windows 7? ano need na software? pra mkpag rpractice?
 
babyjoseph ganito rin ako nung unang sabak ko sa c language nalilito may idea pero nalilito hanggang natutunan ko gumamit ng c language moving to c++ na naman
 
Status
Not open for further replies.

Similar threads

Back
Top