- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
该代码包含在超市中找到的 200 种产品。我想编写一个代码,首先显示所有可用的产品,如果您输入其中一个,它会返回产品,即价格和数量。问题是,如果我输入 scanf("%s",insertproduct) ;
它仅识别具有一个单词的产品。我在其他帖子中看到解决方案是编写 scanf("%[^\t\n]s",insertproduct);
。
但是当我这样做时,程序会忽略 scanf
并在
printf("\nPlease select the desirable product to see it's quantity and price.\n\n");
一种想法是在超过一个单词的产品之间添加_,但这会花费很多时间。关于特定的 scanf() 有什么想法吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <io.h>
#include <conio.h>
#include <fcntl.h>
#include <locale.h>
struct supermarket
{
char name[50];
float price;
int quantity;
};
int main(void) {
struct supermarket product[200] = {
{"sugar",1.2,127},
{"greek coffee",2.03,200},
{"milk",1.83,150},
{"6pack small eggs",3.03,22},
{"AAA bateries",2.2,20},
{"AA bateries",2.3,29},
{"6-pack big eggs",3.5,17},
{"Frosties",4.5,30},
{"All Bran",4.7,32},
{"Special K",5.2,19},
{"Honey Nut Loops",4.3,33},
{"Coco Pops",4.09,28},
{"White Bread",0.5,50},
{"Black Bread",0.7,55},
{"Toast Bread",1.73,66},
{"cinamon",1.9,500},
{"salt",1,423},
{"pepper",1.29,325},
{"coca cola 330ml",0.45,980},
{"coca cola 500ml",0.9,745},
{"coca cola 2lt",1.6,300},
{"coca cola 1lt",1.3,1025},
{"pepsi 330ml",0.45,980},
{"pepsi 500ml",0.9,745},
{"pepsi 2lt",1.6,300},
{"pepsi 1lt",1.3,1025},
{"7-up 330ml",0.6,682},
{"fanta 330ml",0.4,741},
{"fanta 1lt",1.1,120},
{"fanta 500ml",0.75,267},
{"mild mustard", 1.45,612},
{"Hot mustard",1.85,387},
{"Ketchup",1.6,400},
{"mayonaise",2.1,346},
{"apples",0.20,105},
{"oranges",0.4,222},
{"bananas",0.42,55},
{"lettuce",0.12,141},
{"potatoes 1kg",0.7,75},
{"tomatoes",0.35,199},
{"onions",0.11,425},
{"grape-fruits",0.55,94},
{"fresh salads",2.22,23},
{"canned mushrooms",1.5,422},
{"fresh mushrooms 200gr",1.98,55},
{"oreos",1.59,257},
{"Tide",2.47,730},
{"Pampers",5.89,123},
{"Mr Clean",5.01,35},
{"Mr Proper",4.99,8},
{"Kool-Aid",2.45,103},
{"Knorr",3.05,308},
{"Palmolive",3.01,617},
{"Scotch-Brite",2.3,970},
{"Cheetos",1.25,487},
{"Top Ramen",6.27,109},
{"Tomato sauce",0.59,1589},
{"Goya Beans",3.21,300},
{"Quaker Oats",3.39,32},
{"Cream of Wheat",3.09,91},
{"Nesquick",1.75,799},
{"Silk Soy Milk",3.14,50},
{"Philadelphia",4.15,6},
{"Soy Sauce",1.09,45},
{"A1 steak sauce",4.89,643},
{"Barbeque sauce",5.79,94},
{"Lays",2.5,567},
{"Lays oregano",2.7,163},
{"Pringles",1.85,421},
{"Ajax",4.59,2548},
{"Cleanex",5.27,941},
{"Fountain",5.55,559},
{"Mountain Dew",6.09,852},
{"10 DVD",3.09,1896},
{"Lipton ice tea",2.44,53},
{"Lipton tea",1.29,633},
{"Listerine",5.52,188},
{"Centrum",9.99,588},
{"Snapple",5.88,22},
{"Purina 1",12.99,33},
{"Wiskas",6.22,79},
{"Pedigree Pal",9.99,10},
{"Gatorade",2.13,1000},
{"Amstel",0.99,111},
{"Heineken",1.01,155},
{"Guiness",1.8,77},
{"McFarland",1.65,74},
{"Estrella Damn",1.32,488},
{"Stella Artois",0.87,14},
{"Corona",1.11,347},
{"Pilsner",1.74,166},
{"Budweiser",1.77,654},
{"Chimay Blue",2.12,344},
{"Chimay Red",2.35,179},
{"Chimay Black",2.55,333},
{"Chimay Yellow",2.65,198},
{"Beck's",1.72,188},
{"Carlsberg",1.67,843},
{"Paulaner",2.89,42},
{"Erdinger",3.02,7},
{"Colgate",1.75,644},
{"butter",1.28,399},
{"Peanut-Butter",2.74,211},
{"PB&J",3.15,25},
{"Barilla Linguini",2.58,941},
{"Barilla spaghetti",1.56,357},
{"Barilla tortellini",2.08,700},
{"Barilla papardelle",3.58,9},
{"Barilla lazagne",2.99,88},
{"Advil",7.99,499},
{"pears",0.26,2985},
{"peaches",0.27,2456},
{"Kiwi",0.36,2145},
{"nectarines",0.38,2365},
{"melon",0.39,951},
{"watermellon",0.4,961},
{"grapes",0.53,582},
{"cherries",0.6,714},
{"olives",0.09,936},
{"pickles",0.87,195},
{"halvah",1.33,2015},
{"frulite orange-juice",1.61,478},
{"frulite lemon-juice",1.62,477},
{"frulite pineapple-juice",1.63,456},
{"frulite carrot-juice",1.64,698},
{"frulite banana-juice",1.65,874},
{"frulite mixed-juice",1.66,599},
{"rusks 160gr",1.35,588},
{"rusks 320gr",2.03,589},
{"rusks 400gr",2.72,523},
{"rusks 500gr",3.74,542},
{"vanila biscuits",3.30,699},
{"cinamon biscuits",3.32,698},
{"almond biscuits",3.35,612},
{"butter biscuits",3.54,609},
{"chocolate biscuits",3.41,59},
{"cherry croissant",0.47,1025},
{"apricot croissant",0.48,1058},
{"chocolate croissant",0.41,1089},
{"vanilla croissant",0.41,1054},
{"vanilla-chocolate croissant",0.74,1036},
{"cocoa croissant",0.27,1011},
{"double-sized croissant",1.4,555},
{"Derby",0.24,1000},
{"Kinder Bueno",1.22,642},
{"Kinder egg",0.91,377},
{"Nivea hair-gel",5.88,417},
{"Fructise hair-gel",5.97,429},
{"Excellence hair-gel",4.88,437},
{"mixed cake",3.99,11},
{"chocolate cake",3.26,31},
{"vanilla cake",3.36,44},
{"orange cake",3.46,66},
{"orange-chocolate cake",3.86,94},
{"raisins cake",3.06,52},
{"vakalaos",6.98,20},
{"galeos",7.40,30},
{"frozen shrimps",13.80,40},
{"gavros",5.98,50},
{"frozen octopus",6.36,90},
{"fresh octopus",7.23,70},
{"fresh shrimps",15.98,80},
{"espresso",2.25,1896},
{"6 chicken legs",6.66,11},
{"2 chicken breasts",4.59,37},
{"whole chicken",3.12,45},
{"roasted chicken",4.97,48},
{"capuccino",2.55,1478},
{"jacobs vanilla",4.96,2458},
{"jacobs chocolate",4.85,2365},
{"jacobs caramel",4.87,2148},
{"jacobs classic",4.59,1895},
{"liquid fabric softener",5.22,1025},
{"concentrated fabric softener",6.22,1048},
{"noxzema saving foam 200ml",2.2,4832},
{"loreal saving foam 200ml",2.6,613},
{"nivea saving foam 200ml",2.8,2000},
{"gillette saving foam 200ml",2.32,992},
{"bic saving foam 200ml",2.13,469},
{"noxzema saving foam 400ml",4.44,817},
{"loreal saving foam 400ml",4.42,802},
{"nivea saving foam 400ml",4.77,808},
{"gillette saving foam 400ml",5.25,707},
{"bic saving foam 400ml",5.21,303},
{"Giotis baby-cream",7.10,111},
{"Nounou baby-cream",7.05,223},
{"Farin lacte baby-cream",6.82,334},
{"Nutricia baby-cream",8.5,445},
{"Ariel 5kg",22.5,556},
{"Ariel 2kg",10.94,667},
{"Ariel 1kg",5.31,778},
{"Elvive shampoo",6.99,889},
{"Wash and go shampoo",4.98,991},
{"Proderm shampoo",7.22,882},
{"Pantene shampoo",6.44,773},
{"Fructis shampoo",7.13,664},
{"Gliss shampoo",3.19,552},
{"toilet paper 8 rolls",3.77,668},
{"toilet paper 12 rolls",5.99,3},
{"toilet paper 16 rolls",6.54,13}
};
int choise;
printf("Please enter 1 and hit Enter to continue.\n");
scanf("%d", &choise);
int i;
printf("See our full list of products.\n\n");
for(i = 0 ; i < 200 ; i++){
printf("%s\n", product[i].name);
}
char insertproduct[50];
printf("\nPlease select the desirable product to see it's quantity and price.\n\n");
scanf("%s", insertproduct);
for (i = 0 ; i < 200 ; i++) {
if(strcmp(product[i].name, insertproduct) == 0) {
printf("You selected: %s. Price: %.2f euro. Quantity: %d items.\n\n",
product[i].name,
product[i].price,
product[i].quantity);
}
}
return 0;
}
最佳答案
scanf()
当发现空格字符时将停止读取,尽管您可以修复代码并使 scanf()
工作,我认为在这种情况下它最好使用fgets()
clearStdin()
函数,下面代码中有两个任务,首先清除'\n'
以及其他可能留下的空格
scanf("%d", &choise);
如果您输入无效数据,则允许重复调用而不会导致无限循环,然后如果输入有效,也将它们也取走,以便 fgets()
找不到 '\n'
在流中并返回一个空字符串。
尝试这个更正后的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct supermarket
{
char name[50];
float price;
int quantity;
};
void clearStdin()
{
int chr;
while (((chr = getchar()) != EOF) && (chr != '\n'));
}
int main(void) {
struct supermarket product[200] = {
{"sugar",1.2,127},
{"greek coffee",2.03,200},
{"milk",1.83,150},
{"6pack small eggs",3.03,22},
{"AAA bateries",2.2,20},
{"AA bateries",2.3,29},
{"6-pack big eggs",3.5,17},
{"Frosties",4.5,30},
{"All Bran",4.7,32},
{"Special K",5.2,19},
{"Honey Nut Loops",4.3,33},
{"Coco Pops",4.09,28},
{"White Bread",0.5,50},
{"Black Bread",0.7,55},
{"Toast Bread",1.73,66},
{"cinamon",1.9,500},
{"salt",1,423},
{"pepper",1.29,325},
{"coca cola 330ml",0.45,980},
{"coca cola 500ml",0.9,745},
{"coca cola 2lt",1.6,300},
{"coca cola 1lt",1.3,1025},
{"pepsi 330ml",0.45,980},
{"pepsi 500ml",0.9,745},
{"pepsi 2lt",1.6,300},
{"pepsi 1lt",1.3,1025},
{"7-up 330ml",0.6,682},
{"fanta 330ml",0.4,741},
{"fanta 1lt",1.1,120},
{"fanta 500ml",0.75,267},
{"mild mustard", 1.45,612},
{"Hot mustard",1.85,387},
{"Ketchup",1.6,400},
{"mayonaise",2.1,346},
{"apples",0.20,105},
{"oranges",0.4,222},
{"bananas",0.42,55},
{"lettuce",0.12,141},
{"potatoes 1kg",0.7,75},
{"tomatoes",0.35,199},
{"onions",0.11,425},
{"grape-fruits",0.55,94},
{"fresh salads",2.22,23},
{"canned mushrooms",1.5,422},
{"fresh mushrooms 200gr",1.98,55},
{"oreos",1.59,257},
{"Tide",2.47,730},
{"Pampers",5.89,123},
{"Mr Clean",5.01,35},
{"Mr Proper",4.99,8},
{"Kool-Aid",2.45,103},
{"Knorr",3.05,308},
{"Palmolive",3.01,617},
{"Scotch-Brite",2.3,970},
{"Cheetos",1.25,487},
{"Top Ramen",6.27,109},
{"Tomato sauce",0.59,1589},
{"Goya Beans",3.21,300},
{"Quaker Oats",3.39,32},
{"Cream of Wheat",3.09,91},
{"Nesquick",1.75,799},
{"Silk Soy Milk",3.14,50},
{"Philadelphia",4.15,6},
{"Soy Sauce",1.09,45},
{"A1 steak sauce",4.89,643},
{"Barbeque sauce",5.79,94},
{"Lays",2.5,567},
{"Lays oregano",2.7,163},
{"Pringles",1.85,421},
{"Ajax",4.59,2548},
{"Cleanex",5.27,941},
{"Fountain",5.55,559},
{"Mountain Dew",6.09,852},
{"10 DVD",3.09,1896},
{"Lipton ice tea",2.44,53},
{"Lipton tea",1.29,633},
{"Listerine",5.52,188},
{"Centrum",9.99,588},
{"Snapple",5.88,22},
{"Purina 1",12.99,33},
{"Wiskas",6.22,79},
{"Pedigree Pal",9.99,10},
{"Gatorade",2.13,1000},
{"Amstel",0.99,111},
{"Heineken",1.01,155},
{"Guiness",1.8,77},
{"McFarland",1.65,74},
{"Estrella Damn",1.32,488},
{"Stella Artois",0.87,14},
{"Corona",1.11,347},
{"Pilsner",1.74,166},
{"Budweiser",1.77,654},
{"Chimay Blue",2.12,344},
{"Chimay Red",2.35,179},
{"Chimay Black",2.55,333},
{"Chimay Yellow",2.65,198},
{"Beck's",1.72,188},
{"Carlsberg",1.67,843},
{"Paulaner",2.89,42},
{"Erdinger",3.02,7},
{"Colgate",1.75,644},
{"butter",1.28,399},
{"Peanut-Butter",2.74,211},
{"PB&J",3.15,25},
{"Barilla Linguini",2.58,941},
{"Barilla spaghetti",1.56,357},
{"Barilla tortellini",2.08,700},
{"Barilla papardelle",3.58,9},
{"Barilla lazagne",2.99,88},
{"Advil",7.99,499},
{"pears",0.26,2985},
{"peaches",0.27,2456},
{"Kiwi",0.36,2145},
{"nectarines",0.38,2365},
{"melon",0.39,951},
{"watermellon",0.4,961},
{"grapes",0.53,582},
{"cherries",0.6,714},
{"olives",0.09,936},
{"pickles",0.87,195},
{"halvah",1.33,2015},
{"frulite orange-juice",1.61,478},
{"frulite lemon-juice",1.62,477},
{"frulite pineapple-juice",1.63,456},
{"frulite carrot-juice",1.64,698},
{"frulite banana-juice",1.65,874},
{"frulite mixed-juice",1.66,599},
{"rusks 160gr",1.35,588},
{"rusks 320gr",2.03,589},
{"rusks 400gr",2.72,523},
{"rusks 500gr",3.74,542},
{"vanila biscuits",3.30,699},
{"cinamon biscuits",3.32,698},
{"almond biscuits",3.35,612},
{"butter biscuits",3.54,609},
{"chocolate biscuits",3.41,59},
{"cherry croissant",0.47,1025},
{"apricot croissant",0.48,1058},
{"chocolate croissant",0.41,1089},
{"vanilla croissant",0.41,1054},
{"vanilla-chocolate croissant",0.74,1036},
{"cocoa croissant",0.27,1011},
{"double-sized croissant",1.4,555},
{"Derby",0.24,1000},
{"Kinder Bueno",1.22,642},
{"Kinder egg",0.91,377},
{"Nivea hair-gel",5.88,417},
{"Fructise hair-gel",5.97,429},
{"Excellence hair-gel",4.88,437},
{"mixed cake",3.99,11},
{"chocolate cake",3.26,31},
{"vanilla cake",3.36,44},
{"orange cake",3.46,66},
{"orange-chocolate cake",3.86,94},
{"raisins cake",3.06,52},
{"vakalaos",6.98,20},
{"galeos",7.40,30},
{"frozen shrimps",13.80,40},
{"gavros",5.98,50},
{"frozen octopus",6.36,90},
{"fresh octopus",7.23,70},
{"fresh shrimps",15.98,80},
{"espresso",2.25,1896},
{"6 chicken legs",6.66,11},
{"2 chicken breasts",4.59,37},
{"whole chicken",3.12,45},
{"roasted chicken",4.97,48},
{"capuccino",2.55,1478},
{"jacobs vanilla",4.96,2458},
{"jacobs chocolate",4.85,2365},
{"jacobs caramel",4.87,2148},
{"jacobs classic",4.59,1895},
{"liquid fabric softener",5.22,1025},
{"concentrated fabric softener",6.22,1048},
{"noxzema saving foam 200ml",2.2,4832},
{"loreal saving foam 200ml",2.6,613},
{"nivea saving foam 200ml",2.8,2000},
{"gillette saving foam 200ml",2.32,992},
{"bic saving foam 200ml",2.13,469},
{"noxzema saving foam 400ml",4.44,817},
{"loreal saving foam 400ml",4.42,802},
{"nivea saving foam 400ml",4.77,808},
{"gillette saving foam 400ml",5.25,707},
{"bic saving foam 400ml",5.21,303},
{"Giotis baby-cream",7.10,111},
{"Nounou baby-cream",7.05,223},
{"Farin lacte baby-cream",6.82,334},
{"Nutricia baby-cream",8.5,445},
{"Ariel 5kg",22.5,556},
{"Ariel 2kg",10.94,667},
{"Ariel 1kg",5.31,778},
{"Elvive shampoo",6.99,889},
{"Wash and go shampoo",4.98,991},
{"Proderm shampoo",7.22,882},
{"Pantene shampoo",6.44,773},
{"Fructis shampoo",7.13,664},
{"Gliss shampoo",3.19,552},
{"toilet paper 8 rolls",3.77,668},
{"toilet paper 12 rolls",5.99,3},
{"toilet paper 16 rolls",6.54,13}
};
int choise;
int i;
char insertproduct[50];
printf("Please enter 1 and hit Enter to continue.\n");
while (scanf("%d", &choise) != 1)
{
printf("invalid, choice\n");
printf("Please enter 1 and hit Enter to continue.\n");
clearStdin();
}
clearStdin();
printf("See our full list of products.\n\n");
for (i = 0 ; i < 200 ; i++) {
printf("%s\n", product[i].name);
}
printf("\nPlease select the desirable product to see it's quantity and price.\n\n");
if (fgets(insertproduct, sizeof(insertproduct), stdin) != NULL)
{
size_t length;
length = strlen(insertproduct);
/* remove possible '\n' consumed by fgets */
if (insertproduct[length - 1] == '\n')
insertproduct[length - 1] = '\0';
for (i = 0 ; i < 200 ; i++) {
if (strcmp(product[i].name, insertproduct) == 0) {
printf("You selected: %s.\n\tPrice: %.2f euro.\n\tQuantity: %d items.\n\n",
product[i].name,
product[i].price,
product[i].quantity);
}
}
}
return 0;
}
您应该知道的一件事是 fgets()
确实从流中读取 '\n'
字符,因此在比较之前您应该将其从数组中删除,其中您持有输入字符串。
关于c - 当产品有 2 个或更多单词时,scanf() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28203470/
我需要按不同类别过滤我网站上的产品。例如,如果您选择“DRINKS”类别,它将向我显示属于该类别的产品。 为了更好地解释自己。 我需要按类别过滤我网站的出版物 [产品],例如,在选择一个类别时说“饮料
我有 orders 集合,其中包含 products 集合。我将一些产品 ID 作为列表传递给该方法。我需要返回与任何匹配的产品列表input 列表中的 id。 我需要像这样使用 foreach 循环
我已经为临时分发进行存档好几个月了,但今天突然我无法这样做,因为“存档”菜单项已被禁用。我没有改变任何东西。我完成了该项目的配置设置,看起来没问题。 我的临时个人资料即将在 14 天后过期。这可能是问
我正在尝试找出产品和产品属性之间的关系。我有一个 product 表和一个 product_attributes 表。产品可以具有多种属性。我需要查询来查找具有一个特定属性和另一个属性之一的所有产品。
我正在使用 MySQL Workbench 创建 EER 图。 实现产品、类别和公司表之间关系的最佳方式是什么? 我正在考虑这种关系,但考虑到我想让公司的客户管理自己的产品/类别,这是最好的方式吗?如
我正在使用 itertools 包,并尝试在具有 900 个值的数组中创建 1、2 和 3 的所有可能组合,然后将其转换为 30 x 30 矩阵。我必须执行此操作的代码在下面并且工作正常。 for d
我有几个关于 Cartridge 启动器的问题: 我的产品不需要评级或发布日期。他们永远不会出售。一些产品是可下载的,因此“num_in_stock”不相关或本质上是无限制的。没有颜色选项,只有尺寸。
在 MySQL 中,存储产品价格(或一般货币)的首选列类型是什么?谷歌知道我经常使用 DECIMAL of FLOAT,但我想知道哪个更好。 我存储的价格范围是 0.01 到 25.00。当然更高的值
在软件开发过程中,尤其是在准备将新功能或修复后的版本上线之前,进行详尽的自测和上线前检查是至关重要的。以下是一个从多个维度综合考量的上线升级检查清单(Checklist),旨在帮助团队确保软件质量、稳
我正在创建一个闪购网站,并且我已经在主页和商店页面上根据日期范围显示产品。但我也想根据其他地方的日期范围显示产品,因此使用简码。 这是我的代码: function testt($meta_query)
可以在 WooCommerce 上批量创建产品吗?我正在使用 wp-cli Product 命令,但似乎我必须一个一个地创建。 'My product 1'), array('title'
我有一个带有数量和价格列的 excel 文件,我用它来为插件 WooCommerce Dynamic Pricing 创建必要的输出的定价规则。 我几乎想通了,但是 WooCommerce 进口商正在
我刚刚继承了一个woocommerce项目,我需要将主页更改为仅显示特定品牌。他们设置了 Product-Data => Attribute => pa_brand。 如果我打印 pa_brand 数
在插件中如何使用 wc_get_products() 获取产品。或者有其他方法可以做到吗? if ( in_array( 'woocommerce/woocommerce.php', apply_fi
我正在做一个无法从公司网络外部访问的内部网,他们希望在 Plone 中显示一些关于文件下载和最常查看的页面的不错的统计数据。 由于网络限制,我无法使用谷歌分析或任何类型的外部服务,那么是否有任何产品可
我正在就以下问题寻求建议: 保留哪些产品 key 属于哪个客户端的列表。例如,如果我的产品 key 为 8456-7894-4567-7894,则应该这样设计,以便将列表写入数据库而不是文件。 如何将
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
尝试将产品搜索栏添加到 Wordpress 管理栏后端,以进行 Woocommerce 产品搜索。它将位于后端管理菜单栏的顶部,这样无论您在后端的哪个位置,都可以搜索 woo 的产品。我很接近但在小绊
这让我抓狂.. 我正在尝试根据特定属性查询和输出 WooCommerce 产品。例如,我设置了一个名为 on 的属性,可能的值为 yes或 no . 我使用以下查询: $args = array(
我正在尝试从 Shopify 商店获取所有产品的 JSON。我一直在向 {STORE URL}/products.json 端点。但这最终只显示了商店提供的部分产品(很多,但不是全部)。当我将参数更改
我是一名优秀的程序员,十分优秀!