can anyone help me with this switch statement? i want it to go through line by line my file and, in the cases, increment x, y and z counts depending on the value of each digit in the strings. i debugged and its giving me some stupid value for xcount. dont know if its infinite or not. anyone help?

// Allocate memory to hold the vertices
	g_pVertices = new CVector3 [ g_NumberOfAdds ];

	//create counter index for array
             int index = 0;
	char szBuf[256];
	while( fgets(szBuf,256,fp) )
    { 
		int nLineLen = strlen(szBuf);
		for ( int i = 0; i < nLineLen; i++ )
		{	 
		float xCount = 0;
		float yCount = 0;
		float zCount = 0;
		
		switch( szBuf[i] )
		{  
		default: //  don't do anything
		break;

		case '0':  //default
				break;
		
		case '1': xCount++;
				break;

		case '2': yCount++;
				break;

		case '3': xCount++;
				  yCount++;
				break;

		case '4': zCount++;
				break;

		case '5': xCount++;
				  zCount++;
				break;

		case '6': yCount++;
				  zCount++;
				break;

		case '7': xCount++;
				  yCount++;
				  zCount++;
				break;
		}
		xCount = g_pVertices[index].x;
		yCount = g_pVertices[index].y;
		zCount = g_pVertices[index].z;
		index++;
   	}
  }

I think you are ignoring what happens in the switch.

float xCount = 0;
		float yCount = 0;
		float zCount = 0;
		
		switch( szBuf[i] )
		{
		   /* ... */
		}
		xCount = g_pVertices[index].x;
		yCount = g_pVertices[index].y;
		zCount = g_pVertices[index].z;

Before the switch, you initialize them all to zero. After the switch, you discard the calculated values and assign new values.

Is this something similar to what you are trying to do?

#include <stdio.h>
#include <string.h>

int main()
{
   char szBuf[256] = "003124";
   int i, index = 0, nLineLen = strlen(szBuf);
   float xCount = 0, yCount = 0, zCount = 0;
   for ( i = 0; i < nLineLen; i++ )
   {
      switch ( szBuf[i] )
      {
      default:                                break;
      case '0':                               break;
      case '1': xCount++;                     break;
      case '2':           yCount++;           break;
      case '3': xCount++; yCount++;           break;
      case '4':                     zCount++; break;
      case '5': xCount++;           zCount++; break;
      case '6':           yCount++; zCount++; break;
      case '7': xCount++; yCount++; zCount++; break;
      }
      //xCount = g_pVertices[index].x;
      //yCount = g_pVertices[index].y;
      //zCount = g_pVertices[index].z;
      index++;
      printf("xCount = %g, yCount = %g, zCount = %g\n", xCount, yCount, zCount);
   }
   return 0;
}

/* my output
xCount = 0, yCount = 0, zCount = 0
xCount = 0, yCount = 0, zCount = 0
xCount = 1, yCount = 1, zCount = 0
xCount = 2, yCount = 1, zCount = 0
xCount = 2, yCount = 2, zCount = 0
xCount = 2, yCount = 2, zCount = 1
*/

but i need to scan each line and extract 3 values from each. an x y and z value. (point plotting later.) and i want these held in an array. is this possible? or am i being totally shortsighted? sorry but im relatively new.

>but i need to scan each line and extract 3 values from each.

If this is because I used hard-coded text, that is because I didn't feel like reading your mind to get the contents of the text file, and doing the file handling you didn't post.

However you get the line of text, there is text. The part I was concentrating on was the handling of this text, however it got there. I'll try to add color to my previous post.


Either that, or I'm just confused.

think im confused too. sorry i didnt explain so well. the file handling is ok. ive got a string count which matches the amount of strings in the file. the purpose of this program is to read in the strings (which are addresses) and convert them to cartesian coordinates, then hold them in an array for plotting later on. where each member of the array has 3 values vertex.x vertex.y and vertex.z for example. so an address of 00111 would have x = 3, y=0, z=0. does that help?

Does this help?

#include <stdio.h>
#include <string.h>

int main()
{
   char *szBuf[] = {"00111","003124"};
   struct vertex { int x, y, z; } v [ sizeof szBuf / sizeof *szBuf ] = {0};
   size_t i, j, nLineLen;
   for ( j = 0; j < sizeof szBuf / sizeof *szBuf; ++j )
   {
      for ( i = 0, nLineLen = strlen(szBuf[j]); i < nLineLen; i++ )
      {
         switch ( szBuf[j][i] )
         {
         default:                                break;
         case '0':                               break;
         case '1': v[j].x++;                     break;
         case '2':           v[j].y++;           break;
         case '3': v[j].x++; v[j].y++;           break;
         case '4':                     v[j].z++; break;
         case '5': v[j].x++;           v[j].z++; break;
         case '6':           v[j].y++; v[j].z++; break;
         case '7': v[j].x++; v[j].y++; v[j].z++; break;
         }
      }
      printf("%s: v[j].x = %d, v[j].y = %d, v[j].z = %d\n",
             szBuf[j], v[j].x, v[j].y, v[j].z);
   }
   return 0;
}

/* my output
00111: v[j].x = 3, v[j].y = 0, v[j].z = 0
003124: v[j].x = 2, v[j].y = 2, v[j].z = 1
*/

[edit]C code with some file handling:

/* file.txt
00111
003124
*/
#include <stdio.h>
#include <string.h>

int main(void)
{
   static const char filename[] = "file.txt";
   FILE *file = fopen(filename, "r");
   if ( file != NULL )
   {
      struct vertex { int x, y, z; } v [ 5 ] = {0};
      size_t i, j, len;
      char line[256];
      for ( j = 0; j < sizeof v / sizeof *v ; ++j )
      {
         if ( !fgets(line, sizeof line, file) )
         {
            break;
         }
         for ( i = 0, len = strlen(line); i < len; i++ )
         {
            switch ( line[i] )
            {
            default:                                break;
            case '0':                               break;
            case '1': v[j].x++;                     break;
            case '2':           v[j].y++;           break;
            case '3': v[j].x++; v[j].y++;           break;
            case '4':                     v[j].z++; break;
            case '5': v[j].x++;           v[j].z++; break;
            case '6':           v[j].y++; v[j].z++; break;
            case '7': v[j].x++; v[j].y++; v[j].z++; break;
            }
         }
         printf("%s: v[j].x = %d, v[j].y = %d, v[j].z = %d\n",
                line, v[j].x, v[j].y, v[j].z);
      }
   }
   return 0;
}

/* my output
00111: v[j].x = 3, v[j].y = 0, v[j].z = 0
003124: v[j].x = 2, v[j].y = 2, v[j].z = 1
*/
This article has been dead for over six months. Start a new discussion instead.