Dimple problem with if/else and looping

This is a discussion on Dimple problem with if/else and looping within the C Programming forums, part of the General Programming Boards category; I have to create a program that takes one integer (the value of the integer will be 1, 2, or ...

1. Dimple problem with if/else and looping

I have to create a program that takes one integer (the value of the integer will be 1, 2, or 3). The program behaves differently depending on the value of the integer. If the number is 1, then get 2 integers from the user, and print the remainder of the first number divided by the second. If the number given is 2, then take a double from the user and print the double rounded up to the next integer. If the number given is 3, read an int from the user and print whether the number given is odd or even.

I have created the following code but I am having trouble with the 3 different numbers running 3 different jobs. - rounding to the next integer etc

Code:
```#include <stdio.h>

int main()
{
int x, y, z;
double d;

printf("Please enter in 1, 2 or 3: ");
scanf("%d", &x);

if (x == 1)
{
printf("Please enter in 2 new integers: ");
scanf("%d%d", &x, &y);
printf("%d", x % y);
}
else if (x == 2)

{
printf("Please enter in a double with a decimal: ");
scanf("%f", &d);
printf("%f\n", d);
}
else (x == 3);
{
printf("Please enter in an integer: ");
scanf("%d", x);
}
if (x % 2 == 0)
{
printf("%d is even\n", x);
}

else if (x % 2 != 0)
{
printf("%d is odd\n", x);
}
}

return 0;
}```

2. > else (x == 3);
else doesn't have a condition.

> scanf("%d", x);
Compare this with your other scanf calls.

Finding problems is easier with a consistent indent policy.
SourceForge.net: Indentation - cpwiki

3. I'm not sure what you are having problems with exactly, but here you miss an ampersand at least.

Code:
```        else (x == 3);
{
printf("Please enter in an integer: ");
scanf("%d", x);
}```

4. Originally Posted by Subsonics
I'm not sure what you are having problems with exactly, but here you miss an ampersand at least.
first off my program wont even compile.

5. Originally Posted by benrogers
first off my program wont even compile.
Give the errors and indent your code right to get further help.

Tim S.

6. Originally Posted by benrogers
first off my program wont even compile.
Well, then why didn't you say so. My point was that your problem description was pathetic.

7. Code:
```#include <stdio.h>

int main()
{
int x, y, z;
double d;

printf("Please enter in 1, 2 or 3: ");
scanf("%d", &x);

if (x == 1)
{
printf("Please enter in 2 new integers: ");
scanf("%d%d", &x, &y);
printf("%d", x % y);
}
else if (x == 2)

{
printf("Please enter in a double with a decimal: ");
scanf("%f", &d);
printf("%f\n", d);
}
else (x == 3);
{
printf("Please enter in an integer: ");
scanf("%d", x);
}
if (x % 2 == 0)
{
printf("%d is even\n", x);
}

else if (x % 2 != 0)
{
printf("%d is odd\n", x);
}
}

return 0;
}```
40 expected unqualified-id before "return"
40 expected unqualified-id before "return"
41 expected declaration before '}' token

8. Code:
```#include <stdio.h>

int main()
{
int x, y, z;
double d;

printf("Please enter in 1, 2 or 3: ");
scanf("%d", &x);

if (x == 1)
{
printf("Please enter in 2 new integers: ");
scanf("%d%d", &x, &y);
printf("%d", x % y);
}
else if (x == 2)
{
printf("Please enter in a double with a decimal: ");
scanf("%lf", &d);    // %f changed to %lf
printf("%lf\n", d);  // ditto
}
else // redundant check removed here
{
printf("Please enter in an integer: ");
scanf("%d", &x); // ampersand added here
}

if (x % 2 == 0)
{
printf("%d is even\n", x);
}
else if (x % 2 != 0)
{
printf("%d is odd\n", x);
}

// an extra brace found here removed

return 0;
}```

9. Originally Posted by Subsonics
Code:
```#include <stdio.h>

int main()
{
int x, y, z;
double d;

printf("Please enter in 1, 2 or 3: ");
scanf("%d", &x);

if (x == 1)
{
printf("Please enter in 2 new integers: ");
scanf("%d%d", &x, &y);
printf("%d", x % y);
}
else if (x == 2)
{
printf("Please enter in a double with a decimal: ");
scanf("%lf", &d);    // %f changed to %lf
printf("%lf\n", d);  // ditto
}
else // redundant check removed here
{
printf("Please enter in an integer: ");
scanf("%d", &x); // ampersand added here
}

if (x % 2 == 0)
{
printf("%d is even\n", x);
}
else if (x % 2 != 0)
{
printf("%d is odd\n", x);
}

// an extra brace found here removed

return 0;
}```
Thanks for this.

- when entering either 1,2 or 3 it always says the number is either odd or even.
- when entering 1 it does not show the remainder.

when the number 3 is entered everything is fine.

10. Because your odd/even check is outside the else condition. Also, the if (x %2 != 0) is redundant, so ditch it.

11. Originally Posted by anduril462
Because your odd/even check is outside the else condition. Also, the if (x %2 != 0) is redundant, so ditch it.
Alright fixed that.

Updated code:
Code:
```#include <stdio.h>

int main()
{
int x, y, z;
double d;

printf("Please enter in 1, 2 or 3: ");
scanf("%d", &x);

if (x == 1)
{
printf("Please enter in 2 new integers: ");
scanf("%d%d", &x, &y);

z = x % y;

printf("%d", z);
}

else if (x == 2)
{
printf("Please enter in a double with a decimal: ");
scanf("%lf", &d);
printf("%lf\n", d++);
}

else
{
printf("Please enter in an integer: ");
scanf("%d", &x);

if (x % 2 == 0)
{
printf("%d is even\n", x);
}

else
{
printf("%d is odd\n", x);
}
}

return 0;
}```
The remainder isn't working when you press 1 and enter in 2 integers. Everything else works.

12. Remainder worked fine for me. What test data did you use? Your rounding (item 2) however, does not work at all. You just print the number as a float. Your problem specifies this number be "rounded up to the nearest integer". Do you mean always round up, like 123.4 rounds up to 124? If so, look at ciel(). If you mean the more traditional "round to nearest", look at round().