使用数据结构分别注册某些字符数组
Registering certain character arrays separately using Data-Structures
因此,我正在尝试制作一个租赁程序,该程序读取带有汽车列表的文件(年份,制造,型号,价格以及是否可用)。我还必须做出显示所有汽车和让用户查看特定汽车等选项(例如,他们输入1,它将在文本文件中显示最前面的汽车)。这就是我困惑的地方,我试着做了几个车,如car1,car2等。但是我在注册第一个读取的car到car1时遇到了麻烦。
这是一个示例文件
CarData.txt
2014 Toyota Tacoma 115.12 1
2012 Honda CRV 85.10 0
2015 Ford Fusion 90.89 0
2013 GMC Yukon 110.43 0
2009 Dodge Neon 45.25 1
2011 Toyota Rav4 65.02 1
2012 Mazda CX5 86.75 1
2016 Subaru Outback 71.27 0
2015 Ford F150 112.83 1
2010 Toyota Corolla 50.36 1
源代码:#include <iostream>
#include <fstream>
using namespace std;
struct car
{
int year;
char make[10];
char model[10];
float price;
int available;
}car1[1], car2[1], car3[1], car4[1], car5[1], car6[1], car7[1], car8[1], car9[1], car10[1];
void copy_string(char d[][50], char s[][50]);
int my_strcmp(char *a, char *b);
// Main Function
int main ()
{
// declare variables
int choice;
car carLib[10];
char array[30][50];
char filename[10];
ifstream carInData;
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
cout << " 1 - Show Carsn";
cout << " 2 - Rental Costn";
cout << " 3 - Most Expensive Carn";
carInData.open(filename);
cin >> choice;
if(carInData.is_open());
{
int count = 0;
// read list of names into array
for( count; count < 1; count++){
carInData >> car1[count].year >> car1[count].make >> car1[count].model >> car1[count].price >> car1[count].available;
carInData >> car2[count].year >> car2[count].make >> car2[count].model >> car2[count].price >> car2[count].available;
switch (choice){
case 1:
cout << car1[count].year << " " << car1[count].make << " " << car1[count].model << " " << car1[count].price << " " << car1[count].available << " " << "n";
cout << car2[count].year << " " << car2[count].make << " " << car2[count].model << " " << car2[count].price << " " << car2[count].available << " " << "n";
break;
}
}
}
return 0;
}
// copy function
void copy_string(char d[], char s[]) {
int c = 0;
while (s[c] != ' ') {
d[c] = s[c];
c++;
}
d[c] = ' ';
}
// string copy
int my_strcmp(char *a, char *b)
{
while (*a && *b && *a == *b) { ++a; ++b; }
return *a - *b;
}
这是一个示例源代码,如何使用结构类型的数组,如car carLib[300];
:
#include <iostream>
struct car
{
int year;
char make[10];
char model[10]; // that's only 9 letters for zero-terminated strings
float price;
int available;
};
int main()
{
constexpr size_t maxCarLib = 300;
car carLib[maxCarLib]; // array of memory reserved for several cars
size_t carLibCount = 0; // current allocation of array (zero cars entered)
carLib[carLibCount++] = car {1993, {"Nohda"}, {"XYZ"}, 123.45, 0};
carLib[carLibCount++] = car {1994, {"Frod"}, {"Peon"}, 543.21, 1};
carLib[carLibCount++] = car {1977, {"Nohda"}, {"ABC"}, 3.32, 0};
std::cout << "Cars in library: " << carLibCount << std::endl;
for (size_t carIndex = 0; carIndex < carLibCount; ++carIndex) {
const car & carI = carLib[carIndex];
printf("Car at index %lu: %d %s %s %f %dn",
carIndex, carI.year, carI.make, carI.model, carI.price, carI.available);
}
// Find most expensive car
float mostExpensivePrice = 0; // most expensive price found
size_t mostExpensiveIndex = carLibCount; // index points beyond array ("no car")
for (size_t carIndex = 0; carIndex < carLibCount; ++carIndex) {
if (carLib[carIndex].price <= mostExpensivePrice) continue;
// the car at carIndex is more expensive than old maximum, remember it
mostExpensivePrice = carLib[carIndex].price;
mostExpensiveIndex = carIndex;
}
if (mostExpensiveIndex == carLibCount) {
std::cout << "Most expensive car not found." << std::endl;
} else {
const car & carI = carLib[mostExpensiveIndex];
printf("Most expensive car at index %lu: %d %s %s %f %dn",
mostExpensiveIndex, carI.year, carI.make, carI.model, carI.price, carI.available);
}
}
现场演示
请注意,这只是数组索引的例子,这种"老C"代码风格非常容易出现索引越界、覆盖内存或访问错误内存的错误。这就是为什么在现代c++中,你可以使用std::vector<car> carLib;
来代替,它更适合这样的任务。
#include <iostream>
#include <vector>
struct car
{
int year;
char make[10];
char model[10]; // that's only 9 letters for zero-terminated strings
float price;
int available;
};
int main()
{
std::vector<car> carLib;
carLib.push_back(car {1993, {"Nohda"}, {"XYZ"}, 123.45, 0});
carLib.push_back(car {1994, {"Frod"}, {"Peon"}, 543.21, 1});
carLib.push_back(car {1977, {"Nohda"}, {"ABC"}, 3.32, 0});
std::cout << "Cars in library: " << carLib.size() << std::endl;
for (const auto & kar : carLib) {
printf("%d %s %s %f %dn",
kar.year, kar.make, kar.model, kar.price, kar.available);
}
if (0 < carLib.size()) {
// indexing trough size_t index still possible with vector
std::cout << "Last car price is: " <<
carLib[carLib.size()-1].price << std::endl;
// Last valid index being size()-1, because first index is 0
}
}
但是C-like对你学习有关计算机内存的细节是有好处的,而且它确实向你介绍了"float
不是一个很好的变量类型",正如现场演示所示。所以在调试器中打开它,并找到一些内存窗口,并研究数据实际上是如何在内存中布局的,如何10个字母长的汽车名称使您的结构有问题,等等…
std::vector
以与裸数组相同的方式存储结构,只有少量的辅助数据,并根据需要动态地保留内存,因此首先理解数组示例(及其缺点)同样重要。
相关文章:
- 指向指向字符数组的指针数组的指针
- 比较字符数组
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 错误:字符数组的初始值设定项太多
- 对字符数组中的元素执行逐位操作
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- C++ 传递二维字符数组
- 无法在 C++ 中输入字符数组
- 字符数组中的元素数
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 使用字符数组作为 Map 中的键
- C++:__aligned(__alignof__) 导致字符数组数据出现问题?
- 按顺序声明的字符数组重叠
- 在 C++ 中字符串的大小与字符数组的大小
- 寻找一种更好的方法来表示无符号字符数组
- 如何打印 2D 字符数组C++
- 从字符数组的元素中减去'a'是什么意思
- 我是否不正确地集中了这些字符数组?