[英]Why does this code only recognize the first line in the text file? Inputting any number greater than 1 returns "Atomic Number Not Found"
I wanted to make a program that would have a user input a number, and it would search a text file I made on whether or not that number is an atomic number, then pull all the details from the periodic text file and display the info.我想制作一个程序,让用户输入一个数字,它会搜索我制作的文本文件,以确定该数字是否是原子序数,然后从周期性文本文件中提取所有详细信息并显示信息。 However, when the user inputs any number greater than 1, it displays atomic number not found.
但是,当用户输入任何大于 1 的数字时,它会显示未找到原子序数。
A solution I have tried is to make a for loop that would continuously go line by line until it finds it, but that did not work, and only made it display "atomic number not found" several times.我尝试过的一个解决方案是制作一个 for 循环,该循环会不断地逐行运行,直到找到它为止,但这不起作用,并且只会让它多次显示“未找到原子序数”。
#include "pch.h"
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream file_("periodicInfo.txt");
int atomNumReq;
cout << "Please enter atomic number: ";
cin >> atomNumReq;
if (file_.is_open())
{
string Symbol;
string Name;
int atomNum;
int mass;
char line;
while (file_ >> atomNum >> Symbol >> Name >> mass)
{
if (atomNumReq == atomNum) {
cout << atomNum << " " << Symbol << " " << Name << " " << mass << '\n';
}
else {
cout << "Atomic Number Not Found.";
}
}
file_.close();
}
return 0;
}
I expect it to display the line information for the atomic number.我希望它显示原子序数的行信息。 I have the text file arranged so that line 1 starts with the number 1, along with the text "1 H Hydrogen 1.01", line 2 of the file being "2 He Helium 4.00", etc all the way until the last atomic number.
我安排了文本文件,以便第 1 行以数字 1 开头,以及文本“1 H Hydrogen 1.01”,文件的第 2 行是“2 Helium 4.00”,一直到最后一个原子序数。 When the user inputs the number 1, it displays the first line in the text file correctly.
当用户输入数字 1 时,它会正确显示文本文件中的第一行。 When the user inputs any number greater than 1, it displays "Atomic Number Not Found."
当用户输入任何大于 1 的数字时,它会显示“未找到原子数”。
1.01
is not an integral value, so file_ >> atomNum >> Symbol >> Name >> mass
will read in the 1
into mass
but will leave the .01
in the buffer; 1.01
不是整数值,因此file_ >> atomNum >> Symbol >> Name >> mass
会将1
读入mass
但会将.01
留在缓冲区中; After that, probably everything is messed up.在那之后,可能一切都搞砸了。
Define mass
as ...将
mass
定义为...
double mass;
and things should get better.事情应该会好转。
Further, put the cout << "Atomic Number Not Found.";
此外,把
cout << "Atomic Number Not Found.";
with some proper condition outside the loop;在循环外有一些适当的条件; otherwise you'll get this message for every line that failed before you find the right one.
否则,在找到正确的行之前,您将收到每一行失败的消息。
Fixed it by changing the mass to a double instead of an int.通过将质量更改为双精度而不是整数来修复它。 Another problem that showed up was it displaying "Atomic Number Not Found" for all the numbers previous to it.
出现的另一个问题是它为之前的所有数字显示“未找到原子数”。 For example, if input was 3, it would display the error twice before displaying the third atomic number.
例如,如果输入是 3,它会在显示第三个原子序数之前显示两次错误。 To fix this, I put in a new if statement that solved the problem.
为了解决这个问题,我添加了一个新的 if 语句来解决这个问题。
if (atomNumReq != atomNum) continue;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.