# 0 failures but does not comple? anyone understand?

Discussion in 'C' started by colleeng, Aug 21, 2012.

1. ### colleengNew Member

Joined:
Aug 21, 2012
Messages:
1
0
Trophy Points:
0
I wrote a calculator program the output should be:

2. subtraction
3. multiplicaiton
4. division
5. Exit

please enter two numbers separated by a space: ?
the result of %f and % f is %f.

I've tried for a while. I am not sure about the while loop. Any help I would be so grateful. I cant attach the code below so i copied pasted it:

Code:
```#include<stdio.h>
float choice;
float result;
float num1, num2;
int get_choice();
int main()
{
{
float num1, num2;
int choice;
float result;

do
{
printf("Welcome to Colleen Gonzales's Handy Calculator");
printf("2. Subtract\n");
printf("3. Multipilcation\n");
printf("4. Division\n");
printf("5. Exit\n");

scanf("%f", &choice);
}
while(  (choice = 1,2,3, 4 , 5) );

printf("Please choose two numbers seperated by a space:");
scanf("%f %f", &num1, &num2);

return 0;
}

{
float num1, num2, result;
int choice;

while( (choice=get_choice()) !=5)
{
switch (choice)
{
case 1 : result = num1 + num2;
break;
case 2 : result = num1-num2;
break;
case 3 : result = num1 * num2;
break;
case 4 : result = num1/num2;
break;
case 5 : printf("Goodbye");
break;
default  :printf("Please respond with 1, 2, 3, 4, or 5\n");
break; /*end of switch*/
}
}

printf("The result of %f and %f is %.2f\n", &num1, & num2, &result);

return 0;
}
}```

2. ### DRKNew Member

Joined:
Apr 13, 2012
Messages:
44
3
Trophy Points:
0
Code:
```#include <stdio.h>

void clear_keyboard_buffer(void)
{
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
}

int get_choice(float *num1, float *num2)
{
int choice, items;

do
{
puts("Welcome to Colleen Gonzales's Handy Calculator");
puts("2. Subtract");
puts("3. Multipilcation");
puts("4. Division");
puts("5. Exit");
items = scanf("%d", &choice);
clear_keyboard_buffer();
} while (items != 1);

if (choice >= 1 && choice <= 4)
{
do
{
puts("Please choose two numbers seperated by a space:");
items = scanf("%f %f", num1, num2);
clear_keyboard_buffer();
} while (items != 2);
}

return choice;
}

int main()
{
int choice;
float num1, num2, result;

do
{
switch (choice = get_choice(&num1, &num2))
{
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
if (num2 != 0)
{
result = num1 / num2;
break;
}
else
{
puts("Division by zero is invalid!");
continue;
}
case 5:
puts("Goodbye");
continue;
default:
puts("Please respond with 1, 2, 3, 4, or 5");
continue;
}  /*end of switch*/
printf("The result of %f and %f is %.2f\n", num1, num2, result);
} while (choice != 5);

return 0;
}```

Joined:
Aug 18, 2012
Messages:
17
0
Trophy Points:
0
hello! I identified following mistakes in your program
1)Do not use '&' in printf functions because we need to access the value of the variable not the value of the address of the variable.
2)in the while loops do not use assignment operators '=' instead use comparision operator '==',instead of using ' , '(comma) for showing that you value of choice may be 1,2,3,..
use '||' operator .
3)Also note that you should the same type of datatype for a particular variable(here let it be choice,so in all statements use %d and not %f)
you may check the edited program ->
Code:
```#include<stdio.h>
float choice;
float result;
float num1, num2;
int main()
{
float num1, num2;
int choice;
float result;
while(choice<6) {
printf("Welcome to Colleen Gonzales's Handy Calculator\n");
printf("2. Subtract\n");
printf("3. Multipilcation\n");
printf("4. Division\n");
printf("5. Exit\n");

scanf("%d", &choice);
if(choice==5)  {
printf("Goodbye");
return 0;
}
printf("Please choose two numbers seperated by a space:");//no need to write 'separated by two spaces'it is inbuild feature of scanf you may also check it that you may press enter instead of spaces between two numbers
scanf("%f %f", &num1, &num2);
switch (choice)
{
case 1 : result = num1 + num2;
break;
case 2 : result = num1-num2;
break;
case 3 : result = num1 * num2;
break;
case 4 : result = num1/num2;
break;
default  :printf("Please respond with 1, 2, 3, 4, or 5\n");
break; /*end of switch*/
}
printf("The result of %f and %f is %6.2f\n", num1, num2, result);
}
return 0;
}```

4. ### GirlyGeekNew Member

Joined:
Oct 31, 2012
Messages:
4
0
Trophy Points:
0
Why is it a float and not a double or an int? Also, when I ran the program and entered "t" it went into a weird loop. How do you account for the default message to appear when a bad input is entered?

5. ### hobbyistNew Member

Joined:
Jan 7, 2012
Messages:
141
0
Trophy Points:
0
a float works well for either whole numbers or fractional numbers. for example, with division, ints will not give a remainder. doubles aren't really needed unless the desired precision of the result is large.

"t" caused a stream error because a character is the wrong data type expected by scanf. to avoid stream errors, you can use a character array and sscanf or one of the strto... functions.

Code:
```char buf[10];
int num;

do {
num = 0;
printf("Enter a positive number: ");
fgets(buf, sizeof buf, stdin);
if((sscanf(buf, "%d", &num)) != 1)
} while(num == 0);
```
another way is to check the return value of scanf.

Code:
```int num;

printf("Enter a positive number: ");
if((scanf("%d", &num)) != 1)
// stream error
```
either way has issues; with the char array, something like 123abc will appear valid because sscanf will store 123 and ignore the characters. with the scanf, you've still got the stream error to deal with.

in my opinion, the char array is more reliable as you can further test for invalid characters before trying to convert it to a number or whatnot.

6. ### GirlyGeekNew Member

Joined:
Oct 31, 2012
Messages:
4
0
Trophy Points:
0
Why is the char array more reliable for testing invalid characters? I was told strod was faster.

7. ### hobbyistNew Member

Joined:
Jan 7, 2012
Messages:
141
0
Trophy Points:
0
the strto functions return 0 if the string cannot be converted, but what if 0 is a valid result for the program? if invalid input can return a valid result, then that's not good.

Code:
```unsigned char IsValid(const char *s) {

while(*s) {
if(!isdigit(*s))
return 0;
++s;
}

return 1;
}```
try sscanf, strtol, and IsValid on a few string inputs. 123abc is valid on two of them but it's not a valid number.