Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Switch statement and while loop

Switch statement and while loop

Scheduled Pinned Locked Moved C / C++ / MFC
helpregex
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aaron Knox
    wrote on last edited by
    #1

    I'm trying to write a program that reads state abbreviations from an input file and then outputs the corrosponding state to an output file. (this is my first class in programming) I'm using a while loop that runs until the end of input file and switch statements to match states and abbreviations, here is a portion... inAbrev >> firstLetter >> secondLetter; //splits abbreviation into two char's while(inAbrev) //read til the end of the file, inAbrev is the ifstream { switch (firstLetter) //checks first letter of abbreviation and tries to match it to working cases //I have one of these for each letter that a state name begins with { case 'A' : switch (secondLetter) { case 'L' : outState << inAbrev << " is " << "Alabama" << endl; //outState is the break; //output file case 'K' : outState << inAbrev << " is " << "Alaska" << endl; break; case 'Z' : outState << inAbrev << " is " << "Arizona" << endl; break; case 'R' : outState << inAbrev << " is " << "Arkansas" << endl; break; } I can get it to compile but my loop (and maybe my switch statements) seems to be wrong. The problem is that it gets stuck on the first input (AL) and goes into an infinite loop of just reading that input, it is never evaluated and nothing is output. I'm probably missing something simple but my beginners brain can't find it. Any help would be great. Thanks, Aaron arrrgh

    J 1 Reply Last reply
    0
    • A Aaron Knox

      I'm trying to write a program that reads state abbreviations from an input file and then outputs the corrosponding state to an output file. (this is my first class in programming) I'm using a while loop that runs until the end of input file and switch statements to match states and abbreviations, here is a portion... inAbrev >> firstLetter >> secondLetter; //splits abbreviation into two char's while(inAbrev) //read til the end of the file, inAbrev is the ifstream { switch (firstLetter) //checks first letter of abbreviation and tries to match it to working cases //I have one of these for each letter that a state name begins with { case 'A' : switch (secondLetter) { case 'L' : outState << inAbrev << " is " << "Alabama" << endl; //outState is the break; //output file case 'K' : outState << inAbrev << " is " << "Alaska" << endl; break; case 'Z' : outState << inAbrev << " is " << "Arizona" << endl; break; case 'R' : outState << inAbrev << " is " << "Arkansas" << endl; break; } I can get it to compile but my loop (and maybe my switch statements) seems to be wrong. The problem is that it gets stuck on the first input (AL) and goes into an infinite loop of just reading that input, it is never evaluated and nothing is output. I'm probably missing something simple but my beginners brain can't find it. Any help would be great. Thanks, Aaron arrrgh

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      The first line should be inside the while loop. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      A 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        The first line should be inside the while loop. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        A Offline
        A Offline
        Aaron Knox
        wrote on last edited by
        #3

        Thanks Joaquin, my while loop now works. However for some reason my switch statements are not being run. If you or anyone has any suggestions it would be great. Thanks again, Aaron arrrgh

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups