Thread: Error thrown when executing one of my switch cases.

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    9

    Error thrown when executing one of my switch cases.

    Hello,


    We were tasked with creating a program that tracks textbooks. For an example, if I entered in terminal this: B 1234567890123 Books Title
    Then the output would have store the book, and output "Book is defined"
    B character is for defining a book, Now C character defines a course, so if I entered: C CSEE 212 Programming Class
    It would output, "course is created".
    Now in my code, the switch case for "C" seem to throw an arrow in execution mode, like "aborted...out of range" But it takes my case B, M, A, correctly. Can anyone point me to finding my error?
    The code is lengthy so posted the switch case portion only.

    Code:
      int main()
    {
           string code;
           istringstream iss(line);
           iss>>code;
           long newISBN = 0;
           int bookIndex = 0;
           char updateCode;
           switch(code.at(0))
           {
           case 'B': books[bookCounter] = new Book;
               iss>>books[bookCounter]->isbn;
               books[bookCounter]->title = iss.str().substr(iss.tellg());
               bookCounter++;
                       break;
           case 'C': course[courseCounter] = new Course;  //Help in here.
               iss>>course[courseCounter]->deptCode>>course[courseCounter]->courseNumber;
               course[courseCounter]->name = iss.str().substr(iss.tellg());
                   courseCounter++;
                   break;
           case 'A': iss>>newISBN;
                   int deptCode, courseNum, courseIndex;
                   iss>>deptCode>>courseNum;
                   courseIndex = getCourse( courseNum, deptCode);
                   bookIndex = getBook( newISBN);
                   classes[classCounter] = new Class;
                   classes[classCounter]->book = books[bookIndex];
                   classes[classCounter]->course = course[courseIndex];
                   iss>>classes[classCounter]->sectionNumber;
                   iss>>classes[classCounter]->required;
                   classCounter++;
                   break;
           case 'G': printBooks(code, iss); break;
           case 'P': printAllInfo(code, iss); break;
           }
       }
       }
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You need to write code that is maintainable.
    You are doing too much in the switch statement.
    Use functions! Just because you are doing C++ does NOT mean you can NOT use helper functions.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > Now in my code, the switch case for "C" seem to throw an arrow in execution mode, like "aborted...out of range"
    > case 'C': course[courseCounter] = new Course; //Help in here.

    Meanwhile we're left to ponder how exactly you declared the 'course' array (or vector, or pointer), or whether you even bothered to initialise courseCounter to a sensible value.
    Short, Self Contained, Correct Example
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Functions/Switch Cases? (in my code)
    By smogsy in forum C Programming
    Replies: 1
    Last Post: 03-02-2011, 03:04 AM
  2. Question about switch cases
    By cashmerelc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 11:57 PM
  3. switch cases and timers
    By soranz in forum C++ Programming
    Replies: 5
    Last Post: 10-02-2005, 06:43 PM
  4. switch cases and do while
    By exoillusion in forum C Programming
    Replies: 7
    Last Post: 07-28-2003, 07:18 AM
  5. could someone please explain switch cases?
    By .exe in forum C++ Programming
    Replies: 7
    Last Post: 07-01-2003, 01:42 PM

Tags for this Thread