c++ - MV Studio C++ - 二维数组不适用于文件
问题描述
我是 C++ 新手(所以,请原谅我的 C++ 暴行),并再次向 Stack Overflow 上乐于助人的人寻求帮助。我必须 创建一个从 2 个文件中读取的二维数组程序,一个以一年中的月份命名,另一个是整数值表。
我有四个功能可以在切换菜单中中断主功能。功能一应该显示整个图表,月份在第一列的下方。 月&&值。所以,基本上月份都在第一列 [0] 中,并且值与月份相邻(在最上面的行会有一些额外的天赋来描述值是什么,但我相信我可以弄清楚那部分)
然后第二个函数获取第1列和第 2列中的所有值(所以不是 [0],因为那是月份)。
然后我的第三个函数获取列 [4] 和 [5] 并将它们相加并显示总数,月份在值的左侧。
我的第四个也是最后一个函数将允许用户输入给定的月份并在月份后的第一列中显示值。
我所有的输出都不会以 0 结尾。
如果您能给我任何提示/建议/代码示例/任何东西,将不胜感激,我感谢您花时间阅读这些垃圾代码,希望我能在编程方面做得更好!这是我的代码,我还没有完成它,因为当我尝试要求程序显示四个选项之一时,我不断出现显示 0 的循环错误。
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
// Global Array Size
const int ROWS = 12, COLUM = 9;
int parkChart[ROWS][COLUM];
//Function Prototypes
void displayCodes(const int[][COLUM], int);
int totalRecNonRec(const int[][COLUM], int rows, int total);
int tentRvByMonth(const int[][COLUM], int);
int displayMonth(const int[][COLUM], int, string);
int main()
{
// Define variables
const int DISPLAY = 1, REC_NONREC = 2, TENT_RV = 3, PER_MONTH = 4;
int response, total = 0;
string month;
ifstream inputFile;
inputFile.open("Months.txt");
ifstream inputFile2;
inputFile2.open("Vistors.txt");
cout << "Visitors to National Park" << endl;
// Do While Loop to display the menu
cout << "Enter 1 to display data" << endl;
cout << "Enter 2 to display total number of recreation and non-recreational visitors" << endl;
cout << "Enter 3 to display total tent and RV campers by month" << endl;
cout << "Enter 4 to display the number of recreational visitors for a certain month" << endl;
cout << "Enter any other number to exit" << endl << "\n";
cin >> response;
do
{
switch (response)
{
case(DISPLAY):
{
inputFile;
inputFile2;
displayCodes(parkChart, ROWS);
break;
}
case(REC_NONREC):
{
totalRecNonRec(parkChart, ROWS, total);
break;
}
case(TENT_RV):
{
tentRvByMonth(parkChart, ROWS);
break;
}
case(PER_MONTH):
{
displayMonth(parkChart, ROWS, month);
break;
}
default:
{
// Closes the file and exits
inputFile.close();
exit(0);
}
}
} while (response == 1 || response == 2 || response == 3|| response == 4);
system("pause");
return 0;
}
// Four Functions
// Display Function
void displayCodes(const int parkChart[][COLUM], int ROWS)
{
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLUM; col++)
{
cout << parkChart[row][col] << "\t ";
}
cout << endl;
}
}
// Totals/Display Rec. & Non Rec. Visitors to the park + adds all months for visitors (must equal 3,350,493)
int totalRecNonRec(const int parkChart[][COLUM], int rows, int total)
{
// for loop that adds all the elements in [0-11,1]&&[0-11,2]
for (int col = 0; col < 1; col++)
{
total = 0;
for (int row = 0; row < ROWS; row++)
total += col;
cout << "The total number of recreational and non-recreational visitors: " << total << endl;
}
return total;
}
// Totals/Display Number of Tent & RV Campers by month (needs to add rv + tent) and show all months
int tentRvByMonth(const int parkChart[][COLUM], int rows)
{
for (int row = 0; row < ROWS; row++)
{
int total = 0;
for (int col = 4; col < 5; col++)
{
total += parkChart[row][col];
cout << parkChart[row][col] << " " << (col + 1) << endl;
}
cout << endl;
}
return 0;
}
int displayMonth(const int parkChart[][COLUM], int rows, string month)
{
// Prompt the user to enter a month/Display the number of Rec. Visitors for that month + Failsafe of non-months
// Need a for loop to search for the user given month, and then display the [month, 1]
cout << "Enter the month you want the number of recreational visitors\n";
getline(cin, month);
// search in first column of array
cout << "For the month of " << month << " there were " << " recreational visitors" << endl;
return 0;
}
解决方案
推荐阅读
- javascript - TranslateX 每一个transitionend
- java - html页面拒绝应用样式bootstrap.min.css
- python - django.core.exceptions.ImproperlyConfigured:无法导入 ASGI_APPLICATION 模块“project.routing”
- wordpress - Demo的默认主页不能被覆盖
- keras - 验证步骤很冗长
- android - 密码可见性切换android没有textinputlayout
- sql - 在我的情况下,为什么 SQL Server 中的周转换失败?
- mysql - 如何设计具有动态列的关系数据库?
- variable-assignment - 真的将参数绑定到参数吗?(Perl6)
- c# - 如果您刚刚获得 ClientId 和 ClientSecret 并且没有刷新令牌,那么为 API 更新访问令牌的最佳做法是什么?