Thanks for this guys, I am very new to programming and these have helped me a lot. I have only done two of them but will work on the rest of them tonight.
P.S. The DP this is awesome, I'm sure that guy's mom would not like it very much. lol
i have a question on the bunnies exercise. what if there is 2 male adult bunnies and 3 female adult bunnies. does that mean that 6 new bunnies is created? or there should only be 3 bunnies created?
#include <IOStream>
int main()
{
int score;
do {
std::cout<<"Please input your score:";
std::cin>>score;
switch (score/10)
{
case 10:
std::cout<<"You get the A , Congratualations!\n"<<std::endl;
break;
case 9:
std::cout<<"You get the A \n"<<std::endl;
break;
case 8:
std::cout<<"You get the B \n"<<std::endl;
break;
case 7:
std::cout<<"You get the C \n"<<std::endl;
break;
case 6:
std::cout<<"You get the D \n"<<std::endl;
break;
case 5:
std::cout<<"You get the E \n"<<std::endl;
break;
case 4:
std::cout<<"You get the F \n"<<std::endl;
break;
case 3:
std::cout<<"You get the G \n"<<std::endl;
break;
case 2:
std::cout<<"You get the H \n"<<std::endl;
break;
case 1:
std::cout<<"You get the I \n"<<std::endl;
break;
default:
std::cout<<"You fail! \n"<<std::endl;
break;
};
}
while(1);
return 0;
}
do {
std::cout<<"Please input your score:";
std::cin>>score;
switch (score/10)
{
case 10:
std::cout<<"You get the A , Congratualations!\n"<<std::endl;
break;
case 9:
std::cout<<"You get the A \n"<<std::endl;
break;
case 8:
std::cout<<"You get the B \n"<<std::endl;
break;
case 7:
std::cout<<"You get the C \n"<<std::endl;
break;
case 6:
std::cout<<"You get the D \n"<<std::endl;
break;
case 5:
std::cout<<"You get the E \n"<<std::endl;
break;
case 4:
std::cout<<"You get the F \n"<<std::endl;
break;
case 3:
std::cout<<"You get the G \n"<<std::endl;
break;
case 2:
std::cout<<"You get the H \n"<<std::endl;
break;
case 1:
std::cout<<"You get the I \n"<<std::endl;
break;
default:
std::cout<<"You fail! \n"<<std::endl;
break;
};
}
while(1);
Have you run your code? This code will never stop. So that means when your user gives you input, it will give output and immediately ask for input again. In any program, you want it to stop at some point in time.
Also, there is no need for the do-while loop here. To reduce your code and confusion, you would just use a while loop. The use of a do-while loop is when you need the code to be executed at least once before the boolean placed in the while function is checked. As (1) always return true, there should be no problem with this. ALSO, if you were to do that, instead of using while(1), I would use for(;;) which is a quick optimization boost as while(1) actually uses instruction to check and see if 1 is true where as the latter does not.
I also posted a version of the example earlier in this same topic. It DOES use a loop and you can look at it as an example on how to do it more correctly.
I would use for(;;) which is a quick optimization boost as while(1) actually uses instruction to check and see if 1 is true where as the latter does not.
Because the compiler couldn't possible figure out that while (1) can be compiled to a simple unconditional jump. I mean, while (1) is so rarely used, that the developers couldn't possibly think of that one.
@computerquip : Thanks, I ran my code and I stop it with Ctrl+C. and I added "do ... while" for convenience of debugging. I read your code and it help me a lot. I remain my code here and let guys discuss the wrong example.