【发布时间】:2022-01-18 02:41:03
【问题描述】:
我正在尝试编写一个程序,允许用户通过使用名称、原子序数或符号搜索来搜索元素周期表上的任何元素。目前名称和原子序数工作正常,但是当我尝试复制和粘贴符号的代码时,它突然不起作用。甚至在 cout 中进行硬编码
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
std::vector<std::string> readAtomicNumber(std::string file_name,std::string search_term);
std::vector<std::string> readName(std::string file_name,std::string search_term);
std::vector<std::string> readSymbol(std::string file_name,std::string search_term);
int main()
{
int searchchoice;
int AtomicNumber;
string Name;
string Symbol;
string term;
cout << "How will you be searching?\nYou may search using;\n1 = Element Name\n2 = Element Symbol\n3 = Atomic Number\n4 = Show me the entire Periodic Table.\n\n";
cin >> searchchoice;
if(searchchoice == 4)
{
//something here to cout the entire periodic table
}
else if(searchchoice == 3)
{
cout << "\n\nWhat is the Atomic Number of the Element you are searching for?\n";
cin >> term;
}
else if(searchchoice == 2)
{
cout << "\n\nWhat is the Symbol of the Element you are searching for?\n"; // going to need something to return
cin >> term; // "that is not a name/atomic number etc"
} // incase of a false input
else if(searchchoice == 1)
{
cout << "\n\nWhat is the Name of the Element you are searching for?\n";
cin >> term;
}
else
{
cout << "\n\nError. Please re-run the program, and input 1, 2, or 3.\n";
return 0;
}
if(searchchoice == 3)
{
std::vector<std::string> data = readAtomicNumber("PeriodicTableupdated",term); //atomic number
}
else if(searchchoice == 2)
{
std::vector<std::string> data = readSymbol("PeriodicTableupdated",term); //symbol
}
else if(searchchoice == 1)
{
std::vector<std::string> data = readName("PeriodicTableupdated",term); //name
}
return 0;
}
std::vector<std::string> readAtomicNumber(std::string file_name,std::string search_term) //READ ATOMIC NUMBER
{
std::vector<std::string> record;
std::ifstream file;
file.open(file_name);
bool found_record = false;
std::string field_one; //atomic number
std::string field_two; // name
std::string field_three; // symbol
std::string field_four;
while(getline(file,field_one,',') && !found_record)
{
getline(file,field_two,',');
getline(file,field_three,',');
getline(file,field_four,'\n');
if(field_one == search_term)
{
found_record = true;
record.push_back(field_one);
record.push_back(field_two);
record.push_back(field_three);
record.push_back(field_four);
}
}
std::cout << "\nThat Element is: " << record[1] << "\nAtomic Number:\tName:\t\tSymbol:\t\tAtomic Mass:\n" << record[0] << "\t\t" << record[1] << "\t" << record[2] << "\t\t" << record[3];
return record;
}
std::vector<std::string> readName(std::string file_name,std::string search_term) // READ NAME
{
std::vector<std::string> record;
std::ifstream file;
file.open(file_name);
bool found_record = false;
std::string field_one; //atomic number
std::string field_two; // name
std::string field_three; // symbol
std::string field_four;
while(getline(file,field_two,',') && !found_record)
{
getline(file,field_one,',');
getline(file,field_three,',');
getline(file,field_four,'\n');
if(field_one == search_term)
{
found_record = true;
record.push_back(field_one);
record.push_back(field_two);
record.push_back(field_three);
record.push_back(field_four);
}
}
std::cout << "\nThat Element is: " << record[0] << "\nAtomic Number:\t\tName:\t\tSymbol:\t\tAtomic Mass:\n" << record[1] << "\t\t\t" << record[0] << "\t" << record[2] << "\t\t" << record[3];
return record;
}
std::vector<std::string> readSymbol(std::string file_name,std::string search_term) // READ SYMBOL
{
std::vector<std::string> record;
std::ifstream file;
file.open(file_name);
bool found_record = false;
std::string field_one; //atomic number
std::string field_two; // name
std::string field_three; // symbol
std::string field_four;
while(getline(file,field_three,',') && !found_record)
{
getline(file,field_one,',');
getline(file,field_two,',');
getline(file,field_four,'\n');
if(field_three == search_term)
{
found_record = true;
record.push_back(field_one);
record.push_back(field_two);
record.push_back(field_three);
record.push_back(field_four);
}
}
std::cout << "\nThat Element is: " << record[2] << "\nAtomic Number:\t\tName:\t\tSymbol:\t\tAtomic Mass:\n" << record[0] << "\t\t\t" << record[1] << "\t" << record[2] << "\t\t" << record[3];
return record;
}
我使用的元素周期表 csv 有 118 个元素,因此我将只包括前 10 个元素。
AtomicNumber,Element,Symbol,AtomicMass
1,Hydrogen,H,1.007
2,Helium,He,4.002
3,Lithium,Li,6.941
4,Beryllium,Be,9.012
5,Boron,B,10.811
6,Carbon,C,12.011
7,Nitrogen,N,14.007
8,Oxygen,O,15.999
9,Fluorine,F,18.998
10,Neon,Ne,20.18
【问题讨论】:
-
您是否尝试过在调试器中逐行运行代码,同时监控所有变量的值,以确定您的程序在哪个点停止按预期运行?如果您没有尝试过,那么您可能想阅读以下内容:What is a debugger and how can it help me diagnose problems? 您可能还想阅读以下内容:How to debug small programs?。
-
我对复制+粘贴其他代码的代码量感到困扰。这是创建在某些方面有效但在其他方面无效的代码的常见方法。为什么
readSymbol在field_one之前读取field_three?这闻起来像是复制+粘贴错误。 -
在我给出答案之前,请在互联网上搜索“C++ class read CSV”。
-
如果您的搜索失败,则
record为空并且您的代码具有未定义的行为。 -
@DrewDormann 我这样做是因为我认为这就是我试图读取符号的 csv 的引用方式。符号是 field_three,而 field_one 是原子序数。我错了吗?
标签: c++ arrays vector record getline