Thursday, July 30, 2009

C++ prgm, input?

The following code is supposed to accept input that is between 1 %26amp; 5, %26amp; nonduplicaticates but it wont ask the input again if it's a duplicate. What's wrong? thanks!


do{


cout%26lt;%26lt;"Choose the first peg";


cin%26gt;%26gt;pegone;


cout%26lt;%26lt;endl;


if ( pegone %26lt; 1 || pegone %26gt; 5)


cout%26lt;%26lt;"Please select a number from 1 to 5 or do not chose a duplicate "%26lt;%26lt;endl;


} while(pegone %26lt; 0 || pegone %26gt; 5);


do{


cout%26lt;%26lt;"Choose the second peg";


cin%26gt;%26gt;pegtwo;


cout%26lt;%26lt;endl;


if (( pegtwo %26lt; 1 || pegtwo %26gt; 5) || (pegtwo == pegone))


cout%26lt;%26lt;"Please select a number from 1 to 5 or do not chose a duplicate "%26lt;%26lt;endl;


} while((pegtwo != pegone) %26amp;%26amp; ((pegtwo %26lt; 0) || (pegtwo %26gt; 5)));


do{


cout%26lt;%26lt;"Choose the third peg";


cin%26gt;%26gt;pegthree;


cout%26lt;%26lt;endl;


if (( pegthree %26lt; 1 || pegthree %26gt; 5) || ((pegthree == pegone) || (pegthree == pegtwo)))


cout%26lt;%26lt;"Please select a number from 1 to 5 or do not chose a duplicate "%26lt;%26lt;endl;


} while(((pegthree != pegone) || (pegthree != pegtwo)) %26amp;%26amp; (pegthree %26lt; 0 || pegthree %26gt; 5));


}

C++ prgm, input?
Here is your correct program:


do{


cout%26lt;%26lt;"Choose the first peg";


cin%26gt;%26gt;pegone;


cout%26lt;%26lt;endl;


if ( pegone %26lt; 1 || pegone %26gt; 5)


cout%26lt;%26lt;"Please select a number from 1 to 5 or do not chose a duplicate "%26lt;%26lt;endl;


} while(pegone %26lt; 1 || pegone %26gt; 5);


do{


cout%26lt;%26lt;"Choose the second peg";


cin%26gt;%26gt;pegtwo;


cout%26lt;%26lt;endl;


if (( pegtwo %26lt; 1 || pegtwo %26gt; 5) || (pegtwo == pegone))


cout%26lt;%26lt;"Please select a number from 1 to 5 or do not chose a duplicate "%26lt;%26lt;endl;


} while((pegtwo == pegone) || ((pegtwo %26lt; 1) || (pegtwo %26gt; 5)));


do{


cout%26lt;%26lt;"Choose the third peg";


cin%26gt;%26gt;pegthree;


cout%26lt;%26lt;endl;


if (( pegthree %26lt; 1 || pegthree %26gt; 5) || ((pegthree == pegone) || (pegthree == pegtwo)))


cout%26lt;%26lt;"Please select a number from 1 to 5 or do not chose a duplicate "%26lt;%26lt;endl;


} while(((pegthree == pegone) || (pegthree == pegtwo)) || (pegthree %26lt; 0 || pegthree %26gt; 5));
Reply:Don't Trust cardo's answer. It's a copy from Rag's answer
Reply:the while is in the wrong place.


for the second and third do while statements, do not use (not equal to) use (equal to)


remember, anything whithin the while statement that ends up to be true, will cause the do to be re-executed. also say || so either one will make it repeat.


while((pegtwo != pegone) %26amp;%26amp; (pegtwo %26lt; 0 || pegtwo %26gt; 5));


use


while((pegtwo == pegone) || (pegtwo %26lt; 0 || pegtwo %26gt; 5));


you have too many braces so I have removed some.


hope it works! see edited code below.contact me if you still have problems.





do{


cout%26lt;%26lt;"Choose the first peg";


cin%26gt;%26gt;pegone;


cout%26lt;%26lt;endl;


if ( pegone %26lt; 1 || pegone %26gt; 5)


cout%26lt;%26lt;"Please select a number from 1 to 5 or do not chose a duplicate "%26lt;%26lt;endl;


} while(pegone %26lt; 1 || pegone %26gt; 5);





do{


cout%26lt;%26lt;"Choose the second peg";


cin%26gt;%26gt;pegtwo;


cout%26lt;%26lt;endl;


if (( pegtwo %26lt; 1 || pegtwo %26gt; 5) || (pegtwo == pegone))


cout%26lt;%26lt;"Please select a number from 1 to 5 or do not chose a duplicate "%26lt;%26lt;endl;


} while((pegtwo == pegone) || (pegtwo %26lt; 0 || pegtwo %26gt; 5));





do{


cout%26lt;%26lt;"Choose the third peg";


cin%26gt;%26gt;pegthree;


cout%26lt;%26lt;endl;


if (( pegthree %26lt; 1 || pegthree %26gt; 5) || (pegthree == pegone || pegthree == pegtwo))


cout%26lt;%26lt;"Please select a number from 1 to 5 or do not chose a duplicate "%26lt;%26lt;endl;


} while(((pegthree == pegone) || (pegthree == pegtwo)) || (pegthree %26lt; 0 || pegthree %26gt; 5));


}

funeral flowers

No comments:

Post a Comment