-
Hi,
So I currently have the following.
But it does not work because I have not set up the array of forcevertex[j] properly.
How do I work that?
Thanks
Code:
#include <iostream>
int main (void) {
/* Variables */
float Xreal;
float Yreal;
float Zreal;
float Xforce;
float Yforce;
float Zforce;
float Tval;
char inZforce[64];
int check_input;
char input_buffer[160];
int wloop = 8;
int j; /* vertex number */
char forcevertex0[300];
char forcevertex1[300];
char forcevertex2[300];
char forcevertex3[300];
char forcevertex4[300];
char forcevertex5[300];
char forcevertex6[300];
char forcevertex7[300];
/* Prompts */
/* Loop */
for(wloop = 7; wloop >= 0; wloop--)
{
/* Set vertex-number for loop pass */
j=8-wloop;
/* Get coordinates of real */
printf("Real-Point values in (x, y, z) ");
fgets(input_buffer, sizeof input_buffer, stdin);
check_input = sscanf(input_buffer, "(%f,%f,%f)", &Xreal, &Yreal, &Zreal);
while (check_input !=3) {
//Improper format
printf("Improper format.");
return(0);
};
/* Get Z coordinate of forced */
printf("Zforce value: ");
fgets(inZforce, sizeof inZforce, stdin);
Zforce=strtof(inZforce, NULL);
/* Computation */
Tval=Zforce/Zreal;
Xforce=Tval*Xreal;
Yforce=Tval*Yreal;
/* Output Display */
printf(" \n");
printf("Forced Point: (%.3f, %.3f, %.3f) \n",Xforce,Yforce,Zforce);
printf("Real Point: (%.3f, %.3f, %.3f) \n",Xreal,Yreal,Zreal);
printf("T-value: %.3f \n",Tval);
printf(" \n");
/* Save vertex information before going to next one */
forcevertex[j]=("Vertex %i: Forced: (%.3f, %.3f, %.3f) Real: (%.3f, %.3f, %.3f) \n",j,Xforce,Yforce,Zforce,Xreal,Yreal,Zreal);
} /* End Loop */
/* Final Display */
printf("Vertex creation complete. \n");
/* Then display Force-Vertex 0-7 coords */
printf("%s \n",forcevertex0);
printf("%s \n",forcevertex1);
printf("%s \n",forcevertex2);
printf("%s \n",forcevertex3);
printf("%s \n",forcevertex4);
printf("%s \n",forcevertex5);
printf("%s \n",forcevertex6);
printf("%s \n",forcevertex7);
return(0);
}
-
Sorry, to include the code again.
I'm trying to figure out how the arrays will work here but am having a problem
because when I define the variable forcevertex[]
I need it to be a char string containing the following information:
Code:
forcevertex[j]=("Vertex %i: Forced: (%.3f, %.3f, %.3f) Real: (%.3f, %.3f, %.3f) \n",j,Xforce,Yforce,Zforce,Xreal,Yreal,Zreal);
But if I define the variable char forcevertex[8], trying to account for the 8 vertices that will be created, the program will interpret the 8 as the string length rather than the array.
So, I'm confused.
Help please?
Thanks so much.
Code:
#include <iostream>
int main (void) {
/* Variables */
float Xreal;
float Yreal;
float Zreal;
float Xforce;
float Yforce;
float Zforce;
float Tval;
char inZforce[64];
int check_input;
char input_buffer[160];
int wloop = 8;
int j; /* vertex number */
char forcevertex[8];
/* Prompts */
/* Loop */
for(wloop = 7; wloop >= 0; wloop--)
{
/* Set vertex-number for loop pass */
j=8-wloop;
/* Get coordinates of real point */
printf("Real-Point values in (x, y, z) ");
fgets(input_buffer, sizeof input_buffer, stdin);
check_input = sscanf(input_buffer, "(%f,%f,%f)", &Xreal, &Yreal, &Zreal);
while (check_input !=3) {
//Improper format
printf("Improper format.");
return(0);
};
/* Get Z coordinate of forced point */
printf("Zforce value: ");
fgets(inZforce, sizeof inZforce, stdin);
Zforce=strtof(inZforce, NULL);
/* Computation */
Tval=Zforce/Zreal;
Xforce=Tval*Xreal;
Yforce=Tval*Yreal;
/* Output Display */
printf(" \n");
printf("Forced Point: (%.3f, %.3f, %.3f) \n",Xforce,Yforce,Zforce);
printf("Real Point: (%.3f, %.3f, %.3f) \n",Xreal,Yreal,Zreal);
printf("T-value: %.3f \n",Tval);
printf(" \n");
/* Save vertex information before going to next one */
forcevertex[j]=("Vertex %i: Forced: (%.3f, %.3f, %.3f) Real: (%.3f, %.3f, %.3f) \n",j,Xforce,Yforce,Zforce,Xreal,Yreal,Zreal);
} /* End Loop */
/* Final Display */
printf("Vertex creation complete. \n");
/* Then display Force-Vertex 0-7 coords */
printf("%s \n",forcevertex[0]);
printf("%s \n",forcevertex[1]);
printf("%s \n",forcevertex[2]);
printf("%s \n",forcevertex[3]);
printf("%s \n",forcevertex[4]);
printf("%s \n",forcevertex[5]);
printf("%s \n",forcevertex[6]);
printf("%s \n",forcevertex[7]);
return(0);
}
-
Perhaps I need to introduce a pointer?
-
1. #include <stdio.h>
<iostream> is for C++
2.
use struct
Code:
struct vertex
{
float Xreal;
float Yreal;
float Zreal;
float Xforce;
float Yforce;
float Zforce;
float Tval;
};
3. Define array of structs
Code:
struct vertex data[8];
in a loop initialize the corresponding element of the array
Code:
for(j=0;j<8;j++)
{
...
check_input = sscanf(input_buffer, "(%f,%f,%f)", &data[j].Xreal, &data[j].Yreal, &data[j].Zreal);
...
}
After the loop is complited use another loop to print data
Code:
for(j=0;j<8;j++)
{
printf("Vertex %i: Forced: (%.3f, %.3f, %.3f) Real: (%.3f, %.3f, %.3f) \n",
j,data[j].Xforce,data[j].Yforce,data[j].Zforce,data[j].Xreal,data[j].Yreal,data[j].Zreal);
}
-
Hi, thanks.
But I'm not sure if I understood where the j loop is placed. In the code below, an error occurs that "Xreal, Yreal, Zreal, etc were not defined in this scope"
What is wrong with this?
Code:
#include <stdio.h>
int main (void) {
/* Variables */
struct vertex
{
float Xreal;
float Yreal;
float Zreal;
float Xforce;
float Yforce;
float Zforce;
float Tval;
};
char inZforce[64];
int check_input;
char input_buffer[160];
int wloop = 8;
int j; /* vertex number */
char forcevertex[8];
/* Define struct array */
struct vertex data[8];
/* Prompts */
j=8-wloop;
/* Set vertex-number for loop pass */
for(j=0;j<8;j++)
{
/* Loop */
for(wloop = 7; wloop >= 0; wloop--)
{
/* Get coordinates of real point */
printf("Real-Point values in (x, y, z) ");
fgets(input_buffer, sizeof input_buffer, stdin);
check_input = sscanf(input_buffer, "(%f,%f,%f)", &Xreal, &Yreal, &Zreal);
while (check_input !=3) {
//Improper format
printf("Improper format.");
return(0);
};
/* Get Z coordinate of forced point */
printf("Zforce value: ");
fgets(inZforce, sizeof inZforce, stdin);
Zforce=strtof(inZforce, NULL);
/* Computation */
Tval=Zforce/Zreal;
Xforce=Tval*Xreal;
Yforce=Tval*Yreal;
/* Output Display */
printf(" \n");
printf("Forced Point: (%.3f, %.3f, %.3f) \n",Xforce,Yforce,Zforce);
printf("Real Point: (%.3f, %.3f, %.3f) \n",Xreal,Yreal,Zreal);
printf("T-value: %.3f \n",Tval);
printf(" \n");
/* Save vertex information before going to next one */
forcevertex[j]=("Vertex %i: Forced: (%.3f, %.3f, %.3f) Real: (%.3f, %.3f, %.3f) \n",j,Xforce,Yforce,Zforce,Xreal,Yreal,Zreal);
} /* End Loop */
}
/* Final Display */
printf("Vertex creation complete. \n");
/* Then display Force-Vertex 0-7 coords */
for(j=0;j<8;j++)
{
printf("Vertex %i: Forced: (%.3f, %.3f, %.3f) Real: (%.3f, %.3f, %.3f) \n",j,data[j].Xforce,data[j].Yforce,data[j].Zforce,data[j].Xreal,data[j].Yreal,data[j].Zreal,data[j]);
}
return(0);
}
-
Sorry, I fixed
Code:
check_input = sscanf(input_buffer, "(%f,%f,%f)", &Xreal, &Yreal, &Zreal);
to instead be
Code:
check_input = sscanf(input_buffer, "(%f,%f,%f)", &data[j].Xreal, &data[j].Yreal, &data[j].Zreal);
but now
zforce, strtof, Tval, Zreal, Xforce, Xreal, Yforce, Yreal
are still not "declared in this scope"
why is strtof a part of that?
-
Code:
#include <stdio.h>
#include <stdlib.h> /* for strtod */
/* Data type definition */
struct vertex
{
float Xreal;
float Yreal;
float Zreal;
float Xforce;
float Yforce;
float Zforce;
float Tval;
};
int main (void) {
/* Variables */
char inZforce[64];
int check_input;
char input_buffer[160];
int j; /* vertex number */
/* struct array */
struct vertex data[8];
/* Prompts */
/* Set vertex-number for loop pass */
for(j=0;j<8;j++)
{
/* Get coordinates of real point */
printf("Real-Point values in (x, y, z) ");
fgets(input_buffer, sizeof input_buffer, stdin);
check_input = sscanf(input_buffer, "(%f,%f,%f)",
&data[j].Xreal, &data[j].Yreal, &data[j].Zreal);
while (check_input !=3) {
//Improper format
printf("Improper format.");
return(0);
};
/* Get Z coordinate of forced point */
printf("Zforce value: ");
fgets(inZforce, sizeof inZforce, stdin);
data[j].Zforce=(float)strtod(inZforce, NULL);
/* Computation */
data[j].Tval=data[j].Zforce/data[j].Zreal;
data[j].Xforce=data[j].Tval*data[j].Xreal;
data[j].Yforce=data[j].Tval*data[j].Yreal;
/* Output Display */
printf(" \n");
printf("Forced Point: (%.3f, %.3f, %.3f) \n",
data[j].Xforce,data[j].Yforce,data[j].Zforce);
printf("Real Point: (%.3f, %.3f, %.3f) \n",
data[j].Xreal,data[j].Yreal,data[j].Zreal);
printf("T-value: %.3f \n",
data[j].Tval);
printf(" \n");
} /* End Loop */
/* Final Display */
printf("Vertex creation complete. \n");
/* Then display Force-Vertex 0-7 coords */
for(j=0;j<8;j++)
{
printf("Vertex %i: Forced: (%.3f, %.3f, %.3f) Real: (%.3f, %.3f, %.3f) \n",
j,
data[j].Xforce,data[j].Yforce,data[j].Zforce,
data[j].Xreal,data[j].Yreal,data[j].Zreal);
}
return(0);
}
Pay attention to the diferencies
-
thanks
vart,
your approach was perfect and I now am beginning to understand the idea. Thanks so much!
-
Just check user input more thoroughly.
Real-Point values in (x, y, z) (2.3,4.5,6.a)
Zforce value: 2.D
Forced Point: (0.767, 1.500, 2.000)
Real Point: (2.300, 4.500, 6.000)
T-value: 0.333
Real-Point values in (x, y, z) (2.3,4.5,6.a)
Zforce value: D.2
Forced Point: (0.000, 0.000, 0.000)
Real Point: (2.300, 4.500, 6.000)
T-value: 0.000
Real-Point values in (x, y, z)
Improper format.
-
hmm
slinger,
how do you mean exactly?
for right now im satisfied if the program works for correctly formatted inputs because this is sorta an intermediate stage, eventually i will be cutting out the middle man of a user to have the computer provide inputs from another element of the program. but I would like to know how I can have the program check the format nonetheless.
thanks