Read File in Switch

This is a discussion on Read File in Switch within the C++ Programming forums, part of the General Programming Boards category; The following piece of code (which will read a number from input.txt, provided of course that you enter 0) will ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    4

    Read File in Switch

    The following piece of code (which will read a number from input.txt, provided of course that you enter 0) will compile and run fine:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    
    int main()
    {
    
    int n;
    cin >> n;
    
    switch (n)
    {
    case 0:
         char buffer[256];
         ifstream inputfile ("input.txt");
         if (! inputfile.is_open())
         {
              cout << "Error opening file";
         }
         while(! inputfile.eof() )
         {
              inputfile.getline (buffer,100);
              n = atol(buffer);
              cout << n;
         }
         break;
    }
    }
    However, if I add another case into the switch, it will not compile:

    Code:
    switch (n)
    {
    case 0:
         char buffer[256];
         ifstream inputfile ("input.txt");
         if (! inputfile.is_open())
         {
              cout << "Error opening file";
         }
         while(! inputfile.eof() )
         {
              inputfile.getline (buffer,100);
              n = atol(buffer);
              cout << n;
         }
         break;
    case 1:
         break;
    }
    Why does adding that other case make it not work?

    P.S. I know the code is very strange... it is part of a program I am writing that does make sense, but I have identified this as the problem.

    Thanks!
    Last edited by daedalus703; 08-25-2005 at 05:43 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    When you declare variables inside a switch case, you use brackets ( { } ) I believe. Thus making your code more like:

    Code:
    	switch (n)
    	{
    		case 0:
    		{
    			somevar var;
    			... code here....
    			break;
    		}
    		case 1:
    			....more stuff
    			break;
    	}
    Edit: You know just for kicks I should mention you should knock the .h from your header file declarations. It's some C++ ANSI standard. http://www.google.com/search?q=c%2B%2B+header+files
    Last edited by Tonto; 08-25-2005 at 05:51 PM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > Why does adding that other case make it not work?
    Well stating OS, Compiler and error messages would be a start.

    > #include <iostream.h>
    If your compiler doesn't support this, then you need to upgrade.
    #include <iostream>
    using namespace std;

    > while(! inputfile.eof() )
    Read the FAQ as to why eof() in control loops doesn't do what you think it does.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    I enclosed the offending case in brackets and it now works.

    Thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. How to convert char read from file into string
    By cruxxe in forum C Programming
    Replies: 7
    Last Post: 05-22-2002, 02:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21