Hello everybody, i make a simple calculator with C. When I compile it, the compiler didn't show error but when I run my program the result is wrong...

This is my code:

////////////////////////////////////////////////
// Calculator                                 //
// Crated by : Invecta                        //
// Thanks to all members of komunitas-bsi.com //
////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
int main(){

int i, n, iNumber, iResult, iChoice;

printf("+----------------+\n");
printf("|  Calculator    |\n");
printf("+----------------+\n");
printf("| 1. +           |\n");
printf("| 2. -           |\n");
printf("| 3. *           |\n");
printf("| 4. /           |\n");
printf("+----------------+\n");
printf("  Select your choice : ");
scanf("%d",&iChoice);
switch(iChoice){
case 1:
	system("CLS");
	printf("Data : ");scanf("%d", &n);
	for (i=1; i<=n; i++){
	printf("Number %d : ", i ); scanf("%d",&iNumber);
	iResult+=iNumber;}
	break;
case 2:
	system("CLS");
	printf("Data : ");scanf("%d", &n);
	for (i=1; i<=n; i++){
	printf("Number %d : ", i ); scanf("%d",&iNumber);
	iResult-=iNumber;}
	break;
case 3:
	system("CLS");
	printf("Data : ");scanf("%d", &n);
	for (i=1; i<=n; i++){
	printf("Number %d : ", i ); scanf("%d",&iNumber);
	iResult*=iNumber;}
	break;
case 4:
	system("CLS");
	printf("Data : ");scanf("%d", &n);
	for (i=1; i<=n; i++){
	printf("Number %d : ", i ); scanf("%d",&iNumber);
	iResult/=iNumber;}
	break;
}
printf("The result is %d\n", iResult);
system("PAUSE");
return 0;
}

Try initializing these variables to zero...

int i, n, iNumber, iResult, iChoice;

yeah... thats work, but when i choose 2 (-) the result is false.
ex:
Number 1 : 5
Number 2 : 3
The result is -8

Your using

iResult-=iNumber;

Which loads iResult with the negative values of iNumber.

so, what should i do?
i want to number 1 - number 2, how is the formula?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.