Thursday, July 9, 2009

On this calculator written in C, how do I make the whole CODE run again, and how do I make a command to exit?

this is the code:


#include %26lt;stdio.h%26gt;





int main( )


{


int z;


int x;





printf( "This is my first useful (By useful I meant that it does something other than saying hello) program ever created in C" );


printf( "\n" );


printf( "\n" );


printf( "Note that this calculator will subtract and divide the first integer by the second one" );


printf( "\n" );


printf( "\n" );





printf( "Input your first integer to be calculated:" );


scanf( "%d", %26amp;z);





printf( "Please input the second one:" );


scanf( "%d", %26amp;x);





printf ( "Added: %d \n", z + x );


printf ( "Subtracted: %d \n", z - x );


printf ( "Multiplied: %d \n", z * x );


printf ( "Divided: %d \n", z / x );


printf ( "First Integer squared: %d \n", z * z );


printf ( "Second Integer squared: %d \n", x * x );


getchar();


}








so I am trying to make it run again, not exit right after it shows the results!





and I am also trying to make it quit when the user wants to, but not by killing the process!








something similar to this.Close in C#

On this calculator written in C, how do I make the whole CODE run again, and how do I make a command to exit?
create simple function and call it till the user doesnt wish


to quit.








void perform_operation( )


{


int z;


int x;





printf( "This is my first useful (By useful I meant that it does something other than saying hello) program ever created in C" );


printf( "\n" );


printf( "\n" );


printf( "Note that this calculator will subtract and divide the first integer by the second one" );


printf( "\n" );


printf( "\n" );





printf( "Input your first integer to be calculated:" );


scanf( "%d", %26amp;z);





printf( "Please input the second one:" );


scanf( "%d", %26amp;x);





printf ( "Added: %d \n", z + x );


printf ( "Subtracted: %d \n", z - x );


printf ( "Multiplied: %d \n", z * x );


printf ( "Divided: %d \n", z / x );


printf ( "First Integer squared: %d \n", z * z );


printf ( "Second Integer squared: %d \n", x * x );


}





int main()


{


char ans;


do{


perform_operation();


printf("\nEnter 'e' to quit");


scanf("%c",%26amp;ans);


} while (ans!='e');


return 0;


}








//Check the syntax for compile time errors if any.
Reply:put the whole thing in a while loop





char cInput = ' ';


while (cInput != 'q')


{





}





Instead of scanf() into an integer, you read a character string, see if it is "q" (quit) and if so, break; from the loop. You may have to assign to a "command" variable (string) and see if it is "q" or perhaps "n" (new problem).
Reply:Use a while loop with a flag and a prompt.





Insert the following lines after the int x; statement:





char choice='y';


while (choice=='y'){





Then, insert the following lines before getchar();





printf("Press y to run again, n to quit");


scanf( "%c", %26amp;choice);


}
Reply:EDIT:


%c is an individual char, %s is a c-string (array of characters)





===============================


===============================


===============================


I've just now tried compiling this and I see the problem. Change getchar() to getch();





getchar() reads until the [ENTER] key is pressed, while getch() only reads until any key is pressed. For some reason, when I hit the [ENTER] key to input the second integer getchar() is seeing that and returning immediately.
Reply:try this


#include %26lt;stdio.h%26gt;





int main( )


{


int z;


int x;


char ch = 'c';





printf( "This is my first useful (By useful I meant that it does something other than saying hello) program ever created in C" );


printf( "\n" );


printf( "\n" );


printf( "Note that this calculator will subtract and divide the first integer by the second one" );


printf( "\n" );


printf( "\n" );





while(ch != 'X')


{


printf( "Input your first integer to be calculated:" );


scanf( "%d", %26amp;z);





printf( "Please input the second one:" );


scanf( "%d", %26amp;x);





printf ( "Added: %d \n", z + x );


printf ( "Subtracted: %d \n", z - x );


printf ( "Multiplied: %d \n", z * x );


printf ( "Divided: %d \n", z / x );


printf ( "First Integer squared: %d \n", z * z );


printf ( "Second Integer squared: %d \n", x * x );


ch = getchar();


}


}
Reply:main()


{





/* do initial logic here */





for(;;) /* loop forever */


{


char cx[20];


int x;


/* prompt the user to enter a number */








/* get the input as a string */


sscanf("%s", cx);


if (strcmp(cx,"q") == 0) /* quit? */


break;





x=atoi(cx); /* convert the string to an integer */





/* do your other logic here */


}





/* do your quit logic here */





}
Reply:Here is a simple solution using recursion. Append the code right before the end of the main function.





// assuming 'X' is your input for exit


if( getchar( ) != 'X' ) // if the input is not 'X', call main again


main( ) ;


No comments:

Post a Comment