通过文本文件读取数字数据并将其存储到变量

时间:2018-10-17 05:13:50

标签: c++ file

到目前为止,这是我的代码。每次都会读取文件,但是输出看起来像这样:

-92559631349317830736831783200707727132248687965119994463780864.000000

读取两个值。我最近才将变量从float更改为double。我意识到我有未使用的变量,因此,这甚至还不是整个代码。如果我能得到足够的帮助以在cout语句中显示正确的值,将不胜感激。我的inUserAccount语句需要做什么?

到目前为止,我已经在这里检查了很多资源,没有任何东西使用到目前为止所学到的相同代码或书中所包含的内容。我们正在读取的文件如下所示:

4567.89

15.98

一个是支票帐户,另一个是储蓄帐户。 预先谢谢你。

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main() {

fstream inUserAccount;
double checkingBalance, savingsBalance;
char ChkorSav, option, yesorNo;
int  count=0;
bool validInput=true ;

while (validInput = true) {     // my loop for the whole ATM cycle
    cout << "Welcome to your ATM!\nPlease choose from the options below:\n"
        << "1:Deposit\n2:Withdraw\n3:Balance Inquiry\n4:Transfer Money\n";

    setprecision(2);

    while (validInput) {
        cin >> option;
        if (option == '1' || option == '2' || option == '3' || option == '4')
            validInput = false;

        else cout << "Please enter a valid option. \n";

        if (validInput == true && count >= 3)
            cout << "(Enter the corresponding number to the option you desire) \n";
        count++;
    }
    count = 0;          // resetting my loop variables
    validInput = true;


        inUserAccount.open("lab5data.txt");
        double savings, checking;
        inUserAccount >> fixed >> savings >> checking;
        cout << fixed << savings << endl << checking;

1 个答案:

答案 0 :(得分:0)

如果我的理解正确,那么您的问题是读取一个文件,该文件的不同行内的数值将分配给double s ...

使用此魔术功能:

template<class T>
std::vector<T> read_stream(std::ifstream& stream)
{
    std::vector<std::string> numerical_strings;               // A vector which stores all the lines...
    std::stringstream ss;                                     // Creating a string stream...
    ss << stream.rdbuf();                                     // Reading the contents of file into the string stream...
    std::string temporary;                                    // A temporary string that iterates over all lines...
    while (std::getline(ss, temporary, '\n'))                 // Looping over all lines...
        if (!temporary.empty())                               // We will only push it inside the vector if the line is not empty...
            numerical_strings.emplace_back(temporary);        // Push line inside the vector...
    std::vector<T> numbers;                                   // A vector of "numbers" of type you want...
    for (auto & elem : numerical_strings)                     // Iterate over the string vector...
    {
        auto temp_number = T(strtold(elem.c_str(), nullptr)); // Your usual conversion function...
        if (errno == ERANGE)                                  // Checking for errors...
            continue;                                         // If the line is alpha numeric or something like that, skip it...
        numbers.emplace_back(temp_number);                    // If the line is a valid number, push it inside the vector...
    }
    return numbers;                                           // Return the vector of the numbers of arbitrary type...
}


一个演示其用法的示例:

int main()
{
    std::ifstream some_file("account_file.txt");
    auto a = read_stream<long double>(some_file);
    for (auto b : a) // It will iterate through all the numeric lines, not just the first two...
        std::cout << std::fixed << std::setprecision(2) << b << std::endl;
    return 0;
}
  

输出:
  4567.89
  15.98


  

注意:对于您的问题,您只想要向量的前两个元素...因此,只需使用a.empty()检查向量是否为空,然后使用{{ 1}}和第二个数字,带有a[0] ...


  

此外,请确保您在顶部包含以下内容:

a[1]

亲切的问候,

Ruks。