// 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();
}
hayate98
0
Newbie Poster
Recommended Answers
Jump to Post
%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 …
Jump to PostThere's nothing inside the
for
loop. The for loop ends at that semi-colon on line 10.
All 8 Replies
hayate98
0
Newbie Poster
sepp2k
378
Practically a Master Poster
hayate98
0
Newbie Poster
hayate98
0
Newbie Poster
Moschops
683
Practically a Master Poster
Featured Poster
hayate98
0
Newbie Poster
Schol-R-LEA
1,446
Commie Mutant Traitor
Featured Poster
hayate98
0
Newbie Poster
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.