HI
I need a little help with this program

#include<stdio.h>
#include<conio.h>
void main ()
{
float num1,num2,result=0;
char ch;
clrscr();
printf("\nEnter 1st num:");
scanf("%f",&num1);
printf("\nEnter 2nd num:");
scanf("%f",&num2);
wrong:
printf("\nEnter + for addition"
"\nEnter - for subtraction"
"\nEnter / for division"
"\nEnter * for multiplication\n");
scanf(" %c",&ch);
if (ch=='+')
result=num1+num2;
else
if (ch=='-')
result=num1-num2;
else
if (ch=='/')
result=num1/num2;
else
if (ch=='*')
result=num1*num2;
else
{
printf("NO MATCH FOUND");
goto wrong;
}
printf("Result=%f",result);
getch();
}

Ive used goto statement in case if wrong character is given for input and this goto statement will rerun the program till the time correct character is given as input what should i do if i want to run it only for selected number of times(in case of wrong character)????

Use int main(), indent your code, use switch cases and a while loop instead of goto and if cases.

Loops are always better than goto but with goto I think this can be done:

You may also use another variable by the name cnt which would hold the number of times you want to re-run the program in case of a wrong input symbol and alter the code as:

cnt=<number of times you want to run loop>;
----program block----
.
.
.
else // The last else
cnt--;

if(cnt>0)
goto wrong;

----------------------------------------------
And your ' / ' operator will generate an error if num2 is 0 which is not taken care of in the code.

Loops are always better than goto but with goto I think this can be done:

You may also use another variable by the name cnt which would hold the number of times you want to re-run the program in case of a wrong input symbol and alter the code as:

cnt=<number of times you want to run loop>;
----program block----
.
.
.
else // The last else
cnt--;

if(cnt>0)
goto wrong;

That doesn't make much sense. What you said(IF you said what I think you said) can be achieved more conveniently using a loop.

I just said that loops are the best way to achieve the problem.

And if the person wants to continue in the format she has coded then she can use "goto" as mentioned above.

hmmm thx devnar and csurfer that helped i understood my mistake and the new logic...

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.