CPP/Loops/DoWhile

From ProgrammingExamples
< CPP
Revision as of 08:08, 23 June 2010 by Daviddoria (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

DoWhile.cpp

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
#include <fstream>
 
int main(int argc, char *argv[])
{
  if(argc != 2)
  {
    std::cout << "Required parameters: InputFilename" << std::endl;
    exit(-1);
  }
 
  std::string InputFilename = argv[1];
 
  std::ifstream in(InputFilename.c_str());
 
  if (!in) 
  {
    std::cout << "File " << InputFilename << " not found" << std::endl;
    exit(-1);
  }
 
#if 0 //bad way
  std::string line;
  while(getline(in, line))
  {
    if(line[0] == '#') //we found the first line we were looking for, need to switch to the next loop
    {
      break;
    }
  }
 
  //start reading data - //woops - we missed the first line of interests (here the line starting with #)
  while(getline(in, line))
  {
    std::cout << line << std::endl;
  }
#else
  std::string line;
 
  while(getline(in, line))
  {
    if(line[0] == '#') //we found the first line we were looking for, need to switch to the next loop
    {
      break;
    }
  }
 
  //start reading data - //woops - we missed the first line of interests (here the line starting with #)
  do
  {
    std::cout << line << std::endl;
  }while(getline(in, line));
#endif
 
  return 0;
}