-
1 Attachment(s)
Not reading characters
Hello everyone. I am making a project for my C class and i cannot find where exactly the flaw is. I have made printf to insert numbers and it works perfectly. After that I have made another printf to insert letters but that's where the problem begins. I insert the cases I told the program to read but it does not accept them. It doesnt even show a message. I am kind of a beginner at C so it could be something just ridiculously easy the solution to the problem.Attachment 16394 This is the program I have made so far. Thank you in advance for any help i do receive.
-
First off, indent your code.
Code:
#include<stdio.h>
#include<math.h>
int inrange(int, int, int, int);
int cubesum(int, int, int, int);
int avsqrsum(int, int, int, int);
int squaresum(int, int, int, int);
int inrage(int, int, int, int);
int main()
{
int a, b, c, d;
char chr;
printf("Eisagete 4 arithmous\n");
scanf("%d%d%d%d", &a, &b, &c, &d);
printf("Tora dialekste ena gramma apo ta S, C, A\n");
scanf("%c", &chr);
while (chr != 'W') {
if (inrange(a, b, c, d) == 1) {
switch (chr) {
case 'S':
printf("SUM OF THE SQUARES %d\n", squaresum(a, b, c, d));
break;
case 'C':
printf("SUM OF THE CUBES %d\n", cubesum(a, b, c, d));
break;
case 'A':
printf("AVERAGE OF THE SQUARES %d\n", avsqrsum(a, b, c, d));
break;
} /*kleinei to switch */
} /* kleinei to if */
else
printf("INVALID DATA\n");
} /* kleinei to while */
return 0;
}
int inrange(int a, int b, int c, int d)
{
int flag = 1;
if ((a >= 10 || a <= 20) || (b >= 10 || b <= 20) || (c >= 10 || c <= 20) || (d >= 10 || d <= 20))
flag = 1;
return flag;
}
int squaresum(int a, int b, int c, int d)
{
return pow(a, 2) + pow(b, 2) + pow(c, 2) + pow(d, 2);
}
int cubesum(int a, int b, int c, int d)
{
return pow(a, 3) + pow(b, 3) + pow(c, 3) + pow(d, 3);
}
int avsqrsum(int a, int b, int c, int d)
{
return (pow(a, 2) + pow(b, 2) + pow(c, 2) + pow(d, 2)) / 4;
return 0;
}
%c is tricky, because it doesn't obey the usual "skip whitespace" rule that all the other conversions follow.
So you'll probably want
Code:
// note the initial space in this conversion.
scanf(" %c", &chr);