Help with code for simple Y2K problem

This is a discussion on Help with code for simple Y2K problem within the C++ Programming forums, part of the General Programming Boards category; I have to write code for this: A college has an “academic year” and counts four semesters in order of ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    Help with code for simple Y2K problem

    I have to write code for this:
    A college has an “academic year” and counts four semesters in order of Fall, Spring, First Summer and Second Summer as an academic year. The Fall, 2002 semester was the first semester of Academic Year 2003.

    Your program should accept as input an unknown number of 3 digit numbers representing the current semester. For each input record, output the semester in the form given in the first column above.

    Inputs:
    031
    033
    980

    Output:
    Semester Code
    Fall, 2002 031
    First Summer, 2003 033
    Spring, 1998 982

    As you can see, it's really a Y2K problem. My code does ok exept it stays in the loop and I can't get the output to resemble the example above, with the "Semester" then "Code" and the output below it. Can someone help me with that?
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    int main()
    {
        int x, y, z;
    		
        cout<<"Enter semester code or \"q\" to quit > "<<endl;
                                    // actually, any non-numeric character will break out of the loop
        while (cin >> x)
        {
    //      if ((x / 10) >= 0 && (x / 10) <= 50)
    //          y = (x / 10) + 2000;
    				
    //      else if ((x / 10) == 1 || (x /10) >= 51)
    //          y = (x / 10) + 1900;
                                    //	isn't x/10 always >= 0?
                                    //	where you break between 1900's & 2000's is arbitrary, right?
                                    //  isn't this simpler:
    
            if (x/10 <= 50)
                y = x/10 + 2000;
                else y = x/10 + 1900;
    
            z = x % 10;
                                    //	you had some unneeded brackets
            cout << "Semester" <<"              "<<  " Code" <<right <<endl;
            if (x % 10 == 1)
                cout << "Fall, "<< y - 1<<"              "<< setw(3) << setfill('0')<< x <<right <<endl;
            if (x % 10 ==2)
                cout << "Spring, "<< y << "            "<<setw(3) <<right << setfill('0') <<  x <<endl;
            if (x % 10 == 3)
                cout << "First Summer, "<< y << "      "<<setw(3) << right << setfill('0') << x <<endl;
            if (x % 10 == 4)
                cout << "Second Summer, "<< y << "     "<<setw(3) << right << setfill('0') << x <<endl;
    
            cin.ignore();           //	this discards the newline characters entered by user
                                    //	then, prompt for the next entry before exiting loop
    
            cout<<"Enter code or \"q\" to quit > "<<endl;
        }
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    Help with code for simple Y2K problem

    Thank you R.Stiltskin !! I do appreciate your advice.
    How would I only show this line

    cout << "Semester" <<" "<< " Code" <<right <<endl;

    only once, on top of the output?

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    You could move it up to just before the while loop, but since all the output is going to the screen, it'll still be broken up by prompts and input lines between the output lines.

    Otherwise, you would have to send the output to a file, and print it afterwards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  2. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 02:05 PM
  3. Problem with my rotation code?
    By EvBladeRunnervE in forum Game Programming
    Replies: 44
    Last Post: 12-21-2003, 12:33 AM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM

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