C 数组未从文件中获取正确的输入
C++ Array not taking correct input from file
免责声明:我是编程的初学者,所以我说的话听起来真的很愚蠢
我必须为学校做一个"电话目录"。该程序还不完整,但是在继续之前,我需要修复一些事情。Array TelephonEnumbers要么无法正确存储文件中的数字,要么没有显示它们。对于SeaerChreCords功能,正确显示文件中的第一个数字,第二个数字显示为" 2147483647",其余数字显示为" 0"。修改函数也不会更改数字,我在函数中使用WILE确认了这一点。但是,字符串阵列的工作原理很好。有人可以解释我在做什么吗?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string TelephoneNames[100];
int TelephoneNumbers[100];
void ModifyRecords(); //Function to Modify Records
void SearchRecords(); //Function to Search Records
void DeleteRecords(); //Function to Delete Records
int main()
{
fstream inputFile;
fstream outputFile;
char choice;
inputFile.open("Telephone Names.txt"); //To store
for (int count=0;count<100;count++) //file names
{ //into a
inputFile >> TelephoneNames[count]; //string
}
inputFile.close();
inputFile.open("Telephone Numbers.txt");//To store
for (int count=0;count<100;count++) //file #'s
{ //into a
inputFile >> TelephoneNumbers[count];//string
}
inputFile.close();
//Display options available
cout << " Hello, do you want to:n";
cout << " ======================n";
cout << "-Modify Records|Enter Mn";
cout << "-Search Records|Enter Sn";
cout << "-Delete Records|Enter Dn";
//Store choice
cin >> choice;
//Send to different function
if (choice=='M'||choice=='m')
{
ModifyRecords();
}
if (choice=='S'||choice=='s')
{
SearchRecords();
}
return 0;
}
void ModifyRecords()
{
string name;
string newname;
int newnumber;
int count=0;
cout << "Enter the name of the person: ";
cin >> name;
for (count=0;TelephoneNames[count]!=name;count++)//To determine where in the strings the new numbers need to be
{
}
cout << "Enter the new name of the person: ";
cin >> newname;
cout << "Enter the new number of the person: ";
cin >> newnumber;
TelephoneNames[count]={newname};
TelephoneNumbers[count]={newnumber};
count=0;
while (count<6)
{
cout << TelephoneNames[count] << endl;
cout << TelephoneNumbers[count] << endl;
cout << endl;
count++;
}
}
void SearchRecords()
{
string name;
int count=0;
cout << "Enter the name of the person you would like to find: ";
cin >> name;
for (count=0;TelephoneNames[count]!=name;count++)//To determine where in the strings the new numbers need to be
{
}
cout << "Name: " << TelephoneNames[count] << endl;
cout << "Number: " << TelephoneNumbers[count] << endl;
}
由于没有任何答案,而且我目前还没有确切地看到问题,我将提供一些建议,如何在代码中找到问题。在任何编程情况下,当您找不到错误时,第一个任务是尽可能精确地找到它,并检查所有输入数据和假设。通常,调试器用于此类目的,但是您可以在创建程序的最终版本之前将文本输出。首先,您必须检查您真的从文件中收到了名称和电话:
inputFile.open("Telephone Names.txt"); //To store
for (int count=0;count<100;count++) //file names
{ //into a
inputFile >> TelephoneNames[count]; //string
cout << TelephoneNames[count] << endl; //WE MUST SEE WHAT IS REALLY STORED IN TelephoneNames
}
inputFile.close();
inputFile.open("Telephone Numbers.txt");//To store
for (int count=0;count<100;count++) //file #'s
{ //into a
inputFile >> TelephoneNumbers[count];//string
cout << TelephoneNumbers[count] << endl; //WE MUST SEE WHAT IS REALLY STORED IN TelephoneNumbers
}
inputFile.close();
好的,当您检查它时,您可以明确确定数据中没有问题,我们可以移至SeaerChrecords函数执行相同的过程。我们必须在搜索时检查正在发生的事情:
for (count=0;TelephoneNames[count]!=name;count++)//To determine where in the strings the new numbers need to be
{
cout << "Search step: " << count << " name " << name << " found name " << TelephoneNames[count] << " number " << TelephoneNumbers[count] << endl;
}
这样做,您将很快找到错误。问题可以是输入文件格式,"名称"和存储的名称格式等的差异
。我将提供一些其他建议,如何改善代码。1(尝试将常见的记录数量(const int number_of_records = 100;仅在任何地方放置100'(使用常见的事物,它将减少工作和可能的错误。2(尝试检查您编程的所有可能的问题,如果某个数据错误的数据是错误的。如果您现在的文件中的记录少于100个记录将会发生什么?程序会暗中或无声阅读不适当的数据,这甚至更糟。检查您是否在阅读的任何步骤中都没有到达文件结束,以及当前的检查您已访问了您的记录数量并在不适当的数据时做某事。3(检查周期中可能的条件可能的问题,以免将它们运行无限的次数。现在您的状况for(count=0;TelephoneNames[count]!=name;count++)
如果没有这样的名称或仅在100或更多的情况下粉碎程序,将永远执行。您应该检查计数不会超过该值。祝你好运!
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 如何从用户那里获取输入并将其存储在向量中?
- 获取通过重定向传入的输入文件的名称?
- Cryptopp:获取密码输入的填充字符串
- 从格式化输入操作中获取读取字符数
- 如何获取用户输入的算术表达式,如 5-8+7*4-8+9 或 1+5?
- 我无法从 C# 获取 PInvoke 的输入参数以C++ DLL 以用作 IntPtr 的输出
- 我需要帮助在一个类中输入两个字符串并获取其输出
- 用于获取枚举值的用户输入
- C++如何使用表达式获取用户输入?
- 从 cin 获取 c++ 中结构中多个枚举的输入
- 代码在执行之前终止,并且没有从用户那里获取输入
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 推荐一种在未输入获取行分隔符时引发异常的方法?
- 输入获取线的结尾
- 线程不刷新数据,无法从标准输入获取所有数据
- 如何从同一输入获取两个输入 (C++)
- 在C++中静态地将用户输入获取到类中
- 在使用C++字符串时对特定输入获取bad_alloc
- 使用MPI程序,所有进程都从用户输入获取数据