- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力编写一个需要为类编写的程序。我需要编写一个程序,从用户那里读取城市名称,然后要求用户输入月份,后跟逗号,然后输入该月的平均降雨量。这是手头的任务:
Write a program that reads data from the user about the quarterly rainfall in a certain city and displays it on the screen. Your program must prompt the user the following information:
The name of the city. You may assume it is at most 50 characters.
The months and amount of rainfall received for each month. You may assume, for each month, the user will enter both the month and rainfall received values in one line separated by a comma with the month having a length of three characters.
The format of the output in terms of field width and precision.
Your program must then calculate the average rainfall and produce output complying with the following specifications:
Display the name of the city followed by a blank line.
For each monthly rainfall value, display the name of the month left justified in the user specified field width followed by the rainfall for that month displayed right justified formatted using fixed-point notation, with the user specified precision, in a field of 15 spaces wide.
Finally follow last month's rainfall with a blank line and then on a separate line display the sentence "Average rainfall: " left justified in the user specified field width followed by the average rainfall right justified in fixed-point notation with the user specified precision and in a field of 15 spaces wide.
现在这是我到目前为止的代码,我正在努力让代码正确,让用户输入自己的逗号,代码编译并运行,但在输入后按回车键后,它不会打印下个月的行第一个月的详细信息,只留下一个空行:
int main ()
{
char cityName[50];
char monthName[3];
char comma;
float aveRain = 0;
int monthCount = 1;
float aveResult = 0;
int fieldWidth;
int outputPrecision;
puts("This program calculates the average quarterly rainfall for a given city.");
printf("Enter the name of the city: ");
scanf("%s" , &cityName[50]);
while( monthCount <= 3 )
{
printf( "Enter the month and average rainfall of month %d: " , monthCount);
scanf( "%s %s %f" , &monthName[3] , &comma , &aveRain);
monthCount = monthCount + 1;
}
}
最佳答案
你的scanf坏:
char MonthName[3]
(仅 2 个字符且终止 null)并读取 &monthName[3]
就在数组末尾:未定义的行为%s
进行读取:因为 %s
读取至少一个字符并写入终止 null,这里您再次写入 任何有 UB 的地方你应该:
char MonthName[4]
为终止 null 留出空间cr = scanf("%3s,%f",monthName, &aveRain);
以便:
monthName
中的输入为空顺便说一句,前面的 scanf 也很糟糕:您必须在 char 数组中读取,而不是在它之后读取:
scanf("%49s" , cityName);
关于c - 编写一个 C 程序来计算一个城市的平均季度降雨量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30171892/
是否可以获得维基百科上所有有关系的国家、地区和城市的列表?我找不到任何适合此任务的 API。解析我需要的所有信息的最简单方法是什么?PS:我知道,我可以从其他数据源获取此信息。但我对维基百科感兴趣..
我有一个表“位置”,其结构如下 CREATE TABLE IF NOT EXISTS `locations` ( `id` int(11) NOT NULL, `city_code` int(11
我正在使用地点选择器的 Intent 来获取地点。现在我想将地址以国家、城市、密码、州的形式保存。我怎样才能从地点选择器的地址得到所有这些? 代码: public class NameOfBusine
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
我需要在地理图上绘制一些数据。具体来说,我想强调数据来自的国家和州。 我的数据集是 Year Country State/City 0 2009 BGR Sofia 1
我正在寻找一个可以将用户的输入转换为具有特定规则的新输出的函数。 这条规则是: 第一个字母大写(例如:Paris) 如果字符串的每个单词之间包含两个破折号 (-),则对第二个单词的第一个字母应用大写(
总共可能最多有 1000 个字符串。这些应该硬编码还是存储在数据库中?这些内容经常被访问,因为每次用户想要注册或结帐商品时,他们都需要查看区域/郊区/省/国家/地区列表。 如果我有一堆枚举,我认为性能
我有像国家/州和城市这样的下拉列表。有什么办法可以等到人口下降后再继续进行吗?就像首先加载国家/地区,然后根据选择的国家/地区然后加载州/省,与城市相同...... function populate
我想使用 PHP 和 jQuery 执行以下操作 https://www.careerbuilder.com/share/register.aspx?sc_cmp1=JS_LoginASPX_RegN
我正在建立一个网站,其中将有很多公司及其业务列表,因此他们将有他们的地址、城市、州、邮政编码等。 我在这里找到了这个网站: http://jesseprice.com/mysql-city-state
如何使用单一选择器 Objective-C 来管理国家/州/城市的值(value)? 当我尝试它时,它会因索引更改而崩溃。它适用于城市城市和州,但与城市有关。哪个库会更好用? 查看下面的代码 - (v
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我有一张 homes 表,其中包含国家、城市、大陆字段。 当某些人搜索房屋时,如果他输入例如“rom”我想返回 罗马 (30) 罗马尼亚 (120) 我该怎么做? 编辑使用此处的搜索表单 http:/
我希望将 HTML5 地理定位功能集成到网站中。我遇到的问题是在选定的列表中获取最近的城市/城镇。该网站仅在一定数量的城镇和引用中受支持/营销,因此浏览器可以找到其位置并选择最近的城镇或城市。听起来很
我想做一个马来西亚州和城市的下拉列表。 这是我的初始页面:(test3.php) 当我选择KL时,我对该城市的期望输出如下: 当我选择雪兰莪时,我对该城市的预期输出如下: 状态数据 json ($st
我已经实现了一个仅使用整数类型的邻接矩阵图。(我在这里提到的所有内容都考虑 C++) 我正在实现另一个图表,该图表将使用我的旧实现方案接收顶点中的城市和边中的距离。我想知道这是一个好主意还是我应该使用
我正在寻找一些关于在星际争霸/帝国时代等游戏中寻找基础设施开发策略的论文。这些游戏的基本特征是: 连续时间(好吧 - 它可以分成 10 秒的周期,或类似的时间) 许多描述增长的变量(许多资源、建筑物级
我目前有 3 个表存储有关世界上所有主要城市、与这些国家对应的每个地区/州以及这些州/地区中的每个城市的信息。 现在我的数据库中还有大约 6 个其他表,例如需要完全相同的 5 列的用户或组织表:地址、
我正在使用 django 构建一个旅游网站。当用户输入目的地城市名称(或兴趣点,如黄石)时,我想进行 ajax 自动建议。问题是我如何获得建议数据库?有没有网络服务?最好它也能支持外国城市。非常感谢。
我是一名优秀的程序员,十分优秀!