Saturday, May 22, 2010

How do i loop this program in C so that i can just start from the beginning again until i quit?

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


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





main ()


{


float c,f;





char menu_conv;


printf("Conversion Program!!!\n");


printf("Enter the type of source temperature by typing c or f: ");





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





if(menu_conv =='c' || menu_conv=='f')


{


switch(menu_conv)


{


case 'c':


printf("Enter a number in Celcius\n");


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


f= (9/5)* c+32;


printf("%f degrees celcius = %f degrees fahrenheit\n", c,f);


break;








case 'f':


printf("Enter a number in Fahrenheit\n");


scanf("%f", %26amp;f);


c=(5/9)*(f-32);


printf("%f degrees fahrenheit = %f degrees celcius\n", f,c);


break;





default:


break;


}


}


else


{


printf("Invalid choice!\n");


}

















return 0;





}

How do i loop this program in C so that i can just start from the beginning again until i quit?
hey try this. i have made some changes and the changes are in capitals. good luck.





main ()


{


float c,f;


CHAR ANSWER;


char menu_conv;


ANSWER = 'Y'


WHILE (ANSWER)


{


printf("Conversion Program!!!\n");


printf("Enter the type of source temperature by typing c or f: ");





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





if(menu_conv =='c' || menu_conv=='f')


{


switch(menu_conv)


{


case 'c':


printf("Enter a number in Celcius\n");


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


f= (9/5)* c+32;


printf("%f degrees celcius = %f degrees fahrenheit\n", c,f);


break;





case 'f':


printf("Enter a number in Fahrenheit\n");


scanf("%f", %26amp;f);


c=(5/9)*(f-32);


printf("%f degrees fahrenheit = %f degrees celcius\n", f,c);


break;





default:


break;


}/*END OF SWITCH*/


}?*END OF IF*/


else


{


printf("Invalid choice!\n");


}?*END OF ELSE*/





PRINTF ("PLEASE TYPE Y IF YOU WANT TO CONTINUE,ELSE TYPE N.\T");


SCANF("%C",%26amp;ANSWER);


}/*END WHILE LOOP*/


return 0;


}/*END OF MAIN*/
Reply:i'll write it in C++ so you can change it to C


after the main define a while loop on a char like this


char x;


cin%26gt;%26gt;x; --%26gt; (input x)


while ( x != eof()) (if the value doesn't equal to the end of file)


{


(your program)


}


cin%26gt;%26gt;x; --%26gt; (input again)


} (end of main)
Reply:surround it whith a while








menu_conv='x';


// pressq to quit the loop.


while (menu_conv != 'q' ) {





//


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





if(menu_conv =='c' || menu_conv=='f')


{


switch(menu_conv)


{


case 'c':


printf("Enter a number in Celcius\n");


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


f= (9/5)* c+32;


printf("%f degrees celcius = %f degrees fahrenheit\n", c,f);


break;








case 'f':


printf("Enter a number in Fahrenheit\n");


scanf("%f", %26amp;f);


c=(5/9)*(f-32);


printf("%f degrees fahrenheit = %f degrees celcius\n", f,c);


break;





default:


break;


}


}


else if (menu_conv != 'q')


{


printf("Invalid choice!\n");


}








}


No comments:

Post a Comment