Question: How can i fix my program written in C When i input string 1 and the value of string 1 is less than string 2,
How can i fix my program written in C
When i input string 1 and the value of string 1 is less than string 2, and i perform the divide operation.
My answer comes out to be 0.00.
Example:
Enter a String: 1
Enter a String: two
Enter a String divide
Answer = 0.00
All other operations work.
Here is the program
#include
int getNumber(char str1[]);
int main() { char str1[100], str2[100], str3[100]; printf("Enter a string: "); scanf("%s", &str1); printf("Enter a string: "); scanf("%s", &str2); printf("Enter a string: "); scanf("%s", &str3); int num1, num2; double res; num1 = getNumber(str1); printf("Num1 = %d ", num1); num2 = getNumber(str2); printf("Num2 = %d ", num2); if (strcmp(str3, "+") == 0 || strcmp(str3, "plus") == 0 || strcmp(str3, "add") == 0) { res = num1 + num2; } else if (strcmp(str3, "-") == 0 || strcmp(str3, "minus") == 0 || strcmp(str3, "subtract") == 0) { res = num1 - num2; } else if (strcmp(str3, "/") == 0 || strcmp(str3, "divide") == 0) { if (num2 == 0) { printf("Remainder should not be zero "); return 0; } else { res = num1 / num2; } } else if (strcmp(str3, "*") == 0 || strcmp(str3, "multiply") == 0) { res = num1 * num2; } else { printf("Input is invalid "); return 0; } printf("Answer = %.2lf ", res); return 0; }
int getNumber(char str1[]) { int num1; if (strcmp(str1, "One") == 0 || strcmp(str1, "one") == 0) { num1 = 1; } else if (strcmp(str1, "Two") == 0 || strcmp(str1, "two") == 0) { num1 = 2; } else if (strcmp(str1, "Three") == 0 || strcmp(str1, "three") == 0) { num1 = 3; } else if (strcmp(str1, "Four") == 0 || strcmp(str1, "four") == 0) { num1 = 4; } else if (strcmp(str1, "Five") == 0 || strcmp(str1, "five") == 0) { num1 = 5; } else if (strcmp(str1, "Six") == 0 || strcmp(str1, "six") == 0) { num1 = 6; } else if (strcmp(str1, "Seven") == 0 || strcmp(str1, "seven") == 0) { num1 = 7; } else if (strcmp(str1, "Eight") == 0 || strcmp(str1, "eight") == 0) { num1 = 8; } else if (strcmp(str1, "Nine") == 0 || strcmp(str1, "nine") == 0) { num1 = 9; } else if (strcmp(str1, "Zero") == 0 || strcmp(str1, "zero") == 0) { num1 = 0; } else { num1 = atoi(str1); } return num1; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
