Monday, July 27, 2009

C++ adding yes and no program?

this is my program:


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





void binary(int);





void main(void)


{


int number;





cout %26lt;%26lt; "input a decimal value: ";


cin %26gt;%26gt; number;


if (number %26lt; 0)


cout %26lt;%26lt; "That is not a positive integer.\n";


else


{


cout %26lt;%26lt; number %26lt;%26lt; " converted to binary is: ";


binary(number);


cout %26lt;%26lt; endl;


}


}





void binary(int number)


{


int remainder;





if(number %26lt;= 1)


{


cout %26lt;%26lt; number;


return;


}





remainder = number%2;


binary(number %26gt;%26gt; 1);


cout %26lt;%26lt; remainder;


}


question is how can i add yes/no statements?? if yes=program will repeat and if no= program will terminate.. pls help im new in c++

C++ adding yes and no program?
BOOL x = FALSE;





....some statements





if (some condition )


{


x = TRUE;


}


else


{


x = FALSE;


}





if (x == TRUE)


{


some stuff


}
Reply:void Main(){


for(;;){


cout %26lt;%26lt; "Continue?";


cin %26gt;%26gt; myInt;


If (myInt ==2){


Break;}


elseif (myInt==1){


cout %26lt;%26lt; "Continueing";


MainFunction();}


else{


cout %26lt;%26lt; "Error";}


system("pause");}


void MainFunction(){


...


}





2 would be no, 1 would be yes.





The for(;;) is an infinate loop.





If user inputs 2 than it will goto the main function, once done it will go back to the loop.





1 will break the loop.





Any other number will prompt "error" and go back to the top of the loop.
Reply:Put all of the code in the main() section wrapped in a do while loop.





Along these lines:





do


{





bool repeat;





your code.....





cout%26lt;%26lt;"Would you like to run again?\n";


cin%26gt;%26gt;repeat;





}


while (repeat == 1);

sound cards

No comments:

Post a Comment