Hey all,

i would really appreciate some help with abit of code i'm stuck on. I am trying to read data from text files in this way:

1. Open text file
2. Read in the first line from the text file that doesnt start with "/"
3. change this char to int
4. assign this int a name
5. find the next line in the text file that doesnt start with "/"
6. change this char to int
7. repeat steps 5 and 6 until the end of the text file

I am new enough to C++ and the problem I am having is once I have found the first line that doesnt start with "/" how do I set this as the new position to start looking for the next line that starts with "/"

What I have so far is:

char Input[256]; 
  ifstream myfile ("CMPInput.txt"); 
 while (! myfile.eof() ) 
   { 
    myfile.getline (Input,100);
     if (Input[0] != '/')
     {
      int ConDenRan1L = atoi(Input);
      }
	
       char Input1[256];
       myfile.getline (Input1,100);
       if (Input1[0] != '/')
           {
           int ConDenRan1H = atoi(Input1);
		
           int test = ConDenRan1H - ConDenRan1L;
          cout << "test = " << test << endl;
          }

Seems as though I am starting from the start of the text file each time. Thanks in advance for your help.

Colm

The code is cut off at the end, but I don't see a problem.
Try something simple, like below, and get it to work,
before adding complexity.
Also, use a short input file, with 4 lines in it, to start with,
and add more lines after you get it working.

while (!myfile.eof())
{
    myfile.getline(input1,100);
    cout << input1 << endl;
    myfile.getline(input2,100);
    cout << input2 << endl;
}

Thanks for the reply UncleLeroy,
I can extract the data from the text file but I'm forced to use an if statement to only read the rows not starting with "/" and can therefore not use the varibles read from the text file as they are local to the if statement:

char Input[256]; 
char Input1[256];
ifstream myfile ("CMPInput.txt"); 
 
int di = 1;
while (! myfile.eof() ) 
  { 
   if(di == 1)
      {
        myfile.getline (Input,100);
        if (Input[0] != '/')
	{
	int ConDenRan1L = atoi(Input);
                cout << "ConDenRan1L = " << ConDenRan1L << endl;
	di = di + 1;
	}
       }
       if(di == 2)
       {
        myfile.getline (Input1,100);
        if (Input1[0] != '/')
           {
	int ConDenRan1H = atoi(Input1);
                cout << "ConDenRan1H = " << ConDenRan1H << endl;
	break;
	}
        }
  }		

   cout << "ConDenRan1L = " << ConDenRan1L << endl;
   int test = ConDenRan1H - ConDenRan1L;
   cout << "Test = " << test << endl;

The last 3 lines of code are a test, but as the variables are only defined in the if statement, they are not recognised outside the if statement. Has anyone any work around ideas for a novice. Thanks in advance.

Colm (the depressed reseaercher :| )

1) You can control the scope of a variable by moving its declaration outside the "while" statement. Go back to chapter 1.

int ConDenRan1L;
  int ConDenRan2H;
  while (...)
  {
  }

2) You need to detect an early end-of-file, for example if there is an odd number of numbered lines in the file. (e.g. one line, "42"), or all the lines start with '/'. Since that logic is needed for both the odd and the even line, it should be in a function, rather than duplicated code.

// Function: get_a_number_from_myfile()
// Reads a line from myfile, skipping lines with '/'.
// Assumes that all other lines contain exactly one integer.
// (extra characters after the number are discarded).
// If it finds a number, sets outNumber and returns true
// Returns false at EOF, (outNumber is not valid at EOF).
// Examples:
//   /* something moresomething */  - skipped
//   // something moresomething     - skipped
//   /* something                   - skipped
//   12                             - return true, Out = 12
//   11.9999                        - return true, Out = 11
//   1 2 3 4 5                      - return true, Out = 1
//   7e+4                           - return true, Out = 7
//   moresomething */               - return true, Out = 0
//   EOF                            - return false, Out = invalid
bool get_a_number_from_myfile(int &outNumber)
{
  char input[100];
  do {
    if (myfile.eof())
      return false;
    myfile.getline(input,100); // myfile is global
    // cout << "Got: " << input1 << endl; // Uncomment for debug
  } while (input[0] == '/');
  outNumber = atoi(input);
  // cout << "Number: " << outNumber << endl;
  return true;
}

The calling code needs to look at the return value:

while (1) // Loop until "break"
{
    if (get_a_number_from_myfile(ConDenRan1L))
      break; // Normal exit; EOF after an even number of inputs
    if (get_a_number_from_myfile(ConDenRan1H))
      break; // Unusual; EOF after an odd number of inputs
    // Do something with ConDenRan1L/1H here:
    int difference = ConDenRan1H - ConDenRan1L;
    // ...
}

You could wrap the 2 get_a_number's into one function call:

int get_two_numbers(int &Out1, int &Out2)
{
    if (get_a_number_from_myfile(Out1))
      return 0; // No numbers
    if (get_a_number_from_myfile(Out2))
      return 1; // Only one number found
    return 2; // Found 2 numbers
}

And call it with:

while (get_two_numbers(ConDenRan1L, ConDenRan1H) == 2)
{
    // Do something with ConDenRan1L/1H here:
    int difference = ConDenRan1H - ConDenRan1L;
    // ...
}

Suggestions: Put comments in your code. Start small and add to it. Keep it simple by creating small functions. Avoid duplication. Do more thinking, less whining. Learn to use a debugger instead of printf or cout.

Edited 6 Years Ago by UncleLeroy: suggested comments

This article has been dead for over six months. Start a new discussion instead.