The program i am writing takes in 6 strings a0,b0,c0,x0,y0 and z0. It then converts them to doubles a,b,c,x,y,z using the atof function. if the user types "f" then it goes to the divide function that returns a float so there is no need for the atof.
I am trying to create a for loop to cut down unnecessary code
Instead of:
Code:
printf("A = ");
scanf("%s",a0);
if (strcmpi("f",a0)==0)
{
a=divide();
}
else
{
a=atof(a0);
}
For a,b,c,x,y,z^^^^^
I would like something like this:
Code:
for (i=0;i<6;i++)
{
if (i==0) {
sprintf(variable,'a');
sprintf(variable0,'a0');
}
if (i==1) {
sprintf(variable,'b');
sprintf(variable0,'b0');
}
if (i==2) {
sprintf(variable,'c');
sprintf(variable0,'c0');
}
if (i==3) {
sprintf(variable,'x');
sprintf(variable0,'x0');
}
if (i==4) {
sprintf(variable,'y');
sprintf(variable0,'y0');
}
if (i==5) {
sprintf(variable,'z');
sprintf(variable0,'a0');
}
printf("\n %s = ", variable);
scanf("%s",variable0);
if (strcmpi("f",*variable0)==0)
{
*variable=divide();
}
else
{
*variable=atof(variable0);
}
}
I know the code is quite bad but hopefully you can see what i am trying to do. I am literally trying to replace the word "variable" with a,b,c,x,y,z and i know it won't work. I had a go with pointers too - it got the program running but it crashes straight away.
Thanks for all the help,
Jez