我的解决方案一直出错,因为我无法在visual studio d中使用strtof [英] I keep getting an error with my solution because I cant use strtof in visual studio d
问题描述
我的解决方案一直出错,因为由于不受支持,我无法在visual studio中使用strtof。我在第28行得到一个错误说明未定义。有什么我可以用来正确运行这个程序。
Ch9_Ex4Data.txt。输入文本文件是
普通鸡蛋
1.45
培根和鸡蛋
2.45
松饼
0.99
法式吐司
1.99
水果篮
2.49
谷物
0.69
咖啡
0.50
茶
0.75。
#include< iostream>
#include< cstdlib>
#include< string>
#include< fstream>
#include< stdlib.h>
using namespace std;
struct menuItem
{
string item;
浮动价格;
int quantity;
};
menuItem * breakfastMenu = new menuItem [8];
void getData()
{
ifstream myfile(" Ch9_Ex4Data.txt");
if(myfile.is_open())
{
字符串行;
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;
}
}
}
其他cout<< 无法打开文件;
myfile.close();
}
无效printCheck()
{
float billAmount;
for(int i = 0; i< 8; i ++)
{
if(breakfastMenu [i] .quantity> 0)
{
cout< billAmount + = breakfastMenu [i] .quantity * breakfastMenu [i] .price;
}
}
cout<<"金额到期$"< }
void showMenu()
{
int num,quantity;
char c;
for(int i = 0; i< 8; i ++)
cout<<(i + 1)<<"。 " < cout<<<"你想要什么?"< cin>> num;
cout<<"" Quantity?"< cin>>数量;
breakfastMenu [num-1] .quantity =数量;
cout<<"打印支票?(y / n)"< ; CIN>> c;
if(c ==''Y''|| c ==''y'')
printCheck();
else
showMenu();
}
int main()
{
getData();
showMenu();
system(" pause");
返回0;
}
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;
}
推荐答案
"< }
void showMenu()
{
int num,quantity;
char c;
for(int i = 0; i< 8; i ++)
cout<<(i + 1)<<"。 " < cout<<<"你想要什么?"< cin>> num;
cout<<"" Quantity?"< cin>>数量;
breakfastMenu [num-1] .quantity =数量;
cout<<"打印支票?(y / n)"< ; CIN>> c;
if(c ==''Y''|| c ==''y'')
printCheck();
else
showMenu();
}
int main()
{
getData();
showMenu();
system(" pause");
返回0;
}
"< }
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;
}
你的意思是strtok吗?
Did you mean strtok?
我不需要字符串浮动。我尝试使用strtod,但是因为strtod是字符串到数字而billAmount是浮点类型。我得到一个billAmount而没有被初始化。
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.
这篇关于我的解决方案一直出错,因为我无法在visual studio d中使用strtof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!