// code says misplaced else can some tell me why and if possible make the correction?

#include<stdio.h>
#include<conio.h>
char oper;
int num[2];
int ans;
main()
{
clrscr();
printf("Num:");
scanf("%d",&num[0]);
printf("+");
scanf("%c",&oper);
{
if(oper=='+')
{
printf("+");
}
{
printf("Num:");
scanf("%d",&num[1]);
}
ans = num[0] + num[1] ;
printf("%d",ans);
}
 {
else if(oper=='-')
{
printf("+");
}
{
printf("Num:");
scanf("%d",&num[1]);
}
ans = num[0] - num[1] ;
printf("%d",ans);
}
getch();
}

Recommended Answers

All 8 Replies

#include<stdio.h> //Why doest this let me choose the opper it just goes straight to num[1]
#include<conio.h>
char oper[4];
int num[2];
int ans;
main()
{
clrscr();
printf("Num:");
scanf("%d",&num[0]);

scanf("%c",&oper);
{
if(oper[1]=='+')
{
printf("+");
ans = num[0] + num[1] ;
}
else if(oper[1]=='-')
{
printf("-");
ans = num[0] - num[1] ;
}
else if(oper[1]=='*')
{
printf("*");
ans = num[0] * num[1] ;
}
else if(oper[1]=='/')
{
printf("/");
ans = num[0] / num[1] ;
}
{
printf("Num:");
scanf("%d",&num[1]);
}

printf("%d",ans);
}
getch();
}

%c reads any character, including whitespace. So since you still have a space or newline in the buffer from the last scanf, it will just read that. If you use the format string " %c" with a space in front of the %c, it will discard all whitespace before it reads a character.

Oh and your initial error was because you had an else after an opening brace, but I suppose you've figured that out already.

That one mistake held me back for a lot's of hours take you
ive finally manage to fix the code so that it reads the opperators and does what it need to do
thank you

note there's still alot of thing to do
can someone explaine to me how im gonna be able to incremenent the array oper[1] to opper[2]
cause i need to loop this 4 times w/ the array incrementing each time.

i know the if i use a for loop it will repeats it 4 times but the design should also increment the oper[] for each new operation.

here's the fixed code

#include<stdio.h>
#include<conio.h>
char oper[4];
int num[2];
int ans;
main()
{
clrscr();
printf("Num:");
scanf("%d",&num[0]);
printf("");
scanf(" %c",&oper[1]);
{
if(oper[1]=='+')
{
printf("Num:");
scanf("%d",&num[1]);
ans = num[0] + num[1];
printf("%d",ans);
}
else if(oper[1]=='-')
{
printf("Num:");
scanf("%d",&num[1]);
ans = num[0] - num[1];
printf("%d",ans);
}
else if(oper[1]=='*')
{
printf("Num:");
scanf("%d",&num[1]);
ans = num[0] * num[1];
printf("%d",ans);
}
else if(oper[1]=='/')
{
printf("Num:");
scanf("%d",&num[1]);
ans = num[0] / num[1];
printf("%d",ans);
}
else
printf("Error");

}
getch();
}

//ive figured out the pointer sollutions but the for statement does not work

#include<stdio.h>

    #include<conio.h>

char oper[4];
int num[2],*a,*b;
int ans,i;

main()
{
clrscr();
for(i=1;i<=5;i=i+1); //<---------- This does not loop help me loop the program why?
a=&num[0];
printf("Num:");
scanf("%d",a);
printf("");
scanf(" %c",&oper[0]);
{
{
if(oper[0]=='+')
{
b=&num[1];
printf("Num:");
scanf("%d",b);
ans = *a + *b;
printf("%d",ans);

}
else if(oper[0]=='-')
{
 b=&num[1];
printf("Num:");
scanf("%d",b);
ans = *a - *b;
printf("%d",ans);
}
else if(oper[0]=='*')
{
 b=&num[1];
printf("Num:");
scanf("%d",b);
ans = *a * *b;
printf("%d",ans);
}
else if(oper[0]=='/')
{
 b=&num[1];
printf("Num:");
scanf("%d",b);
ans = *a / *b;
printf("%d",ans);
}
else
printf("Error");

}
}
getch();
return 0;
}

There's nothing inside the for loop. The for loop ends at that semi-colon on line 10.

i always do the simple mistakes..
thank you

You would find it much easier to detect many bugs of this sort if you indent the code in a suitable fashion. While it may take some getting used to - it makes the code look like some sort of modernist poetry - it is well worth the trouble. There are even tools available to perform indentation for you, and these often do a better job than you would do by hand.

For example, here is your code as rendered in Allman Style using the AStyle tool:

#include<stdio.h>
#include<conio.h>

char oper[4];
int num[2],*a,*b;
int ans,i;

int main()
{
    clrscr();
    for(i=1; i<=5; i=i+1);
    a=&num[0];
    printf("Num:");
    scanf("%d",a);
    printf("");
    scanf(" %c",&oper[0]);
    {
        {
            if(oper[0]=='+')
            {
                b=&num[1];
                printf("Num:");
                scanf("%d",b);
                ans = *a + *b;
                printf("%d",ans);

            }
            else if(oper[0]=='-')
            {
                b=&num[1];
                printf("Num:");
                scanf("%d",b);
                ans = *a - *b;
                printf("%d",ans);
            }
            else if(oper[0]=='*')
            {
                b=&num[1];
                printf("Num:");
                scanf("%d",b);
                ans = *a * *b;
                printf("%d",ans);
            }
            else if(oper[0]=='/')
            {
                b=&num[1];
                printf("Num:");
                scanf("%d",b);
                ans = *a / *b;
                printf("%d",ans);
            }
            else
                printf("Error");

        }
    }
    getch();
    return 0;
}

Perhaps this will help you see where the problem lies with the for() loop. If not, consider what the semi-colon at the end of the loop conditional means.

Thank You Ive Sorted the program and it works great now.

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.