简体   繁体   English

为什么这段代码只识别文本文件中的第一行? 输入任何大于 1 的数字将返回“未找到原子数”

[英]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.

相关问题 为什么这段代码报告 -31 大于 6? - Why does this code report that -31 is greater than 6? C ++阶乘程序为任何大于2的数提供无穷大 - C++ factorial program give infinity for any number greater than 2 为什么在头文件的第一行放一个随机数? - Why put a random number on the first line in header file? 有没有简单的方法从文件中读取一行,将第一个文本部分拆分为字符串,然后将最后一个数字部分拆分为浮点数? - Is there any easy way to read a line from a file, split the first text part into a string, then split the last number part into a float? 如果小数位数大于0,如何仅使用printf打印小数? - How to only print decimals using printf if the number of decimals is greater than 0? 代码仅从源文件读取文本的第一行 - code only reads first line of text from source file 获取大于一个数字的元素数量 - Get number of elements greater than a number 给定数字n,则打印系列中的第一个大于或等于n的数字 - Given a number n, print the first number of the series greater than or equal to n 读取文本文件时,如何跳到每行最后一个数字之后的下一行? - How to skip to next line after the last number found in each line when reading a text file? 为什么我的代码只读取文件的最后一行? - Why does my code only read the last line of the file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM