Need help with program using structs & arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Woody714
    New Member
    • Jun 2007
    • 3

    Need help with program using structs & arrays

    I'm fairly new to C++ and have an assignment that is knocking me out. The text file is fairly simple:



    Plain Egg
    1.45
    Bacon and Egg
    2.45
    Muffin
    0.99..... etc....


    And the code is:


    [code=cpp]
    //Breakfast Billing System

    //Marlen LaBianco

    //May 20, 2007

    //Program to calculate a local restaurant's breakfast billing system

    #include<iostre am>

    #include<fstrea m>

    #include<iomani p>

    #include<string >

    using namespace std;

    const int Breakfast_Items = 8;

    struct menuItemType

    {

    string menuItem;

    double menuPrice;

    };

    menuItemType menuList[Breakfast_Items];

    ifstream inMenu1;

    ofstream outMenu;

    void getData ();

    void showMenu ();



    int main()

    {


    string inputMenu;

    string outputMenu;

    inMenu1.open("i temNo.txt", ios::in);

    if (!inMenu1)

    {

    cout << "Cannot open the input file." << endl;

    return 1;

    }

    getData ();

    showMenu ();

    inMenu1.close() ;

    outMenu.close() ;


    return 0;

    }

    void getData ()

    {

    int index;

    string menuItem;

    string item;

    double price;

    for (index = 0; index < 8; index++)

    {

    getline(inMenu1 , menuList[index].menuItem);

    item = menuList[index].menuItem;

    inMenu1 >> menuList[index].menuPrice;

    price = menuList[index].menuPrice;



    cout << item << setw(15) << setprecision(2)

    << setiosflags(ios ::fixed) << right << "$"

    << price << endl;


    }

    }

    void showMenu ()

    {

    int index;

    int maxIndex = 0;


    cout << "Restaurant Breakfast Menu" << endl;


    for (index = 0; index < 8; index++)

    {

    cout << menuList[index].menuItem << setw(15) << setprecision(2)

    << setiosflags(ios ::fixed) << right << "$"

    << menuList[index].menuPrice << endl;


    }

    }

    [/code]

    And this is what the program is giving out:

    Plain Egg $1.45

    $0.00

    $0.00

    $0.00

    $0.00

    $0.00

    $0.00

    $0.00

    Restaurant Breakfast Menu

    Plain Egg $1.45

    $0.00

    $0.00

    $0.00

    $0.00

    $0.00

    $0.00

    $0.00



    Please help!




    --------------------------------------------------------------------------------
    Woody
  • Silent1Mezzo
    New Member
    • Feb 2007
    • 208

    #2
    Can you please put your code in [CODE] tags? It maakes it a lot easier to read it.

    Thanks.

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #3
      Here is the better formatted code (I think):
      [code=cpp]
      //Breakfast Billing System
      //Marlen LaBianco
      //May 20, 2007

      //Program to calculate a local restaurant's breakfast billing system

      #include<iostre am>
      #include<fstrea m>
      #include<iomani p>
      #include<string >

      using namespace std;

      const int Breakfast_Items = 8;

      struct menuItemType
      {
      string menuItem;
      double menuPrice;
      };

      menuItemType menuList[Breakfast_Items];

      ifstream inMenu1;
      ofstream outMenu; // not used

      void getData ();
      void showMenu ();

      int main()
      {
      string inputMenu;
      string outputMenu;

      inMenu1.open("i temNo.txt", ios::in);

      if (!inMenu1) // I would prefer "if (!inMenu1.is_op en())"
      {
      cout << "Cannot open the input file." << endl;
      return 1;
      }

      getData ();
      showMenu ();

      inMenu1.close() ;
      outMenu.close() ;

      return 0;
      }


      void getData ()
      {
      int index;

      string menuItem; //not used
      string item;

      double price;

      for (index = 0; index < 8; index++)
      {
      getline(inMenu1 , menuList[index].menuItem);
      item = menuList[index].menuItem;

      inMenu1 >> menuList[index].menuPrice;
      price = menuList[index].menuPrice;

      cout << item << setw(15) << setprecision(2)

      << setiosflags(ios ::fixed) << right << "$"

      << price << endl;
      }
      }

      void showMenu ()
      {
      int index;

      int maxIndex = 0; // not used

      cout << "Restaurant Breakfast Menu" << endl;

      for (index = 0; index < 8; index++)
      {
      cout << menuList[index].menuItem << setw(15) << setprecision(2)

      << setiosflags(ios ::fixed) << right << "$"

      << menuList[index].menuPrice << endl;
      }

      }[/code]

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        [

        I'm quite sure that problem is among this lines:

        [CODE=cpp]for (index = 0; index < 8; index++)
        {
        getline(inMenu1 , menuList[index].menuItem);
        item = menuList[index].menuItem;

        inMenu1 >> menuList[index].menuPrice;
        price = menuList[index].menuPrice;

        cout << item << setw(15) << setprecision(2)

        << setiosflags(ios ::fixed) << right << "$"

        << price << endl;
        }[/CODE]

        Now let us take a closer look.

        Savage

        Comment

        • echo26
          New Member
          • May 2007
          • 5

          #5
          Is there any particular reason that you are outputting the menu twice (i.e in getData () and showMenu ())?

          Code:
          cout << item << setw(15) << setprecision(2)
          
          << setiosflags(ios::fixed) << right << "$"
          
          << price << endl;
          Last edited by echo26; Jun 5 '07, 07:02 AM. Reason: Umm....

          Comment

          • echo26
            New Member
            • May 2007
            • 5

            #6
            Okay, ignore the previous post.

            The question that I should have really asked is what exactly is this program supposed to do or output etc. (Correct me if I'm wrong, but...) I (and possibly others) would find a brief description useful to finding the solution to your problem (that way we know what needs correcting) =).

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Your problem is here:

              [code=cpp]
              inMenu1 >> menuList[index].menuPrice;
              [/code]

              True, menuList[index].menuPrice is a double and the price of 1.45 does go in there.

              BUT! The \n on that line of data is still in the input stream. You need to eat that enter key.

              Change your code to:

              [code=cpp]
              inMenu1 >> menuList[index].menuPrice;
              inMenu1.ignore( );
              [/code]

              and your world will become happy again.

              Comment

              • Woody714
                New Member
                • Jun 2007
                • 3

                #8
                HEY! Thank you! It worked!!

                Thanks for your patience with this newbie...if I post again, I will be sure to remember the rules.

                Thanks again,

                Woody

                Comment

                Working...