i'm currently writing a program that needs one small piece in my opinion in order to work. i have an array which reads in a bunch of numbers (call them addresses, an example is "003124") and counts the amount of addresses at the moment. i need this to be passed into a switch statement that does certain things for each number in each address, i've written a mix of code/pseudocode

entil EOF do;
        string = input;
        len = length(string);
          for i=1 to len do
              ch = substr(string, i-1, 1)
                switch(ch)
                 //case statements

i want each "string" in the input to be a single address. does anyone know how to do this? i'll provide more information if needed.
thanks in advance.

Recommended Answers

All 8 Replies

First, I have grown to hate the "while not at end of file" stuff; I now prefer "while successfully obtaining the data I request from the file" approach. Anyways, here is a possible starting point.

/* file.txt
003124
*/
#include <iostream>
#include <fstream>
#include <string>

int main ()
{
   std::ifstream file("file.txt");
   std::string input;
   while ( std::getline(file, input) )
   {
      for ( std::string::size_type i = 0, len = input.size(); i < len; ++i )
      {
         switch ( input[i] )
         {
         case '0': std::cout << "zero"  << '\n'; break;
         case '1': std::cout << "one"   << '\n'; break;
         case '2': std::cout << "two"   << '\n'; break;
         case '3': std::cout << "three" << '\n'; break;
         default:  std::cout << "other" << '\n'; break;
         }
      }
   }
   return 0;
}

/* my output
zero
zero
three
one
two
other
*/

>I have grown to hate the "while not at end of file" stuff
Me too. Mostly because in C and C++ it's a subtle and dangerous error.

ok well since i've counted all the addresses in a previous function call it EOF or "while count < Number of addresses". thats not the part thats bothering me. the purpose of the switch statements is to set up a couple of variables, say xCount YCount and zCount and the case statements will increment 1,2 or all 3 of these depending on the number found. then be able to store these in an array. like so

switch(addresses[i])
		{  
		default: //  don't do anything
		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++;
	}
}

would something like this work?

const int MAX_ADDRESSES = g_NumberOfAdds;
	StrType word;
	ifstream fp;
	StrType words[MAX_ADDRESSES];
	int numAdds = 0;
	
	word.MakeEmpty();
	word.GetStringFile(true, ALPHA_NUM, fp);
	while(fp && numAdds < MAX_ADDRESSES)
	{
             word.CopyString(words[numAdds]);
             numAdds++;
             word.GetStringFile(true, ALPHA_NUM, fp);
             }

where fp is my file pointer and g_Number Of Adds is my address count from the file

can anyone help please? ive got most of the other stuff working its just this thats the bridge in my program that i cant seem to suss out. can anyone shed some light on this for me please?

I dont think you explained right. Maybe take more time to explain it
Did you solve this already?

Jaja You right didnt pay attention.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.