how can i.....please help.......

i want the loop to repeat from the line where the user enters the mathematical operator...

#include
#include

void main(void)

{

clrscr();

int a, b;
char ch, ans;

do
{

printf("
Enter the operation you want to perform:");
scanf("%c",&ch);

printf("
Enter a value:");
scanf("%d",&a);
printf("
Enter another value:");
scanf("%d",&b);

switch (ch) {

case '+': printf("
%d + %d = %d",a,b,a+b);break;

case '-': printf("
%d - %d = %d",a,b,a-b);break;

case '*': printf("
%d * %d = %d",a,b,a*b);break;

case '/': printf("
%d / %d = %d",a,b,a/b);break;

default:
printf("
Invalid Operation!");
}

printf("
Do you want to perform another operation?");
ans = getche();

} while(ans == 'y' || ans == 'Y');

}

Comments

  • [b][red]This message was edited by MT2002 at 2007-2-12 13:14:31[/red][/b][hr]

    : i want the loop to repeat from the line where the user enters the mathematical operator...

    [blue]
    You need to flush the input buffer before the second iteration (just use getchar()):[/blue][code]
    #include
    #include

    void main(void)

    {

    int a, b;
    char ch, ans;

    clrscr ();

    do
    {
    printf("
    Enter the operation you want to perform:");
    scanf("%c",&ch);
    [blue][b]getchar ();[/b] /*scanf() doesnt remove chars from the input buffer, so we need to do it*/
    [/blue]
    printf("
    Enter a value:");
    scanf("%d",&a);
    printf("
    Enter another value:");
    scanf("%d",&b);

    switch (ch) {

    case '+': printf("
    %d + %d = %d",a,b,a+b);break;

    case '-': printf("
    %d - %d = %d",a,b,a-b);break;

    case '*': printf("
    %d * %d = %d",a,b,a*b);break;

    case '/': printf("
    %d / %d = %d",a,b,a/b);break;

    default:
    printf("
    Invalid Operation!");
    }

    printf("
    Do you want to perform another operation?");
    ans = getche();

    } while(ans == 'y' || ans == 'Y');

    }[/code][blue]
    The above should work just fine.

    Hope this helps;[/blue]






  • main() is of return type int.
    See references at http://www.codepedia.com/1/CMain
    bilderbikkel

  • : [b][red]This message was edited by MT2002 at 2007-2-12 13:14:31[/red][/b][hr]
    :
    : : i want the loop to repeat from the line where the user enters the mathematical operator...
    :
    : [blue]
    : You need to flush the input buffer before the second iteration (just use getchar()):[/blue][code]
    : #include
    : #include
    :
    : void main(void)
    :
    : {
    :
    : int a, b;
    : char ch, ans;
    :
    : clrscr ();
    :
    : do
    : {
    : printf("
    Enter the operation you want to perform:");
    : scanf("%c",&ch);
    : [blue][b]getchar ();[/b] /*scanf() doesnt remove chars from the input buffer, so we need to do it*/
    : [/blue]
    : printf("
    Enter a value:");
    : scanf("%d",&a);
    : printf("
    Enter another value:");
    : scanf("%d",&b);
    :
    : switch (ch) {
    :
    : case '+': printf("
    %d + %d = %d",a,b,a+b);break;
    :
    : case '-': printf("
    %d - %d = %d",a,b,a-b);break;
    :
    : case '*': printf("
    %d * %d = %d",a,b,a*b);break;
    :
    : case '/': printf("
    %d / %d = %d",a,b,a/b);break;
    :
    : default:
    : printf("
    Invalid Operation!");
    : }
    :
    : printf("
    Do you want to perform another operation?");
    : ans = getche();
    :
    : } while(ans == 'y' || ans == 'Y');
    :
    : }[/code][blue]
    : The above should work just fine.
    :
    : Hope this helps;[/blue]
    :
    :
    :
    :
    :
    :
    :
    no this code is not doing what i want it to do
    it is asking to enter the operation but the ch character gets a garbage value and the output is invalid operation please help here
  • : no this code is not doing what i want it to do
    : it is asking to enter the operation but the ch character gets a garbage value and the output is invalid operation please help here
    :
    [blue]Try this... (Modified code in [b]blue[/b])[/blue][code]
    #include
    #include

    void main(void)

    {
    [blue]
    int a=0, b=0;
    char ch=0, ans=0;

    clrscr();
    [/blue]
    do
    {

    printf("
    Enter the operation you want to perform:");
    scanf("%c",&ch);

    printf("
    Enter a value:");
    scanf("%d",&a);
    printf("
    Enter another value:");
    scanf("%d",&b);

    switch (ch) {

    case '+': printf("
    %d + %d = %d",a,b,a+b);break;

    case '-': printf("
    %d - %d = %d",a,b,a-b);break;

    case '*': printf("
    %d * %d = %d",a,b,a*b);break;

    case '/': printf("
    %d / %d = %d",a,b,a/b);break;

    default:
    printf("
    Invalid Operation!");
    }

    printf("
    Do you want to perform another operation?");
    ans = getche();
    [blue]
    /* flush input buffer for next iteration */
    fflush(stdin);
    [/blue]
    } while(ans == 'y' || ans == 'Y');

    }
    [/code][blue]
    This code flushes the entire input buffer after each iteration to insure it is clean before the next loop.

    This should work.

    Hope this helps!
    [/blue]
  • [b][red]This message was edited by sasha88 at 2007-2-14 4:21:0[/red][/b][hr]
    : : no this code is not doing what i want it to do
    : : it is asking to enter the operation but the ch character gets a garbage value and the output is invalid operation please help here
    : :
    : [blue]Try this... (Modified code in [b]blue[/b])[/blue][code]
    : #include
    : #include
    :
    : void main(void)
    :
    : {
    : [blue]
    : int a=0, b=0;
    : char ch=0, ans=0;
    :
    : clrscr();
    : [/blue]
    : do
    : {
    :
    : printf("
    Enter the operation you want to perform:");
    : scanf("%c",&ch);
    :
    : printf("
    Enter a value:");
    : scanf("%d",&a);
    : printf("
    Enter another value:");
    : scanf("%d",&b);
    :
    : switch (ch) {
    :
    : case '+': printf("
    %d + %d = %d",a,b,a+b);break;
    :
    : case '-': printf("
    %d - %d = %d",a,b,a-b);break;
    :
    : case '*': printf("
    %d * %d = %d",a,b,a*b);break;
    :
    : case '/': printf("
    %d / %d = %d",a,b,a/b);break;
    :
    : default:
    : printf("
    Invalid Operation!");
    : }
    :
    : printf("
    Do you want to perform another operation?");
    : ans = getche();
    : [blue]
    : /* flush input buffer for next iteration */
    : fflush(stdin);
    : [/blue]
    : } while(ans == 'y' || ans == 'Y');
    :
    : }
    : [/code][blue]
    : This code flushes the entire input buffer after each iteration to insure it is clean before the next loop.
    :
    : This should work.
    :
    : Hope this helps!
    : [/blue]
    :
    i am sorry but this is not helping either, :(,i have tried almost everything.....


  • Changes in [b][blue]BLUE[/blue][/b] below:


    : i want the loop to repeat from the line where the user enters the mathematical operator...
    :
    : #include
    : #include
    :
    : [blue]int[/blue] main(void)
    :
    : {
    :
    : clrscr();
    :
    : int a, b;
    : char ch, ans;
    :
    : do
    : {
    :
    : printf("
    Enter the operation you want to perform:");
    : scanf("%c",&ch);[blue]getchar();[/blue]
    :
    : printf("
    Enter a value:");
    : scanf("%d",&a);[blue]getchar();[/blue]
    : printf("
    Enter another value:");
    : scanf("%d",&b);[blue]getchar();[/blue]
    :
    : switch (ch) {
    :
    : case '+': printf("
    %d + %d = %d",a,b,a+b);break;
    :
    : case '-': printf("
    %d - %d = %d",a,b,a-b);break;
    :
    : case '*': printf("
    %d * %d = %d",a,b,a*b);break;
    :
    : case '/': printf("
    %d / %d = %d",a,b,a/b);break;
    :
    : default:
    : printf("
    Invalid Operation!");
    : }
    :
    : printf("
    Do you want to perform another operation?");
    : ans = [blue]getchar(); /* removed getche, no need to use non-standard functions here */[/blue]
    :
    : } while(ans == 'y' || ans == 'Y');
    :
    [blue]return 0;[/blue]
    : }
    :

  • : : no this code is not doing what i want it to do
    : : it is asking to enter the operation but the ch character gets a garbage value and the output is invalid operation please help here
    : :
    : [blue]Try this... (Modified code in [b]blue[/b])[/blue][code]
    : #include
    : #include
    :
    : void main(void)
    :
    : {
    : [blue]
    : int a=0, b=0;
    : char ch=0, ans=0;
    :
    : clrscr();
    : [/blue]
    : do
    : {
    :
    : printf("
    Enter the operation you want to perform:");
    : scanf("%c",&ch);
    :
    : printf("
    Enter a value:");
    : scanf("%d",&a);
    : printf("
    Enter another value:");
    : scanf("%d",&b);
    :
    : switch (ch) {
    :
    : case '+': printf("
    %d + %d = %d",a,b,a+b);break;
    :
    : case '-': printf("
    %d - %d = %d",a,b,a-b);break;
    :
    : case '*': printf("
    %d * %d = %d",a,b,a*b);break;
    :
    : case '/': printf("
    %d / %d = %d",a,b,a/b);break;
    :
    : default:
    : printf("
    Invalid Operation!");
    : }
    :
    : printf("
    Do you want to perform another operation?");
    : ans = getche();
    : [blue]
    : /* flush input buffer for next iteration */
    : fflush(stdin);
    : [/blue]
    : } while(ans == 'y' || ans == 'Y');
    :
    : }
    : [/code][blue]
    : This code flushes the entire input buffer after each iteration to insure it is clean before the next loop.
    :
    : This should work.
    :
    : Hope this helps!
    : [/blue]
    :


    fflush(stdin) is not ANSI C. http://c-faq.com/stdio/stdinflush.html
  • : Changes in [b][blue]BLUE[/blue][/b] below:
    :
    :
    : : i want the loop to repeat from the line where the user enters the mathematical operator...
    : :
    : : #include
    : : #include
    : :
    : : [blue]int[/blue] main(void)
    : :
    : : {
    : :
    : : clrscr();
    : :
    : : int a, b;
    : : char ch, ans;
    : :
    : : do
    : : {
    : :
    : : printf("
    Enter the operation you want to perform:");
    : : scanf("%c",&ch);[blue]getchar();[/blue]
    : :
    : : printf("
    Enter a value:");
    : : scanf("%d",&a);[blue]getchar();[/blue]
    : : printf("
    Enter another value:");
    : : scanf("%d",&b);[blue]getchar();[/blue]
    : :
    : : switch (ch) {
    : :
    : : case '+': printf("
    %d + %d = %d",a,b,a+b);break;
    : :
    : : case '-': printf("
    %d - %d = %d",a,b,a-b);break;
    : :
    : : case '*': printf("
    %d * %d = %d",a,b,a*b);break;
    : :
    : : case '/': printf("
    %d / %d = %d",a,b,a/b);break;
    : :
    : : default:
    : : printf("
    Invalid Operation!");
    : : }
    : :
    : : printf("
    Do you want to perform another operation?");
    : : ans = [blue]getchar(); /* removed getche, no need to use non-standard functions here */[/blue]
    : :
    : : } while(ans == 'y' || ans == 'Y');
    : :
    : [blue]return 0;[/blue]
    : : }
    : :
    :
    :
    please explain why have you declared main as int and what does the return 0; part do ..........
  • : : Changes in [b][blue]BLUE[/blue][/b] below:
    : :
    : :
    : : : i want the loop to repeat from the line where the user enters the mathematical operator...
    : : :
    : : : #include
    : : : #include
    : : :
    : : : [blue]int[/blue] main(void)
    : : :
    : : : {
    : : :
    : : : clrscr();
    : : :
    : : : int a, b;
    : : : char ch, ans;
    : : :
    : : : do
    : : : {
    : : :
    : : : printf("
    Enter the operation you want to perform:");
    : : : scanf("%c",&ch);[blue]getchar();[/blue]
    : : :
    : : : printf("
    Enter a value:");
    : : : scanf("%d",&a);[blue]getchar();[/blue]
    : : : printf("
    Enter another value:");
    : : : scanf("%d",&b);[blue]getchar();[/blue]
    : : :
    : : : switch (ch) {
    : : :
    : : : case '+': printf("
    %d + %d = %d",a,b,a+b);break;
    : : :
    : : : case '-': printf("
    %d - %d = %d",a,b,a-b);break;
    : : :
    : : : case '*': printf("
    %d * %d = %d",a,b,a*b);break;
    : : :
    : : : case '/': printf("
    %d / %d = %d",a,b,a/b);break;
    : : :
    : : : default:
    : : : printf("
    Invalid Operation!");
    : : : }
    : : :
    : : : printf("
    Do you want to perform another operation?");
    : : : ans = [blue]getchar(); /* removed getche, no need to use non-standard functions here */[/blue]
    : : :
    : : : } while(ans == 'y' || ans == 'Y');
    : : :
    : : [blue]return 0;[/blue]
    : : : }
    : : :
    : :
    : :
    : please explain why have you declared main as int and what does the return 0; part do ..........
    :


    That is the only allowed way to declare main in ANSI C. If you do in any other way, you aren't following the standard and your program might not work on other compilers.

    The return code from the program is sent to the OS, as a report of how the execution of the program was terminated. If the OS or another program calls your program, they can check this result to see if everything worked as expected.
  • : : : Changes in [b][blue]BLUE[/blue][/b] below:
    : : :
    : : :
    : : : : i want the loop to repeat from the line where the user enters the mathematical operator...
    : : : :
    : : : : #include
    : : : : #include
    : : : :
    : : : : [blue]int[/blue] main(void)
    : : : :
    : : : : {
    : : : :
    : : : : clrscr();
    : : : :
    : : : : int a, b;
    : : : : char ch, ans;
    : : : :
    : : : : do
    : : : : {
    : : : :
    : : : : printf("
    Enter the operation you want to perform:");
    : : : : scanf("%c",&ch);[blue]getchar();[/blue]
    : : : :
    : : : : printf("
    Enter a value:");
    : : : : scanf("%d",&a);[blue]getchar();[/blue]
    : : : : printf("
    Enter another value:");
    : : : : scanf("%d",&b);[blue]getchar();[/blue]
    : : : :
    : : : : switch (ch) {
    : : : :
    : : : : case '+': printf("
    %d + %d = %d",a,b,a+b);break;
    : : : :
    : : : : case '-': printf("
    %d - %d = %d",a,b,a-b);break;
    : : : :
    : : : : case '*': printf("
    %d * %d = %d",a,b,a*b);break;
    : : : :
    : : : : case '/': printf("
    %d / %d = %d",a,b,a/b);break;
    : : : :
    : : : : default:
    : : : : printf("
    Invalid Operation!");
    : : : : }
    : : : :
    : : : : printf("
    Do you want to perform another operation?");
    : : : : ans = [blue]getchar(); /* removed getche, no need to use non-standard functions here */[/blue]
    : : : :
    : : : : } while(ans == 'y' || ans == 'Y');
    : : : :
    : : : [blue]return 0;[/blue]
    : : : : }
    : : : :
    : : :
    : : :
    : : please explain why have you declared main as int and what does the return 0; part do ..........
    : :
    :
    :
    : That is the only allowed way to declare main in ANSI C. If you do in any other way, you aren't following the standard and your program might not work on other compilers.
    :
    : The return code from the program is sent to the OS, as a report of how the execution of the program was terminated. If the OS or another program calls your program, they can check this result to see if everything worked as expected.
    :
    ok thanx sorry but this code is still not working according to what i want it to do i am sorry...........after the first time the loop is executed the output is ok but the second time it is run and the client is questioned to enter another operation i am able to enter the operation and the values but the output is invalid operation..
  • : : : Changes in [b][blue]BLUE[/blue][/b] below:
    : : :
    : : :
    : : : : i want the loop to repeat from the line where the user enters the mathematical operator...
    : : : :
    : : : : #include
    : : : : #include
    : : : :
    : : : : [blue]int[/blue] main(void)
    : : : :
    : : : : {
    : : : :
    : : : : clrscr();
    : : : :
    : : : : int a, b;
    : : : : char ch, ans;
    : : : :
    : : : : do
    : : : : {
    : : : :
    : : : : printf("
    Enter the operation you want to perform:");
    : : : : scanf("%c",&ch);[blue]getchar();[/blue]
    : : : :
    : : : : printf("
    Enter a value:");
    : : : : scanf("%d",&a);[blue]getchar();[/blue]
    : : : : printf("
    Enter another value:");
    : : : : scanf("%d",&b);[blue]getchar();[/blue]
    : : : :
    : : : : switch (ch) {
    : : : :
    : : : : case '+': printf("
    %d + %d = %d",a,b,a+b);break;
    : : : :
    : : : : case '-': printf("
    %d - %d = %d",a,b,a-b);break;
    : : : :
    : : : : case '*': printf("
    %d * %d = %d",a,b,a*b);break;
    : : : :
    : : : : case '/': printf("
    %d / %d = %d",a,b,a/b);break;
    : : : :
    : : : : default:
    : : : : printf("
    Invalid Operation!");
    : : : : }
    : : : :
    : : : : printf("
    Do you want to perform another operation?");
    : : : : ans = [blue]getchar(); /* removed getche, no need to use non-standard functions here */[/blue]
    : : : :
    : : : : } while(ans == 'y' || ans == 'Y');
    : : : :
    : : : [blue]return 0;[/blue]
    : : : : }
    : : : :
    : : :
    : : :
    : : please explain why have you declared main as int and what does the return 0; part do ..........
    : :
    :
    :
    : That is the only allowed way to declare main in ANSI C. If you do in any other way, you aren't following the standard and your program might not work on other compilers.
    :
    : The return code from the program is sent to the OS, as a report of how the execution of the program was terminated. If the OS or another program calls your program, they can check this result to see if everything worked as expected.
    :
    ok thanx sorry but this code is still not working according to what i want it to do i am sorry...........after the first time the loop is executed the output is ok but the second time it is run and the client is questioned to enter another operation i am able to enter the operation and the values but the output is invalid operation..
  • : : : : Changes in [b][blue]BLUE[/blue][/b] below:
    : : : :
    : : : :
    : : : : : i want the loop to repeat from the line where the user enters the mathematical operator...
    : : : : :
    : : : : : #include
    : : : : : #include
    : : : : :
    : : : : : [blue]int[/blue] main(void)
    : : : : :
    : : : : : {
    : : : : :
    : : : : : clrscr();
    : : : : :
    : : : : : int a, b;
    : : : : : char ch, ans;
    : : : : :
    : : : : : do
    : : : : : {
    : : : : :
    : : : : : printf("
    Enter the operation you want to perform:");
    : : : : : scanf("%c",&ch);[blue]getchar();[/blue]
    : : : : :
    : : : : : printf("
    Enter a value:");
    : : : : : scanf("%d",&a);[blue]getchar();[/blue]
    : : : : : printf("
    Enter another value:");
    : : : : : scanf("%d",&b);[blue]getchar();[/blue]
    : : : : :
    : : : : : switch (ch) {
    : : : : :
    : : : : : case '+': printf("
    %d + %d = %d",a,b,a+b);break;
    : : : : :
    : : : : : case '-': printf("
    %d - %d = %d",a,b,a-b);break;
    : : : : :
    : : : : : case '*': printf("
    %d * %d = %d",a,b,a*b);break;
    : : : : :
    : : : : : case '/': printf("
    %d / %d = %d",a,b,a/b);break;
    : : : : :
    : : : : : default:
    : : : : : printf("
    Invalid Operation!");
    : : : : : }
    : : : : :
    : : : : : printf("
    Do you want to perform another operation?");
    : : : : : ans = [blue]getchar(); /* removed getche, no need to use non-standard functions here */[/blue]
    : : : : :
    : : : : : } while(ans == 'y' || ans == 'Y');
    : : : : :
    : : : : [blue]return 0;[/blue]
    : : : : : }
    : : : : :
    : : : :
    : : : :
    : : : please explain why have you declared main as int and what does the return 0; part do ..........
    : : :
    : :
    : :
    : : That is the only allowed way to declare main in ANSI C. If you do in any other way, you aren't following the standard and your program might not work on other compilers.
    : :
    : : The return code from the program is sent to the OS, as a report of how the execution of the program was terminated. If the OS or another program calls your program, they can check this result to see if everything worked as expected.
    : :
    : ok thanx sorry but this code is still not working according to what i want it to do i am sorry...........after the first time the loop is executed the output is ok but the second time it is run and the client is questioned to enter another operation i am able to enter the operation and the values but the output is invalid operation..
    :


    Oops, when I removed getche() I forgot to add an additionald getchar();
    With this change, things should work:

    ans = getchar();
    getchar();


    As you may notice, the code looks poor. scanf() doesn't remove the linefeed character that comes when you press enter, and therefore leaves it in the input buffer. Next function to read from the input buffer will get it. Adding an extra getchar() will take care of that, but it doesn't look pretty.

    In a serious program, scanf() wouldn't be used at all, but it is good to use it while learning C, since it is similar to printf().
  • : : : : : Changes in [b][blue]BLUE[/blue][/b] below:
    : : : : :
    : : : : :
    : : : : : : i want the loop to repeat from the line where the user enters the mathematical operator...
    : : : : : :
    : : : : : : #include
    : : : : : : #include
    : : : : : :
    : : : : : : [blue]int[/blue] main(void)
    : : : : : :
    : : : : : : {
    : : : : : :
    : : : : : : clrscr();
    : : : : : :
    : : : : : : int a, b;
    : : : : : : char ch, ans;
    : : : : : :
    : : : : : : do
    : : : : : : {
    : : : : : :
    : : : : : : printf("
    Enter the operation you want to perform:");
    : : : : : : scanf("%c",&ch);[blue]getchar();[/blue]
    : : : : : :
    : : : : : : printf("
    Enter a value:");
    : : : : : : scanf("%d",&a);[blue]getchar();[/blue]
    : : : : : : printf("
    Enter another value:");
    : : : : : : scanf("%d",&b);[blue]getchar();[/blue]
    : : : : : :
    : : : : : : switch (ch) {
    : : : : : :
    : : : : : : case '+': printf("
    %d + %d = %d",a,b,a+b);break;
    : : : : : :
    : : : : : : case '-': printf("
    %d - %d = %d",a,b,a-b);break;
    : : : : : :
    : : : : : : case '*': printf("
    %d * %d = %d",a,b,a*b);break;
    : : : : : :
    : : : : : : case '/': printf("
    %d / %d = %d",a,b,a/b);break;
    : : : : : :
    : : : : : : default:
    : : : : : : printf("
    Invalid Operation!");
    : : : : : : }
    : : : : : :
    : : : : : : printf("
    Do you want to perform another operation?");
    : : : : : : ans = [blue]getchar(); /* removed getche, no need to use non-standard functions here */[/blue]
    : : : : : :
    : : : : : : } while(ans == 'y' || ans == 'Y');
    : : : : : :
    : : : : : [blue]return 0;[/blue]
    : : : : : : }
    : : : : : :
    : : : : :
    : : : : :
    : : : : please explain why have you declared main as int and what does the return 0; part do ..........
    : : : :
    : : :
    : : :
    : : : That is the only allowed way to declare main in ANSI C. If you do in any other way, you aren't following the standard and your program might not work on other compilers.
    : : :
    : : : The return code from the program is sent to the OS, as a report of how the execution of the program was terminated. If the OS or another program calls your program, they can check this result to see if everything worked as expected.
    : : :
    : : ok thanx sorry but this code is still not working according to what i want it to do i am sorry...........after the first time the loop is executed the output is ok but the second time it is run and the client is questioned to enter another operation i am able to enter the operation and the values but the output is invalid operation..
    : :
    :
    :
    : Oops, when I removed getche() I forgot to add an additionald getchar();
    : With this change, things should work:
    :
    : ans = getchar();
    : getchar();
    :
    :
    : As you may notice, the code looks poor. scanf() doesn't remove the linefeed character that comes when you press enter, and therefore leaves it in the input buffer. Next function to read from the input buffer will get it. Adding an extra getchar() will take care of that, but it doesn't look pretty.
    :
    : In a serious program, scanf() wouldn't be used at all, but it is good to use it while learning C, since it is similar to printf().
    :
    THANX MR.LUDIN THE CODE IS WORKING THE WAY I WANTED THANX A MILLION AGAIN FOR HELPING
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories