我正在学习数据结构。我写了一个简单的程序,要求用户填写3本书的购买信息。像书名,作者,费用和页面一样。
但该程序从未提示我最后一个问题(多少页)。但是为第二本书再次开始循环。
struct bookinfo {
char title[40];
char author[25];
double price;
int pages;
}; // Header File "bookinfo"
#include "bookinfo.h"
#include <stdio.h>
int main(void)
{
int ctr;
struct bookinfo books[3]; // Array of three structure variables
// Get the information about each book from the user
for(ctr = 0; ctr < 3; ctr++)
{
printf("What is the name of the book #%d?\n", (ctr+1));
gets(books[ctr].title);
puts("Who is the author? ");
gets(books[ctr].author);
puts("How much did the book cost? ");
scanf(" $%f", &books[ctr].price);
puts("How many pages in the book? ");
scanf(" %d", &books[ctr].pages);
getchar(); // Clears last newline for the next loop
}
// Print a header line and then loop through and print the info
printf("\n\nHere is the collection of books: \n");
for(ctr = 0; ctr < 3; ctr++)
{
printf("#%d: %s by %s", (ctr+1), books[ctr].title, books[ctr].author);
printf("\nIt is %d pages and cost $%.2f", books[ctr].pages, books[ctr].price);
printf("\n\n");
}
return (0);
}
答案 0 :(得分:1)
如果您未在金额之前输入$,则第一次scanf
来电会失败。结果,scanf
立即返回,第二个scanf
读取应该转到之前scanf
来电的输入。
编辑抱歉,我没有提供相关信息来源。根据scanf(3)的手册页:
格式字符串由一系列指令组成,这些指令描述了如何处理输入字符序列。如果指令处理失败,则不再读取其他输入,并返回scanf()。 “失败”可以 以下两种情况之一:输入失败,意味着输入字符不可用或匹配失败,意味着输入不合适(见下文)。
答案 1 :(得分:1)
像这样改变
<Page x:Class="WpfApplication3.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1" Loaded="Page_Loaded">
<Grid>
<MediaElement Grid.Column="2" Grid.Row="4" Margin="0,0,47,0" HorizontalAlignment="Right" Width="80" Source="progress.gif" LoadedBehavior="Manual" Name="MP"/>
</Grid>
</Page>
private void Page_Loaded(object sender, RoutedEventArgs e)
{
MP.Play();
}
scanf手册页
答案 2 :(得分:1)
我使用文件处理功能编辑了您的代码。这是一个好方法,因为它省略了用户在回答后可能输入的所有无效输入。像:
75467 jhfjkghsj;kfjgklj
这是您编辑的代码:
struct bookinfo {
char title[40];
char author[25];
double price;
int pages;
}; // Header File "bookinfo"
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int ctr;
struct bookinfo books[3]; // Array of three structure variables
// Get the information about each book from the user
for(ctr = 0; ctr < 3; ctr++)
{
printf("What is the name of the book #%d?\n", (ctr+1));
gets(books[ctr].title);
fseek(stdin,0,SEEK_END);
puts("Who is the author? ");
gets(books[ctr].author);
fseek(stdin,0,SEEK_END);
puts("How much did the book cost? ");
scanf(" $%f", &books[ctr].price);
fseek(stdin,0,SEEK_END);
puts("How many pages in the book? ");
scanf(" %d", &books[ctr].pages);
fseek(stdin,0,SEEK_END);
}
// Print a header line and then loop through and print the info
printf("\n\nHere is the collection of books: \n");
for(ctr = 0; ctr < 3; ctr++)
{
printf("#%d: %s by %s", (ctr+1), books[ctr].title, books[ctr].author);
printf("\nIt is %d pages and cost $%.2f", books[ctr].pages, books[ctr].price);
printf("\n\n");
}
return (0);
}
我在每次输入后都添加了fseek功能。此函数将文件指针移动到您想要的位置。这里,距离stdin的末尾有0个偏移量