What\'s wrong with this piece of code. It should run correctly as far as I know,
ID: 3695208 • Letter: W
Question
What's wrong with this piece of code. It should run correctly as far as I know, but when i execute it it just runs on indefinitely.
--------------------------------------------------------
The question:
Generate a random number from 1 to 7 and use it to pick which part to add.
Write the logic that completes the following table:
Add a loop around the above logic. Have the loop end when the cootie is completed. To make sure the loop around the whole program is correct and will eventually end; you'll need to keep track of what parts have rolled already and how many of have been rolled. Create counters (int numLegs=0;) and increment (numLegs++) them when appropriate. (if (roll==1) {numBody=1;} )
With all that done, change the if so that you can only place pieces if the prerequisites are met. To add a leg, you need to have a body and the right dice roll. The code will look similar to:
if (roll == 6 && numBody == 1)
{
numLegs ++;
}
--------------------------------------------------------------------------------
The code;
#include<iostream>
#include<cstdlib>
#include<conio.h>
#include<ctime>
using namespace std;
int diceRoll, Body, Head, Antenna, Wings, Stinger, Legs, Eyes;
int rollDice() {
int rand_no= (rand()%7);
int r=rand_no+1;
cout<<" You rolled a "<<r<<endl;
return r;
}
int addBody(int Body){
if(Body<1) {
Body++;
}
return Body;
}
int addHead(int Body, int Head){
if(Head<1 && Body==1){
Head++;
}
return Head;
}
int addAntenna(int Body, int Head, int Antenna){
if(Antenna<2 &&Body==1 &&Head==1){
Antenna++;
}
return Antenna;
}
int addWings(int Body, int Wings){
if(Wings<2 &&Body==1){
Wings++;
}
return Wings;
}
int addStinger(int Body, int Stinger){
if(Stinger<1 &&Stinger==1){
Stinger++;
}
return Stinger;
}
int addLegs(int Body, int Legs){
if(Legs<4 &&Body==1){
Legs++;
}
return Legs;
}
int addEyes(int Body, int Head, int Eyes){
if(Eyes<6 &&Body==1 &&Head==1){
Eyes++;
}
return Eyes;
}
void applyRoll(int diceRoll){
if (diceRoll==1){
Body=addBody(Body);
}
else if (diceRoll==2 && Body==1){
Head=addHead(Body, Head);
}
else if (diceRoll==3 && Body==1 && Head==1){
Antenna=addAntenna(Body, Head, Antenna);
}
else if (diceRoll==4 && Body==1){
Wings=addWings(Body,Wings);
}
else if (diceRoll==5 && Body==1){
Stinger=addStinger(Body, Stinger);
}
else if (diceRoll==6 && Body==1){
Legs=addLegs(Body, Legs);
}
else if (diceRoll==7 && Body==1 && Head==1){
Eyes=addEyes(Body, Head, Eyes);
}
}
//PrintCootie
void PrintCootie(int Body, int Head, int Antenna, int Wings, int Stinger, int Legs, int Eyes)
{
cout<<"Cootie has "<<endl;
if(Body<=1){
cout<<Body<<" Body "<<endl;
}
if(Head<=1){
cout<<Head<<" Head "<<endl;
}
if(Antenna<=2){
cout<<Antenna<<" Antenna(s) "<<endl;
}
if(Wings<=2){
cout<<Wings<<" Wing(s) "<<endl;
}
if(Stinger<=1){
cout<<Stinger<<" Stinger "<<endl;
}
if(Legs <=4){
cout<<Legs<<" Leg(s) "<<endl;
}
if(Eyes <=6){
cout<<Eyes<<" Eye(s) "<<endl;
}
}
int CootieComplete()
{
if(Body==1 && Head==1 && Antenna==2 && Wings==2 && Stinger==1 && Legs==4 && Eyes==6){
cout<<"cootie complete"<<endl;
return 1;
}
else {
return 0;
}
}
int main()
{
char ch;
do
{
Body=0, Head=0, Antenna=0, Wings=0, Stinger=0, Legs=0, Eyes=0;
do{
diceRoll=rollDice();
applyRoll(diceRoll);
PrintCootie(Body, Head, Antenna, Wings, Stinger, Legs, Eyes);
}
while (!CootieComplete());
cout<<" Play Again? [y/n]";
cin>>ch;
}
while (ch=='y');
getch();
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------
If the Roll is: You can add this Body Part if you already have this part Maximum Number you may have. Also the number required to finish the cootie 1 Body none 1 2 Head Body 1 3 Antenna Head 2 4 Wings Body 2 5 Stinger Body 1 6 Legs Body 4 7 Eyes Head 6Explanation / Answer
I have corrected your code. Now working fine.
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<stdio.h>
using namespace std;
int diceRoll =0, Body = 0, Head = 0, Antenna = 0, Wings = 0, Stinger = 0, Legs = 0, Eyes = 0;
int rollDice() {
int rand_no= (rand()%7);
int r=rand_no+1;
cout<<" You rolled a "<<r<<endl;
return r;
}
void addBody(){
if(Body<1) {
Body++;
}
}
void addHead(){
if(Head<1 && Body==1){
Head++;
}
}
void addAntenna(){
if(Antenna<2 &&Body==1 &&Head==1){
Antenna++;
}
}
void addWings(){
if(Wings<2 &&Body==1){
Wings++;
}
}
void addStinger(){
if(Stinger<1 &&Body==1){
Stinger++;
}
}
void addLegs(){
if(Legs<4 &&Body==1){
Legs++;
}
}
void addEyes(){
if(Eyes<6 &&Body==1 &&Head==1){
Eyes++;
}
}
void applyRoll(int diceRoll){
if (diceRoll==1){
addBody();
}
else if (diceRoll==2 && Body==1){
addHead();
}
else if (diceRoll==3 && Body==1 && Head==1){
addAntenna();
}
else if (diceRoll==4 && Body==1){
addWings();
}
else if (diceRoll==5 && Body==1){
addStinger();
}
else if (diceRoll==6 && Body==1){
addLegs();
}
else if (diceRoll==7 && Body==1 && Head==1){
addEyes();
}
}
//PrintCootie
void PrintCootie()
{
cout<<"Cootie has "<<endl;
if(Body<=1){
cout<<Body<<" Body "<<endl;
}
if(Head<=1){
cout<<Head<<" Head "<<endl;
}
if(Antenna<=2){
cout<<Antenna<<" Antenna(s) "<<endl;
}
if(Wings<=2){
cout<<Wings<<" Wing(s) "<<endl;
}
if(Stinger<=1){
cout<<Stinger<<" Stinger "<<endl;
}
if(Legs <=4){
cout<<Legs<<" Leg(s) "<<endl;
}
if(Eyes <=6){
cout<<Eyes<<" Eye(s) "<<endl;
}
}
int CootieComplete()
{
if(Body==1 && Head==1 && Antenna==2 && Wings==2 && Stinger==1 && Legs==4 && Eyes==6){
cout<<"cootie complete"<<endl;
return 1;
}
else {
return 0;
}
}
int main()
{
char ch;
do
{
Body=0, Head=0, Antenna=0, Wings=0, Stinger=0, Legs=0, Eyes=0;
do{
diceRoll=rollDice();
applyRoll(diceRoll);
PrintCootie();
//cin>>ch;
}
while (!CootieComplete());
cout<<" Play Again? [y/n]";
cin>>ch;
}
while (ch=='y');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.