Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
int main(int argc, char *argv[]) {
int hacim,yas;
char ad[20]; //Kullanıcı adı
printf("Kullanici adinizi girin: \n");
scanf("%s", ad);
printf("Sifrenizi girin: \n");
char sifre[20]; // Şifre
scanf("%s", sifre);
if (strcmp(ad,"ahmet")==0 && strcmp(sifre,"1234")==0){
printf("Giris basarili. Hosgeldiniz\n\n");
}
else
{
printf("Hatali giris yaptiniz \n");
}
printf("Aracin motor hacmini giriniz:");
scanf("%d",&hacim);
printf("\nAracin model yilini giriniz:");
scanf("%d",&yas);
if(1<=yas<=3 && 0<=hacim<=1300){
printf("\nOdemeniz gereken tutar 646TL");
}
else if(1<=yas<=3 && 1301<=hacim<=1600){
printf("Odemeniz gereken tutar 1035TL");
}
else if(4<=yas<=6 && 0<=hacim<=1300){
printf("Odemeniz gereken tutar 450TL");
}
else if(4<=yas<=6 && 1300<=hacim<=1600){
printf("Odemeniz gereken tutar 776TL");
}
else{
printf("yanlis deger");
}
return 0;
}
I always get 646 even with different values. Why is this happening?
1<=yas<=3
In C this expression is always true. Why? It can be written as (1<=yas)<=3
1<=yas can be 0 or 1. Both of those values are smaller than 3.
You need to use logical expression to have more complex number comparisons:
if(yas >= 1 && yas <= 3)
You need to change all simialt expressions in your program.
0<=hacim<=1300
This expression performs the first relational operation because of the precedence of the operator,it's obviously going to be 1.And then you compare 1 to 1300, and obviously you still get 1.So as long as hacim is greater than 0, it's going to be 1. That's what went wrong.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 23 days ago.
Improve this question
void Payment(){
do{
printf("\nPLEASE ENTER YOUR PREFERED PAYMENT METHOD (ONLINE/CARD):");
scanf("%s", &PaymentMethod);
if(strcmp(PaymentMethod, "CARD")){
Option = 1;
printf("\nYOU HAVE SELECTED CARD PAYMENT METHOD");
}
else if(strcmp(PaymentMethod, "ONLINE")){
Option = 1;
printf("\nYOU HAVE SELECTED ONLINE PAYMENT METHOD");
printf("\n|DRUM E-BOOK ACCOUNT DETAILS : 6734-343-8621 (GOODBANK BERHAD)");
printf("\nYOU HAVE SELECTED CARD PAYMENT METHOD");
}
else{
printf("\nYOU HAVE ENTERED WRONG PAYMENT METHOD! PLEASE TRY AGAIN");
Option=0;
}
}while(Option == 0);
printf("\nPress any number to continue:");
scanf("%d", &Option);
Receipt();
each time i choose CARD , it will show the ONLINE output and show the opposite if i choose ONLINE
if(strcmp(PaymentMethod, "CARD"))
is equivalent to:
if (strcmp(PaymentMethod, "CARD") != 0)
strcmp returns 0 on equality, but the if construct considers zero to be false, so the block is never entered.
Change the condition to:
if (strcmp(PaymentMethod, "CARD") == 0)
Re: "should i change to PaymentMethod == "CARD"?"
Answer: No, you can't compare strings with the equality == operator. That only compares the pointer values.
Also note that you do not need to use the & operator with the %s format specifier in the call to scanf.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I am trying to get the value of "w" but when I do the For loop ,it only gives me 4 results:
Problem:
https://imgur.com/a/F9wPKF7
Code:
#include <stdio.h>
#include <math.h>
int main() {
int x,e,w;
float a = 2.5;
for (x=1; x<6; x++) {
if (x>a) {
w = x*cbrt(x-a);
}
else if (x=a) {
w = x*sin(a*x);
}
else if (x<a) {
w = pow(e,(-a*x))*cos(a*x);
}
printf("%d ",x);
}
return 0;
}
Output:
2 3 4 5
I know in this program i dont search for the value of w but I wanted to see why does it give me only 4 number instead of 5? why is the one missing and ow can I solve it? Thank you
else if (x=a) should beelse if (x==a)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
hey im lost on why this loop doesnt work it all seems right but nothing inside the while works please help the rest of the code is in other files if you need them i can post them
#include <stdio.h>
#include "weatherstation.h"
int dunits = METRIC;
void main(void)
{
char test;
InitializeWeatherStation();
while(1);
{
UpdateWeatherStation();
printf("Enter m for Metric units, b for British units, or q to quit");
scanf_s("%c",&test);
if(test == 'm')
{
dunits = METRIC;
}
else if(test == 'b')
{
dunits = BRITISH;
}
else if(test == 'q')
{
return;
}
DisplayWeatherData(dunits);
}
}
while(1);
{
something;
}
is exactly the same as:
while(1)
{
}
{
something;
}
In other words, what you have there is an infinite loop followed by a scoped block of code (which will never be reached).
Get rid of the semicolon and it should fix that particular problem.
You must not end the while(1) with a semi-colon dude. Because that's a null statement you wrote in there.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I saw a tutorial that explaining how to use the "break" statement in a loop
but every time i'm trying to use it i'm getting a compilation error saying:
"break statement not within loop or switch
break;"
This is my code:
if (finalFirstChar > 6 || finalFirstChar < 1)
{
printf("You didn't entered a proper number! \n");
break;
}
FWIW, if is a condition (selection statement, to be exact), and break works in a loop (iteration statement)/switch-case statement.
As per C11, chapter §6.8.6.3
A break statement shall appear only in or as a switch body or loop body.
In any case, you don't need a break in a if statement body.
OK:
while (cond1) {
if (cond2) break; /* or continue; */
if (cond3) return [something];
}
for (init; cond4; after) {
if (cond5) break; /* or continue; */
if (cond6) return [something];
}
do {
if (cond7) break; /* or continue; */
if (cond8) return [something];
} while (cond9);
if (cond10) return [something];
NOT OK:
if (cond11) { /* not in any while/do/for loop, or case switch */
break; /* or continue; */
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to create a function that will printf a certain string if the user presses any button on the keyboard EXCEPT for capital P, if the user presses P then it will break the loop.
However I don't think I'm using _kbhit and _getch properly. I use the number 80 because that is the ASCII symbol for 80....sorry for any confusion
void activateAlarm(int channelID) {
int key = 0;
while(temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit
||temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit) {
beep(350,100);
if (_kbhit()) {
key = _getch();
if(key == 'P');
break;
}
}
}
No need to explain, the code talks better :
#include <conio.h>
// ...
printf("please press P key to pause \n ");
int key = 0;
while(1)
{
if (_kbhit())
{
key =_getch();
if (key == 'P')
break;
}
}