help with switch statement?

This is a discussion on help with switch statement? within the C++ Programming forums, part of the General Programming Boards category; I'm doing an assignment for school, not asking anyone to write my program. But I am still stuck on the ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    help with switch statement?

    I'm doing an assignment for school, not asking anyone to write my program. But I am still stuck on the switch statement. I keep getting errors when I compile. Anyways here is my new program...

    #include <cmath>
    #include <iostream>

    double totalPayed(double baseSalary, double bonus, double sum)
    {
    return (baseSalary + bonus) = sum;
    }
    double payroll(double baseSalary, double bonus)
    {
    return (baseSalary * 5) = payroll;
    }
    using namespace std;

    void main()
    {
    int employeeID, baseSalary=2000, bonus, j, sum1, sum2, sum3, sum4, sum5;

    switch(totalPayed) {
    case 1:
    sum1=baseSalary + 1400;
    break;
    case 2:
    sum2=baseSalary + 600;
    break;
    case 3:
    sum3=baseSalary + 350;
    break;
    case 4:
    sum4=baseSalary + 4500;
    break;
    case 5:
    sum5=baseSalary + 1500;
    break;
    default:
    for (j=1; j<=6; j++) {
    cout << "The total payout to all employees is: " << totalPayed << endl;
    }
    }
    cout << "The payroll is: " << payroll(baseSalary, bonus) << endl;
    cout << "EmployeeID Base Salary Bonus Total Pay" << endl;
    cout << " " << endl;
    cout << "Total Payroll-----------------_________" << endl;
    }
    Is this last part how you make it print out a table??
    Thanks for any help! ;-)
    Kristina

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    what errors do you get upon trying to compile? and please try to use [ code] and [/ code] tags next time (just a friendly reminder).
    later

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    10
    This is one error that I get.....
    [ error C2450: switch expression of type 'double (__cdecl *)(double,double,double)' is illegal]


    Here is a warning I don't understand...
    [warning C4551: function call missing argument list]
    Thanks.
    Kristina

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Try: switch( (int)totalPayed )

    Since switch statements only work with integers, you have to cast it to one. The other option is to use an if/else statement.

    And you cannot say: "(baseSalary + bonus) = sum;"

    This should be "sum = (baseSalary + bonus) ;"


    One more thing. Take a look at this function here:

    Code:
    double totalPayed(double baseSalary, double bonus, double sum) 
    { 
    return (baseSalary + bonus) = sum; 
    }

    Why are you passing "sum" into the function by value? The math will be performed within the function, but "sum" will be unchanged on exit...so the proper way is to pass by reference.


    Code:
    double totalPayed(double baseSalary, double bonus, double *sum) 
    { 
    return *sum = (baseSalary + bonus); 
    }

    Or since your just returning it, just do:


    Code:
    double totalPayed(double baseSalary, double bonus) 
    { 
       double sum;
       return sum = (baseSalary + bonus); 
    }



    ITSA
    Socket Library!

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    10
    Yes, thank you. But now I just have 1 more problem. I am getting on more error and I don't know how to fix the prob.

    [error C2440: '=' : cannot convert from 'double (__cdecl *)(double,double)' to 'double'
    There is no context in which this conversion is possible]

    Any idea how to fix this? I have no clue.
    Thanks again.
    Kristina

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    10
    Disregard what I just posted up above! Ok my program works but the output is screwed up!! The output that I get is...

    OUTPUT
    [The total payout to all employees is: 004011F9
    The total payout to all employees is: 004011F9
    The total payout to all employees is: 004011F9
    The total payout to all employees is: 004011F9
    The total payout to all employees is: 004011F9
    The total payout to all employees is: 004011F9
    The payroll is: 10000
    Employee ID Base Salary Bonus Total Pay
    Total Payroll-----------------__________]

    I don't understand how to get the correct info into my table. I want it to look like
    ID Base Salary Bonus Total Pay
    ---- --------------- -------- ------------
    101 2000 1400 3400
    102

    etc. I have no clue about making tables in C++ any help is greatly appreciated. Thank You
    Kristina :-)

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Your error isn't in formatting a table. There are no "tables" in C++ per se. I'm not sure where your code stands after the advice you've already received. There are several errors in your original code, some pointed out, another is that you switch on totalPayed, which is a function, not a variable, and you do so before the variable returned by totalPayed is defined.
    Maybe post your revised code for more help; the program isn't that hard, really
    Also, use int main(), returning 0, not void main(). Check the faqs or search the archives for why. Some on the board who can help the most have given up replying to people using void main().
    Truth is a malleable commodity - Dick Cheney

  8. #8
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    If you want to format text you have to pick a I/O stream and format it manually.. there are no "tables", thats only a concept you create...

    as for the guy who posted above me, about you creating a void main() function, haha dont worry about it, since returning 0 or 1 to the operating system in your case doesnt matter. a void return will just return a unpredictable number to the OS'.


    But i must appreciate you saying you dont want someone to write your code, there are so many morons on this board that come on and ask for code, that is for there assignments...
    Last edited by Liam Battle; 05-13-2002 at 08:22 PM.
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    10
    Thanks sooo much for the help. Here is my revised (working) code. Well if there are no tables in C++, what do I put in my cout statement to make it spit out into a table like format?

    [
    // Kristina Manning-Klm15975@aol.com
    // Disk File: MANKLP2.cpp
    // Instructior Mr. Christopherson
    // Assigned: 5/8/02, Due: 5/15/02, Finished: 5/13/02

    #include <cmath>
    #include <iostream>

    double totalPayed(double baseSalary, double bonus1, double bonus2, double bonus3, double bonus4, double bonus5)
    {
    double sum;
    return sum = ((baseSalary * 5) + bonus1 + bonus2 + bonus3 + bonus4 + bonus5);
    }
    double payroll(double baseSalary)
    {
    double payroll;
    return payroll = (baseSalary * 5);
    }
    using namespace std;

    int main()
    {
    int baseSalary=2000, bonus1=1400, bonus2=600, bonus3=350, bonus4=4500, bonus5=1500, j, sum1, sum2, sum3, sum4, sum5;

    switch( (int)totalPayed ) {
    case 1:
    sum1=baseSalary + bonus1;
    break;
    case 2:
    sum2=baseSalary + bonus2;
    break;
    case 3:
    sum3=baseSalary + bonus3;
    break;
    case 4:
    sum4=baseSalary + bonus4;
    break;
    case 5:
    sum5=baseSalary + bonus5;
    break;
    default:
    for (j=1; j<=6; j++) {
    cout << j << endl;
    }
    }
    cout << "The total payout to all employees is: " << totalPayed(baseSalary, bonus1, bonus2, bonus3, bonus4, bonus5) << endl;
    cout << "The payroll is: " << payroll(baseSalary) << endl;
    cout << "EmployeeID Base Salary Bonus Total Pay" << endl;
    }
    ]

    Thanks again for all the help.
    Kristina
    :-)

  10. #10
    Unregistered
    Guest
    you can use tabs '\t', they work well in organization.

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Comments added...
    Code:
    #include <cmath>      // Not needed in this code
    #include <iostream> 
    
    double totalPayed(double baseSalary, double bonus1, double bonus2, double bonus3, double bonus4, double bonus5) 
    { 
    double sum;     // Doesn't hurt, but not needed. You can return the equation result by itself
    return sum = ((baseSalary * 5) + bonus1 + bonus2 + bonus3 + bonus4 + bonus5); 
    } 
    double payroll(double baseSalary) 
    { 
    double payroll; 
    return payroll = (baseSalary * 5); 
    } 
    using namespace std;    // this belongs right after the #include statements.
    
    int main() 
    { 
       int baseSalary=2000, bonus1=1400, bonus2=600,      bonus3=350, bonus4=4500, bonus5=1500, j, sum1, sum2, sum3,  sum4, sum5; 
    
    switch( (int)totalPayed )   // you can't do this. totalPayed is a function, not a variable.
       // All functions need () after their name when you call them.
      // You also have to pass the various arguments in that you defined above - six double values.
      // Given that you only use integer values, and adding or multiplying them will still result in an integer, why not declare the functions to return int? If you divided them, you'd need doubles.
      // Even if you could use totalPayed here (you can't), the result of totalPayed() won't equal 1, 2, 3, etc, so you'll always hit the default statement.
    { 
    case 1: 
    sum1=baseSalary + bonus1; 
    break; 
    case 2: 
    sum2=baseSalary + bonus2; 
    break; 
    case 3: 
    sum3=baseSalary + bonus3; 
    break; 
    case 4: 
    sum4=baseSalary + bonus4; 
    break; 
    case 5: 
    sum5=baseSalary + bonus5; 
    break; 
    default: 
    for (j=1; j<=6; j++)  // This will only run when the other case statements don't. I'm guessing you want to print the results on numbered lines. This won't do it.
    {   
    cout << j << endl; 
    } 
    } 
    cout << "The total payout to all employees is: " << totalPayed(baseSalary, bonus1, bonus2, bonus3, bonus4, bonus5) << endl; 
    cout << "The payroll is: " << payroll(baseSalary) << endl; 
    cout << "EmployeeID Base Salary Bonus Total Pay" << endl;
          // need return 0 as last statement in main() 
    } 
    ]    // this isn't part of code
    That should help some. Fix that stuff before worrying about formatting. Look up setw() for that.
    Truth is a malleable commodity - Dick Cheney

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. char switch statement
    By jmarsh56 in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 05:04 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM

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