473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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<iostream>
#include<cstdlib>
#include<string>
#include<fstream>
#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.txt");
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(line.c_str(),NULL);
flag=1;
}
}
}
else cout << "Unable to open file";
myfile.close();
}
void printCheck()
{
float billAmount;
for(int i=0;i<8;i++)
{
if(breakfastMenu[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;
}
Nov 16 '14 #1
11 2495
weaknessforcats
9,208 Expert Mod 8TB
Did you mean strtok?
Nov 16 '14 #2
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.
Nov 16 '14 #3
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.
Nov 16 '14 #4
weaknessforcats
9,208 Expert Mod 8TB
First of all, the code does not compile. There's little I can do until it compiles.

Some problems are:

Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. 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.
Nov 16 '14 #5
#include<iostream>
#include<cstdlib>
#include<string>
#include<fstream>

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

void getData()
{
ifstream myfile ("Ch9_Ex4Data.txt");
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(line.c_str(),NULL);
flag=1;
}
}
}
else cout << "Unable to open file";
myfile.close();
}
void printCheck()
{
float billAmount;
for(int i=0;i<8;i++)
{
if(breakfastMenu[i].quantity > 0)
{
cout<<breakfastMenu[i].quantity<<"\t"<<breakfastMenu[i].item<<"\t$"<<breakfastMenu[i].price<<endl;
billAmount += breakfastMenu[i].quantity * breakfastMenu[i].price;
}
}
cout<<"Amount Due $"<<billAmount;
}

void showMenu()
{
int num,quantity;
char c;
for(int i=0;i<8;i++)
cout<<(i+1) <<". " <<breakfastMenu[i].item << " $"<<breakfastMenu[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;
}
Nov 16 '14 #6
This is the error

\consoleapplication1\source.cpp(31): error C3861: 'strtof': identifier not found
Nov 16 '14 #7
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 16 '14 #8
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?
Nov 16 '14 #9
weaknessforcats
9,208 Expert Mod 8TB
Just initialize billAmount to 0.0f to remove the line 47 warning:

Expand|Select|Wrap|Line Numbers
  1. float billAmount = 0.0f;
I don't get any error using strtof or strtod.

What s your error exactly?
Nov 16 '14 #10
what line do i put the initialize billAmount at?
Nov 16 '14 #11
Thanks for the help, it worked
Nov 16 '14 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

27
by: Richard Blewett [DevelopMentor] | last post by:
I've just seen on Eric Gunnerson's blog that C# is getting Edit and Continue in Whidbey. That will please alot of people - although me, I have mixed feelings about it ;-) ...
1
by: Jose Martinez | last post by:
Hola a todos, Tengo instalado el Visual Studio .NET 2002, en el cual, he creado un proyecto, que al ejecutar en modo depuracion, me sale el siguiente mensaje: Error while trying to run...
8
by: Jacob Crossley | last post by:
Preface: Not to sound mean or arrogant, but please don't answer this question unless you have a specific and tested answer. I'm saying this only because I posted the same quesion earlier and got...
3
by: Paras Sharma | last post by:
Hi all, We are facing this big problem. Scenario is as follows. We have one single solution (say EIS) under which there are 25 projects. All the files are saved at a central location under...
1
by: RMorgan | last post by:
Attempting to databind any property of any control on any form in the entire solution in the designer and I recieve a messagebox error, "Object reference not set to an instance of an object." My...
3
by: Mani | last post by:
Can you build a solution from the command prompt? like so "c:\ build mysolution.sln" or do you always have to manually open the solution in Visual Studio and click "build" Second Question is...
1
by: Count in Excel | last post by:
I have this really weird pop message everytime i m trying to uninstall Visual Studio 2003. The message said "Set up is unable to determine the valid order for installation. See the error log for...
3
by: Andy B | last post by:
Is there anything special you have to do to upgrade or transfer a vs2005 solution to a vs2008 solution? Or do you just open it in vs2008 and follow some directions?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.