What is a better way to code this?

I am currently trying to code a program that chooses relevant questions based upon yes/no (1/0) answers to a couple of other questions. It is for observations made on chemical reactions, and the questions ask if there was access to different chemicals, so that it will not ask questions about the one's that had no access.

I currently have it so that if the value for a given substance is set to zero, and the RNG picks the number of a question involving that substance, it will add one to the random number until a question that does not involve a substance that was not used. I personally entered the numbers of the questions it could not be, but I don't think the codes that I used work properly.

If anyone could suggest a better way I would be eternally grateful. Thank you.

Below is parts of the code in question:
{ //Process for Randomquestion1
Randomquestion = rand()%51+1;
Randomquestion1=Randomquestion;
if (distilledwater==0 or HCl==0 or NaOH==0)
{ do {Randomquestion1=Randomquestion1+1;} while (distilledwater ==0 and Randomquestion==6 or Randomquestion==11 or Randomquestion==15 or Randomquestion==20 or Randomquestion==23 or Randomquestion==24 or Randomquestion==29 or Randomquestion==32 or Randomquestion==35 or Randomquestion==38 or Randomquestion==44 or Randomquestion==51);
do {Randomquestion1=Randomquestion1+1;} while (HCl ==0 and Randomquestion==7 or Randomquestion==9 or Randomquestion==17 or Randomquestion==25 or Randomquestion==30 or Randomquestion==33 or Randomquestion==36 or Randomquestion==40 or Randomquestion==41 or Randomquestion==43 or Randomquestion==18 or Randomquestion==21 or Randomquestion==45);
do {Randomquestion1=Randomquestion1+1;} while (NaOH ==0 and Randomquestion==8 or Randomquestion==10 or Randomquestion==16 or Randomquestion==26 or Randomquestion==31 or Randomquestion==34 or Randomquestion==37 or Randomquestion==39 or Randomquestion==42 or Randomquestion==46 or Randomquestion==22 or Randomquestion==19);
}
cout << "1.";
{ //List of randomquantity quantity and question asked
if (Randomquestion==1) {cout << "What color was the powder?\n";}
if (Randomquestion==2) {cout << "What was the shape and relative size of the particles of the powder?\n";}
if (Randomquestion==3) {cout << "Would you describe the structure of the powder as amorphic, uniform crystalline or irregular crystalline?\n ";}
if (Randomquestion==4) {cout << "Did the substance have and odor, if so what was it like?\n";}
if (Randomquestion==5) {cout << "Describe the luster of the powder.\n";}
if (Randomquestion==6) {cout << "Did the water and powder solution have and odor, if so what was it like?\n";}
if (Randomquestion==7) {cout << "Did the HCl and powder solution have and odor, if so what was it like?\n";}
if (Randomquestion==8) {cout << "Did the NaOH and powder solution have and odor, if so what was it like?\n";}
if (Randomquestion==9) {cout << "How did the powder and HCl solution look?\n";}
if (Randomquestion==10) {cout << "How did the powder and NaOH solution look?\n";}
if (Randomquestion==11) {cout << "How did the powder and water look?\n";}
if (Randomquestion==12) {cout << "What was the density of the powder?\n";}
if (Randomquestion==13) {cout << "What was the initial mass of the powder?\n";}
if (Randomquestion==14) {cout << "What was the initial volume of the powder?\n";}
if (Randomquestion==15) {cout << "What was the volume of water given?\n";}
if (Randomquestion==16) {cout << "What was the volume of NaOH given?\n";}
if (Randomquestion==17) {cout << "What was the volume of HCl given?\n";}
if (Randomquestion==18) {cout << "Did the powder react with the acid to create a gas?\n";}
if (Randomquestion==19) {cout << "Did the powder react with the base to create a gas?\n";}
if (Randomquestion==20) {cout << "Did the powder react with water to create a gas?\n";}
if (Randomquestion==21) {cout << "Did the powder react with the acid in any way?\n";}
if (Randomquestion==22) {cout << "Did the powder react with the base in any way?\n";}
if (Randomquestion==23) {cout << "Did the powder react with the water in any way?\n";}
if (Randomquestion==24) {cout << "Is the powder soluble in water?\n";}
if (Randomquestion==25) {cout << "Is the powder soluble in HCl?\n";}
if (Randomquestion==26) {cout << "Is the powder soluble in NaOH?\n";}
if (Randomquestion==27) {cout << "Was the powder hygroscopic?\n";}
if (Randomquestion==28) {cout << "Was the powder hydrophobic or hydrophilic?\n";}
if (Randomquestion==29) {cout << "What is the pH of the powder and water solution?\n";}
if (Randomquestion==30) {cout << "What is the pH of the HCl?\n";}
if (Randomquestion==31) {cout << "What is the pH of the NaOH?\n";}
if (Randomquestion==32) {cout << "What is the pH of the water?\n";}
if (Randomquestion==33) {cout << "What is the pH of the powder and HCl solution?\n";}
if (Randomquestion==34) {cout << "What is the pH of the powder and NaOH solution?\n";}
if (Randomquestion==35) {cout << "Did the water conduct a current?\n";}
if (Randomquestion==36) {cout << "Did the HCl conduct a current?\n";}
if (Randomquestion==37) {cout << "Did the NaOH conduct a current?\n";}
if (Randomquestion==38) {cout << "Did the powder and water solution conduct a current?\n";}
if (Randomquestion==39) {cout << "Did the powder and NaOH solution conduct a current?\n";}
if (Randomquestion==40) {cout << "Did the powder and HCl solution conduct a current?\n";}
if (Randomquestion==41) {cout << "What was the temperature of the HCl before the powder was added?\n";}
if (Randomquestion==42) {cout << "What was the temperature of the NaOH before the powder was added?\n";}
if (Randomquestion==43) {cout << "What was the temperature of the HCl before the powder was added?\n";}
if (Randomquestion==44) {cout << "Was there a temperature change when the powder was added to the water? Was the reaction exothermic or endothermic\n?";}
if (Randomquestion==45) {cout << "Was there a temperature change when the powder was added to the HCl? Was the reaction exothermic or endothermic?\n";}
if (Randomquestion==46) {cout << "Was there a temperature change when the powder was added to the NaOH? Was the reaction exothermic or endothermic?";}
if (Randomquestion==47) {cout << "Did a precipitate form in any of your tests? If so, what color was the precipitate?\n";}
if (Randomquestion==48) {cout << "Describe the luster of the powder.\n";}
if (Randomquestion==49) {cout << "Is the powder a covalent or ionic compound?\n";}
if (Randomquestion==50) {cout << "Would the powder be classified as acidic, neutral, or basic?\n";}
if (Randomquestion==51) {cout << "Is the density of the powder greater than or less than that of water?\n";}}
;}
i guess you should put all the "or" statements in each parenthesis in a separate one. that is do (A and (B or C or D or ...)). i'm just assuming this is what you want.

also, throwing all the if statements after cin<<"1." into a container like a string vector, it would be nice and easier to read. you still have a lot to do initializing the vector. if anyone have a better idea, please let us know.
Use an array literal, like
char *messages[52]= {"What color was the powder?\n", /*put all the messages in here*/};
This way the array can be intiialized at compile time. Then write one statement:
cout << messages[Randomquestion];
This will both compile and run faster.
Last edited on
Topic archived. No new replies allowed.