Reputation: 1
im new to c language and i am facing some problems with my code.. I have to make a program that computes the prices for renting cars but the while loop is not functioning properly.The scanf above the while condition is %s and not %c because it wont let me give a characther when it is %c.Thank you in advance.
int main(){
float skms, ekms, tkms, price;
char catcode;
int days;
do{
printf("Selected rental program:\n");
scanf("%c",&catcode);
printf("Enter number of days:\n");
scanf("%d",&days);
printf("Starting kilometers display:\n");
scanf("%f",&skms);
printf("Kilometers display at end of rent:\n");
scanf("%f",&ekms);
tkms=(ekms/10)-(skms/10);
switch(catcode){
case'a':
case'A':
price=ammountDueA(days, tkms);
printf("%.2f\n",price);
break;
case'b':
case'B':
price=ammountDueB(days, tkms);
printf("%.2f\n",price);
break;
case'c':
case'C':
price=ammountDueC(days, tkms);
printf("%.2f\n",price);
}
printf("Select rental program.('Q' or 'q' to exit)\n");
scanf("%s",&catcode);
}while(catcode==!'Q'&&catcode==!'q');
printf("%c",catcode);
return 0;
}
Upvotes: 0
Views: 81
Reputation: 8576
There are two problems in your code:
First of all, the specifiers in scanf
...
scanf("%s", &catcode);
This won't work. The "%s"
specifier expects its corresponding argument to be a char*
to a buffer of unspecified length (i.e: this has the same problem as gets()
). You probably meant "%c"
, whose corresponding argument shall point to a single character. Now, your format specifier is not recommended to be "%c"
, but " %c", because that extra space indicates
scanf()to ignore all whitespace it can before
"%c"`. So, that line becomes...
scanf(" %c", &catcode);
Now, you're right, there's a problem in your while
loop:
while(catcode==!'Q'&&catcode==!'q');
Let's "expand" this with whitespace, to make what you (apparently) intended more obvious (that's reason #1 to use spaces between operators!)...
while(catcode ==! 'Q' && catcode ==! 'q');
For you it appears okay. Not for me. The C language does not define any operator ==!
, rather, it defines operator ==
(that compares for equality), and operator !
(that negates its single operand). The compiler would have understood that line as...
while(catcode == !'Q' && catcode == !'q');
Now, what does that mean? If the operand is non-zero, the !
operator returns zero (in this case, the null character, '\0'
). Otherwise, it returns a non-negative value (usually 1
(does the standard mandates this?)). Because 'Q'
is not '\0'
, and same for 'q'
, that means that your loop is effectively...
while(catcode == '\0' && catcode == '\0'); // Think of '\0' as 0
Absolute non-sense expressed in code! The solution is to simply use operator !=
(compares for inequality)...
while(catcode != 'Q' && catcode != 'q');
I hope this has led some light on you!
Upvotes: 1
Reputation: 212564
By "not working", I suppose you mean that it never enters a second iteration, but that is exactly what it is supposed to do. catcode==!'Q'&&catcode==!'q'
is true if catcode
is equal to !'Q'
and equal to !'q'
. 'Q'
is some non-zero integer (depends on your system, but is probably 81), and !'Q'
is zero. Similarly, !'q'
is zero. catcode
isn't zero, so the loop terminates.
Upvotes: 1