Saturday 15 January 2011

Value not assigning to variable? C calculator -


I am trying to create a simple calculator in C. I currently have only one problem, that is, when I try to value the value of my operator is stored in the array of characters, it specifies it, but when I get out of the loop, Then it is not assigned to him. I tried to use malloc, but it does not work in advance thanks

  integer calculator () {int exit; Exit = 1; While (exit == 1) {printf ("Welcome to the calculator, please enter the calculation you want to do, if you want to exit exit type \ n"); Float number 1; Float no.2; Four operations; Name ans; Four strings [10]; Infied = 0; Scanf ("% s", and string); Int results = strncmp (string, "EXIT", 10); If (result == 0) {exit = 0; } And {int length = Stellen (string); Int i; = (I = 0; i & lt; = length; i ++) {if (isdigit (string [i]) = 0) {if (= was) = ​​{0} {num1 = (float) string [i] - ' 0 '; Although = 1; } And {num2 = (float) string [i] - '0'; }} Else {operation = string [i]; }} Printf ("num1% f \ n", num1); Printf ("% c \ n", operating); Printf ("num2% f \ n", num2); If (operation == '+') {ans = num1 + num2; } If (operation == '-') {ans = num1 - num2; } If (operation == '/') {ans = num1 / num2; } If (operation == '*') {ans = num1 * num2; } If (operation == '^') {ans = (float) p (num1, num2); } Printf ("Your answer is% f \ n", ans); }} Return 0;   

}

Edit: I am referring to forloop, where is the assignment: operation = string [i];

In the loop for your problem:

 for  ( I = 0; i & lt; = length; i ++) << Code>  

Since the length is strlen (..) , you can not go to length, But for length-1 .

You are doing an extra loop, which is with four letters of 0, which is set to set that zero value - that is the empty string.

Change your loop to:

 For  (i = 0; i   < / Div> 

No comments:

Post a Comment