I keep getting an error with my solution because I cant use strtof in visual studio d

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zorse03
    New Member
    • Nov 2014
    • 8

    #1

    I keep getting an error with my solution because I cant use strtof in visual studio d

    I keep getting an error with my solution because I cant use strtof in visual studio due to not being supported. I get an error saying is undefined on line 28. Is there anything i can use to run this program properly.


    Ch9_Ex4Data.txt . The input text file is
    Plain Egg
    1.45
    Bacon and Egg
    2.45
    Muffin
    0.99
    French Toast
    1.99
    Fruit Basket
    2.49
    Cereal
    0.69
    Coffee
    0.50
    Tea
    0.75.




    #include<iostre am>
    #include<cstdli b>
    #include<string >
    #include<fstrea m>
    #include<stdlib .h>

    using namespace std;
    struct menuItem
    {
    string item;
    float price;
    int quantity;
    };
    menuItem* breakfastMenu = new menuItem[8];

    void getData()
    {
    ifstream myfile ("Ch9_Ex4Data.t xt");
    if (myfile.is_open ())
    {
    string line;
    int i=0,flag=1;
    while ( getline (myfile,line) )
    {
    if(flag){
    breakfastMenu[i].item = line;
    flag=0;
    }
    else{
    breakfastMenu[i++].price = (float)strtof(l ine.c_str(),NUL L);
    flag=1;
    }
    }
    }
    else cout << "Unable to open file";
    myfile.close();
    }
    void printCheck()
    {
    float billAmount;
    for(int i=0;i<8;i++)
    {
    if(breakfastMen u[i].quantity > 0)
    {
    cout< billAmount += breakfastMenu[i].quantity * breakfastMenu[i].price;
    }
    }
    cout<<"Amount Due $"< }

    void showMenu()
    {
    int num,quantity;
    char c;
    for(int i=0;i<8;i++)
    cout<<(i+1) <<". " < cout<<"What would you like to have?"< cin>>num;
    cout<<"Quantity ?"< cin>>quantity;
    breakfastMenu[num-1].quantity = quantity;
    cout<<"Print check?(y/n)"< cin>> c;
    if(c=='Y' || c=='y')
    printCheck();
    else
    showMenu();
    }


    int main()
    {
    getData();
    showMenu();
    system("pause") ;
    return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Did you mean strtok?

    Comment

    • zorse03
      New Member
      • Nov 2014
      • 8

      #3
      No I need string to float. I try using strtod, however since strtod, which is string to digit and billAmount is float type. I get an billAmount is used without being initialized.

      Comment

      • zorse03
        New Member
        • Nov 2014
        • 8

        #4
        No I need string to float. I try using strtod, however since strtod, is string to digit and billAmount is float type. I get an billAmount is used without being initialized.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          First of all, the code does not compile. There's little I can do until it compiles.

          Some problems are:

          Code:
          cout< billAmount += breakfastMenu[i].quantity * breakfastMenu[i].price;
          The operator for cout is << and not <.

          While billAmount can be displayed, += cannot. Do the calculation before the cout statement and just display the result.

          Code:
          cout<<"Amount Due $"<
          Did you mean ; instead of the <?

          There are other places with the same sort of problem.

          Nothing I could see had anything to do with strtof.

          Comment

          • zorse03
            New Member
            • Nov 2014
            • 8

            #6
            #include<iostre am>
            #include<cstdli b>
            #include<string >
            #include<fstrea m>

            using namespace std;
            struct menuItem
            {
            string item;
            float price;
            int quantity;
            };
            menuItem* breakfastMenu = new menuItem[8];

            void getData()
            {
            ifstream myfile ("Ch9_Ex4Data.t xt");
            if (myfile.is_open ())
            {
            string line;
            int i=0,flag=1;
            while ( getline (myfile,line) )
            {
            if(flag)
            {
            breakfastMenu[i].item = line;
            flag=0;
            }
            else
            {
            breakfastMenu[i++].price = (float)strtof(l ine.c_str(),NUL L);
            flag=1;
            }
            }
            }
            else cout << "Unable to open file";
            myfile.close();
            }
            void printCheck()
            {
            float billAmount;
            for(int i=0;i<8;i++)
            {
            if(breakfastMen u[i].quantity > 0)
            {
            cout<<breakfast Menu[i].quantity<<"\t" <<breakfastMe nu[i].item<<"\t$"<<b reakfastMenu[i].price<<endl;
            billAmount += breakfastMenu[i].quantity * breakfastMenu[i].price;
            }
            }
            cout<<"Amount Due $"<<billAmou nt;
            }

            void showMenu()
            {
            int num,quantity;
            char c;
            for(int i=0;i<8;i++)
            cout<<(i+1) <<". " <<breakfastMe nu[i].item << " $"<<breakfastMe nu[i].price<<endl;
            cout<<"What would you like to have?"<<endl;
            cin>>num;
            cout<<"Quantity ?"<<endl;
            cin>>quantity;
            breakfastMenu[num-1].quantity = quantity;
            cout<<"Print check?(y/n)"<<endl;
            cin>> c;
            if(c=='Y' || c=='y')
            printCheck();
            else
            showMenu();
            }


            int main()
            {
            getData();
            showMenu();
            system("pause") ;
            return 0;
            }

            Comment

            • zorse03
              New Member
              • Nov 2014
              • 8

              #7
              This is the error

              \consoleapplica tion1\source.cp p(31): error C3861: 'strtof': identifier not found

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                What Visual Studio are you using?

                I'm using Visual Studio 2013 and strtof is not causing an error.

                I just get a warning that billAmount on line 47 is being used without being initialized.

                Comment

                • zorse03
                  New Member
                  • Nov 2014
                  • 8

                  #9
                  IM using visual studio 2012, i switch strtof with strtod, then i got that warning as well on line 47. How can i fix that warning?

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Just initialize billAmount to 0.0f to remove the line 47 warning:

                    Code:
                    float billAmount = 0.0f;
                    I don't get any error using strtof or strtod.

                    What s your error exactly?

                    Comment

                    • zorse03
                      New Member
                      • Nov 2014
                      • 8

                      #11
                      what line do i put the initialize billAmount at?

                      Comment

                      • zorse03
                        New Member
                        • Nov 2014
                        • 8

                        #12
                        Thanks for the help, it worked

                        Comment

                        Working...