selection structure problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zelmila19
    New Member
    • Apr 2008
    • 16

    selection structure problem

    Hi
    I have to create a program that displays a shipping charge. The shipping charge is based on the state name entered by the user, as shown:
    State
    Shipping charge ($)
    Alabama
    25
    Alaska
    50
    If the user enters any other state name, the shipping charge should be 0 (zero).
    So far I've come up with this but am having errors. Can someone help check my code out?

    #include <iostream>
    #include <string>
    #include <algorithm>

    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;

    int main()
    {
    //declare variables
    string state = "";
    int shipCharge = 0;
    const int Alabama = 25;
    const int Alaska = 20;

    //enter input item
    cout << "Enter the state name: ";
    cin >> state;
    getline(cin, state);

    //assign shipping charge
    if (state = "Alabama")
    {
    cout << shipCharge << "Alabama" << endl;
    }
    else if (state = "Alaska")
    {
    cout << shipCharge << Alaska << endl;
    } //end if

    //display shipping charge
    cout << "Shipping charge: " << shipCharge << endl;

    return 0;
    } //end of main function
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Can you write those errors here?

    Comment

    • zelmila19
      New Member
      • Apr 2008
      • 16

      #3
      Originally posted by zodilla58
      Can you write those errors here?
      This is the error am getting:

      error C2451: conditional expression of type 'std::basic_str ing<_Elem,_Trai ts,_Ax>' is illegal
      with
      [
      _Elem=char,
      _Traits=std::ch ar_traits<char> ,
      _Ax=std::alloca tor<char>

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        This is your problem:

        [CODE=cpp]if (state = "Alabama")
        {
        cout << shipCharge << "Alabama" << endl;
        }
        else if (state = "Alaska")
        {
        cout << shipCharge << Alaska << endl;[/CODE]

        You are assigning "Alabama" to state instead of testing whether state equals Alabama.Change assigment operator(=) with equality(==) and it should be fine.

        Comment

        • zelmila19
          New Member
          • Apr 2008
          • 16

          #5
          Originally posted by Savage
          This is your problem:

          [CODE=cpp]if (state = "Alabama")
          {
          cout << shipCharge << "Alabama" << endl;
          }
          else if (state = "Alaska")
          {
          cout << shipCharge << Alaska << endl;[/CODE]

          You are assigning "Alabama" to state instead of testing whether state equals Alabama.Change assigment operator(=) with equality(==) and it should be fine.
          Thank you, now it doesnt have errors but it's not giving the value :(

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by zelmila19
            Thank you, now it doesnt have errors but it's not giving the value :(
            That's because of this:

            [CODE=cpp]cin >> state;
            getline(cin, state);[/CODE]

            Any reason why are you inputing state twice?

            Comment

            • zelmila19
              New Member
              • Apr 2008
              • 16

              #7
              Originally posted by Savage
              That's because of this:

              [CODE=cpp]cin >> state;
              getline(cin, state);[/CODE]

              Any reason why are you inputing state twice?
              oh whoops my bad, thanks.... :)

              Comment

              Working...