Ok so I have to write this program where it will list five different computations. The program doesn't have to run those computations, it just has to list them. The catch is, when someone selects a computation, the program is suppose to remove that computation from the list, then recall all the computations in order the user defined. Here is what I have so far
#include <stdio.h>
void main()
{
int comp,x;
printf("This is a program for selecting computations.\n\nEnter number of computations [0-5]:");
scanf("%i",&comp);
while (comp > 5)
{
printf("\tError! %i is not a valid entry.\n\nEnter number of computations [0-5]:",comp);
scanf("%i",&comp);
}
do
{
printf("\n\t1 - Surface area of a cylinder \n\t2 - Surface area of a cone \n\t3 - Radius of a circle sector \n\t4 - Volume of a cylinder \n\t5 - Volume of a sphere");
printf("\n\nSelect a computation:");
scanf("%i",&x);
}
while (comp!=0);
if (comp==0)
{
printf("\nEnd program");
}
}
I would just like help to get started or how I can approach this. Thanks!
#include <stdio.h>
#include <string>
int main()
{
string computation[5]={"Surface area of a cylinder", "Surface area of a cone ", "Radius of a circle sector", "Volume of a cylinder", "Volume of a sphere"}
int comp;
printf("This is a program for selecting computations.\n\nEnter number of computations [0-5]:");
scanf("%i",&comp);
while (comp > 5)
{
printf("\tError! %i is not a valid entry.\n\nEnter number of computations [0-5]:",comp);
scanf("%i",&comp);
}
while(comp!=0)
{
printf("\n");
int counter=1;
for(int i=0; i<comp; i++) {
if(computation[i]!=NULL) {
printf(" %i ", counter); //this will display "1 " "2 " "3 ", etc
printf(" %i", computation[i];
printf("\n");
counter++;
}
}
scanf("%i",&comp);
computation[comp-1]=NULL; //example you remove choice nr2 = computation[1]
}
printf("\nEnd program");
return 0;
Ok that's cool only I'm dumb and I forgot to mention. I can only use while, do while, for loops, if and switch statements and no arrays or strings. =( I think I can use structures but I doubt it.
#include <stdio.h>
#include <string>
void main()
{
string comps="fffff"; //string containing values -true or false
int comp,x;
printf("This is a program for selecting computations.\n\nEnter number of computations [0-5]:");
scanf("%i",&comp);
while (comp<0 && comp > 5) //make sure its between bounds
{
printf("\tError! %i is not a valid entry.\n\nEnter number of computations [0-5]:",comp);
scanf("%i",&comp);
}
for(int i=0;i<comp;i++)
{
comps[i]='t';
do
{
if(comp[0]=='t') print "sa of cylinder"if(comp[1]=='t') print "sa of cone"if(comp[2]=='t') print "r of circle"
.....
/*
printf("\n\t1 - Surface area of a cylinder \n\t2 - Surface area of a cone \n\t3 - Radius of a circle sector \n\t4 - Volume of a cylinder \n\t5 - Volume of a sphere");*/
printf("\n\nSelect a computation:");
scanf("%i",&x);
}
while (comp!=0);
//if (comp==0) <-- you do not need this since comp has to be zero
{
printf("\nEnd program");
}
}
this is pretty hard with just basic things... you could just have a lot of if statements but it is not good coding
the strings used are still used as arrays
you could spell it out like 'if(comp=3) comp1=true; comp2=true; comp3=true;... and so on but arrays are a very important part of programming :P
Thanks guys! I'll keep working on it. If you have any other ideas let me know.
and to intmain,
I know arrays are important, and it would be easier if i could, but school projects limited me to this so i was very confused on how to do it.
class computations
{
private:
bool comp1=false;
bool comp2=false; //i don't think you can actually initialize here --> constructor
...
bool comp5=false;
string comp1str,comp2str,comp3str,comp4str,comp5str;
public:
computations(){};
//add another function to set the strings equal to the names of the computations
void setTrue(int compNum) //when initializing the number of comps
{
switch(compNum)
{
case 1: comp1=true; break;
case 2: comp2=true; break;
...
case 5: comp5=true; break;
}
};
void setFalse(int compNum) //after selecting computations
{
switch(compNum) { //same as above}
};
void print()
{
if(comp1) print comp1str;
if(comp2) print comp2str; //and so on...
}
};