在单独的数据中显示数组数据

Display array data in seperate data

本文关键字:数据 显示 数组 单独      更新时间:2023-10-16

我是一名学生,目前要学习一个非常基础的计算机编程模块。问题是,他们并不完全教授C++编程语言,而是教授其中的一些内容,并期望我们能够自己拼凑拼图并构建一个程序来磨练我们的"问题思维技能"。

我希望在数组中显示数据,隐藏在单独的项目中。换句话说,数据在一个数组中,在一个称为 data 的单独项目中,而在主程序中,我想在用户选择时显示数据。

例如,在每月天气数据的上下文中,我怎样才能使信息排列成数组,以显示用户何时输入某一天。因此,当用户在 21 中键入时,它应该显示该月21日的温度,也显示在数组的第20个索引处。

这是我到目前为止所做的:

printf("Enter selected month.n"); 
scanf_s("%c", &month);
if (month==October)
{
    printf("Enter selected date, from 1st to 31st.n"); 
    scanf_s("%d", &octoberTemperature[i]); 
    printf("%.2fn", octoberTemperature[i]);
}

非常欢迎任何帮助! :)

&octoberTemperature[i]你遇到了一个问题 - 你正在尝试写入数组的对象。你应该这样做:

printf("Enter selected month.n"); 
scanf_s("%c", &month); 
unsigned short day = 0;
if (month==October)
{
    printf("Enter selected date, from 1st to 31st.n"); 
    scanf_s("%d", &day);         
    printf("%.2fn", octoberTemperature[day - 1]);
    // as it starts from 0
    // if your array containing the month report starts from 0 then it has to be (day - 1)
}

在您的代码中,您实际上已经输入了您希望在数组中查找天气的日期并打印了相同的数组。现在不要使用相同的 var 来获取日期作为输入

scanf ("%d",&day)//I assumed day as a var to receive the month day cout<<Octobertemperature[day-1]; // now if u already have a array containing the data of the weather report..then this will solve your problem.