program looping with final output

This is a discussion on program looping with final output within the C Programming forums, part of the General Programming Boards category; Hi, So I currently have the following. But it does not work because I have not set up the array ...

  1. #16
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    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, "(&#37;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);
    }

  2. #17
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    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 &#37;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);
    }

  3. #18
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Perhaps I need to introduce a pointer?

  4. #19
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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, "(&#37;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);	
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #20
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    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, "(&#37;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);
    }

  6. #21
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Sorry, I fixed
    Code:
    check_input = sscanf(input_buffer, "(&#37;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?

  7. #22
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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, "(&#37;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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #23
    Registered User
    Join Date
    Feb 2008
    Posts
    84

    thanks

    vart,
    your approach was perfect and I now am beginning to understand the idea. Thanks so much!

  9. #24
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    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.

  10. #25
    Registered User
    Join Date
    Feb 2008
    Posts
    84

    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

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling an external program + capture output?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 03-21-2008, 12:49 AM
  2. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 05:02 PM
  3. Ideas for FINAL program?
    By ray in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2002, 02:46 PM
  4. My menu program won't stop looping
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-26-2002, 03:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21