Thread: Switch statement help

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    22

    Switch statement help

    Writing a program about a theoretical bank, designed to calculate monthly charges per number of checks based on what kind of account. This program utilizes switch statements.

    In terms of the value of checks, we're not allowed to use a counting while loop or an if...else loop, so it needs to be another switch. The cases need to be a ranges of values, and what I have written is below.

    Here is the code.

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
        ofstream outfile;
        outfile.open("output.txt");
    
        char choice, account_type;
        int checks;
        double charge;
    
        cout << "Would you like to calculate an account fee (Y or N)?  ";
        cin >> choice;
    
        switch(choice)
        {
        case 'Y': case 'y':
            cout << "Please enter the type of account (B for business, or P for personal):  ";
            cin >> account_type;
            cout << "Please enter the number of checks:  ";
            cin >> checks;
            switch (account_type)
            {
            case 'B': case 'b':
                switch (checks);
                {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                case 16:
                case 17:
                case 18:
                case 19:
                    charge = 25.00 + (checks * 0.10);
                    break;
                case 20:
                case 21:
                case 22:
                case 23:
                case 24:
                case 25:
                case 26:
                case 27:
                case 28:
                case 29:
                case 30:
                case 31:
                case 32:
                case 33:
                case 34:
                case 35:
                case 36:
                case 37:
                case 38:
                case 39:
                    charge = 25.00 + (checks * 0.08);
                    break;
                case 40:
                case 41:
                case 42:
                case 43:
                case 44:
                case 45:
                case 46:
                case 47:
                case 48:
                case 49:
                case 50:
                case 51:
                case 52:
                case 53:
                case 54:
                case 55:
                case 56:
                case 57:
                case 58:
                case 59:
                    charge = 25.00 + (checks * 0.06);
                    break;
                default:
                    charge = 25.00 + (checks * 0.04);
                    break;
                }
                break;
            case 'P': case 'p':
                switch (checks);
                {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                case 16:
                case 17:
                case 18:
                case 19:
                    charge = 25.00 + (checks * 0.10);
                    break;
                case 20:
                case 21:
                case 22:
                case 23:
                case 24:
                case 25:
                case 26:
                case 27:
                case 28:
                case 29:
                case 30:
                case 31:
                case 32:
                case 33:
                case 34:
                case 35:
                case 36:
                case 37:
                case 38:
                case 39:
                    charge = 25.00 + (checks * 0.10);
                    break;
                case 40:
                case 41:
                case 42:
                case 43:
                case 44:
                case 45:
                case 46:
                case 47:
                case 48:
                case 49:
                case 50:
                case 51:
                case 52:
                case 53:
                case 54:
                case 55:
                case 56:
                case 57:
                case 58:
                case 59:
                    charge = 25.00 + (checks * 0.10);
                    break;
                case 60:
                case 61:
                case 62:
                case 63:
                case 64:
                case 65:
                case 66:
                case 67:
                case 68:
                case 69:
                    break;
                default:
                    break;
                }
            default:
                cout << "ERROR:  invalid account type.  Please enter B or P only:  ";
                cin >> account_type;
                break;
            }
            break;
        case 'N': case'n':
            system("pause");
            return 0;
            break;
        default:
            cout << "ERROR:  Invalid choice.  Please enter only Y or N:  ";
            cin >> choice;
            break;
        }
    
        system("pause");
        return 0;
    }

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    In response to your big list of cases...
    After a lecture on cosmology and the structure of the solar system, William James was accosted by a little old lady.
    "Your theory that the sun is the centre of the solar system, and the earth is a ball which rotates around it has a very convincing ring to it, Mr. James, but it's wrong. I've got a better theory," said the little old lady.
    "And what is that, madam?" Inquired James politely.
    "That we live on a crust of earth which is on the back of a giant turtle,"
    Not wishing to demolish this absurd little theory by bringing to bear the masses of scientific evidence he had at his command, James decided to gently dissuade his opponent by making her see some of the inadequacies of her position.
    "If your theory is correct, madam," he asked, "what does this turtle stand on?"
    "You're a very clever man, Mr. James, and that's a very good question," replied the little old lady, "but I have an answer to it. And it is this: The first turtle stands on the back of a second, far larger, turtle, who stands directly under him."
    "But what does this second turtle stand on?" Persisted James patiently.
    To this the little old lady crowed triumphantly. "It's no use, Mr. James---it's turtles all the way down."

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    22
    ......lol

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well. Did the requirement specifically state that switch must be used, or just not an if-else statement? You could write a single formula for each of those, if you were so inclined and knew some fancy operators.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    22
    yes switch has to be used. and it specifically says to NOT use if-elses

  6. #6
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Yes, well to be fair it's really referring to infinite regression when your program is leading to a infinite explosion of updates to add "hmm one more number" but my point stands. You can use in if inside a case, you know...

  7. #7
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Tim Yap View Post
    yes switch has to be used. and it specifically says to NOT use if-elses
    Are you sure? If I were you I'd go to the pet shop and buy as many turtles as you can and give them to your teacher as a gift.

  8. #8
    Registered User
    Join Date
    Oct 2013
    Posts
    22
    in fact, ill give you the link for the programs instructions.
    http://cs.mwsu.edu/~ranette/CMPS1043...ank-switch.pdf

  9. #9
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Tim Yap View Post
    in fact, ill give you the link for the programs instructions.
    http://cs.mwsu.edu/~ranette/CMPS1043...ank-switch.pdf
    Yep. Buy those turtles.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In that case there's not a great deal you can do. What you have is certainly the most straightforward thing; you can get a lot less typing done by doing a bit of formula work (for instance, in the business case you could switch checks/20 instead of switch checks and you're down to four lines).

  11. #11
    Registered User
    Join Date
    Oct 2013
    Posts
    22
    By the way. I'm only in Comp Science 1 and I'm a freshman, so I more than likely don't know all the shortcuts and stuff that you guys do... so... yea

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    What exactly is the problem that you're facing with your solution, anyway?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Oct 2013
    Posts
    22
    well it's not compiling... the build is failing for some reason... ive made a few changes... here is the updated version.
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
        ofstream outfile;
        outfile.open("output.txt");
    
        char choice, account_type;
        int checks;
        double charge;
    
        cout << "Would you like to calculate an account fee (Y or N)?  ";
        cin >> choice;
    
        switch(choice)
        {
        case 'Y': case 'y':
            cout << "Please enter the type of account (B for business, or P for personal):  ";
            cin >> account_type;
            cout << "Please enter the number of checks:  ";
            cin >> checks;
            switch (account_type)
            {
            case 'B': case 'b':
                switch (checks);
                {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                case 16:
                case 17:
                case 18:
                case 19:
                    charge = 25.00 + (checks * 0.10);
                    break;
                case 20:
                case 21:
                case 22:
                case 23:
                case 24:
                case 25:
                case 26:
                case 27:
                case 28:
                case 29:
                case 30:
                case 31:
                case 32:
                case 33:
                case 34:
                case 35:
                case 36:
                case 37:
                case 38:
                case 39:
                    charge = 25.00 + (checks * 0.08);
                    break;
                case 40:
                case 41:
                case 42:
                case 43:
                case 44:
                case 45:
                case 46:
                case 47:
                case 48:
                case 49:
                case 50:
                case 51:
                case 52:
                case 53:
                case 54:
                case 55:
                case 56:
                case 57:
                case 58:
                case 59:
                    charge = 25.00 + (checks * 0.06);
                    break;
                default:
                    charge = 25.00 + (checks * 0.04);
                    break;
                }
                break;
            case 'P': case 'p':
                switch (checks);
                {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                case 16:
                case 17:
                case 18:
                case 19:                
                case 20:
                case 21:
                case 22:
                case 23:
                case 24:
                case 25:
                case 26:
                case 27:
                case 28:
                case 29:
                    charge = 5.00 + (checks * 0.05);
                    break;
                case 30:
                case 31:
                case 32:
                case 33:
                case 34:
                case 35:
                case 36:
                case 37:
                case 38:
                case 39:
                case 40:
                case 41:
                case 42:
                case 43:
                case 44:
                case 45:
                case 46:
                case 47:
                case 48:
                case 49:
                    charge = 5.00 + (checks * 0.04);
                    break;
                case 50:
                case 51:
                case 52:
                case 53:
                case 54:
                case 55:
                case 56:
                case 57:
                case 58:
                case 59:
                case 60:
                case 61:
                case 62:
                case 63:
                case 64:
                case 65:
                case 66:
                case 67:
                case 68:
                case 69:
                    charge = 5.00 + (checks * 0.03);
                    break;
                default:
                    charge = 5.00 + (checks * 0.02);
                    break;
                }
            default:
                cout << "ERROR:  invalid account type.  Please enter B or P only:  ";
                cin >> account_type;
                break;
            }
            break;
        case 'N': case'n':
            system("pause");
            return 0;
            break;
        default:
            cout << "ERROR:  Invalid choice.  Please enter only Y or N:  ";
            cin >> choice;
            break;
        }
    
        system("pause");
        return 0;
    }

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Okay, so what is the compile error that you're getting and what do you understand concerning it?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Oct 2013
    Posts
    22
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(35): warning C4060: switch statement contains no 'case' or 'default' labels
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(108): warning C4060: switch statement contains no 'case' or 'default' labels
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(110): error C2196: case value '1' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(111): error C2196: case value '2' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(112): error C2196: case value '3' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(113): error C2196: case value '4' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(114): error C2196: case value '5' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(115): error C2196: case value '6' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(116): error C2196: case value '7' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(117): error C2196: case value '8' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(118): error C2196: case value '9' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(119): error C2196: case value '10' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(120): error C2196: case value '11' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(121): error C2196: case value '12' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(122): error C2196: case value '13' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(123): error C2196: case value '14' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(124): error C2196: case value '15' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(125): error C2196: case value '16' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(126): error C2196: case value '17' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(127): error C2196: case value '18' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(128): error C2196: case value '19' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(129): error C2196: case value '20' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(130): error C2196: case value '21' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(131): error C2196: case value '22' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(132): error C2196: case value '23' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(133): error C2196: case value '24' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(134): error C2196: case value '25' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(135): error C2196: case value '26' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(136): error C2196: case value '27' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(137): error C2196: case value '28' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(138): error C2196: case value '29' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(141): error C2196: case value '30' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(142): error C2196: case value '31' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(143): error C2196: case value '32' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(144): error C2196: case value '33' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(145): error C2196: case value '34' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(146): error C2196: case value '35' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(147): error C2196: case value '36' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(148): error C2196: case value '37' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(149): error C2196: case value '38' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(150): error C2196: case value '39' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(151): error C2196: case value '40' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(152): error C2196: case value '41' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(153): error C2196: case value '42' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(154): error C2196: case value '43' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(155): error C2196: case value '44' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(156): error C2196: case value '45' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(157): error C2196: case value '46' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(158): error C2196: case value '47' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(159): error C2196: case value '48' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(160): error C2196: case value '49' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(163): error C2196: case value '50' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(164): error C2196: case value '51' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(165): error C2196: case value '52' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(166): error C2196: case value '53' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(167): error C2196: case value '54' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(168): error C2196: case value '55' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(169): error C2196: case value '56' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(170): error C2196: case value '57' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(171): error C2196: case value '58' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(172): error C2196: case value '59' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(179): error C2196: case value '66' already used
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(185): error C2048: more than one default
    1>c:\users\timothy\documents\visual studio 2010\projects\program 4\program 4\main.cpp(189): error C2048: more than one default
    1>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch Statement Help
    By IMMORTALX in forum C Programming
    Replies: 8
    Last Post: 09-06-2011, 08:24 PM
  2. switch statement
    By xniinja in forum C Programming
    Replies: 1
    Last Post: 07-13-2010, 04:07 PM
  3. Switch statement
    By BSmith4740 in forum C Programming
    Replies: 29
    Last Post: 02-28-2008, 08:28 PM
  4. After the switch statement?
    By cerin in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2005, 08:01 PM
  5. The Switch Statement
    By gozlan in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:44 AM