C ++银行项目;我如何从文件中读取字符串转换为浮点数并加总

用户名

问题陈述:读取客户的支票账户信息的C ++程序计算其账户余额。应开发基于菜单的应用程序以迭代执行以下功能,直到用户请求退出程序为止:1.显示,2。存款,3。提款,4。退出。

到目前为止,这就是我所拥有的。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

//Main function
int main()
{ 
    //Identify type variables
    int x=0;
    float amountinput,famount,sum;
    string firstname, lastname,date,filename,input,ID,amount;
    fstream file;
//Program loop
    while(true)
    {
        //Main menu loop
        do
        {

        cout<<"Enter the number of the option you would like carried out.\n";
        cout<<"1. Summary\n";
        cout<<"2. Deposit\n";
        cout<<"3. Withdraw\n";
        cout<<"4. Quit\n";
        cin>>x;
    }
    while(!x);

    //When Quit is input, break
    if(x==4)
    {
        break;
    }
        //Summary display
        if (x==1)
        {
            cout<<"You have selected option number 1. Summary.\n"<<endl;
            cout<<"Enter your Cust_ID: "; 
            cin>>ID;
            file.open("C:\\Users\\Raggulddon\\Desktop\\C++ supplement
                       \\Cust_"+ID+".dat", ios::in|ios::out|ios::app);

            //IF not found error.
            if(!file)
            {
                cout<<"Sorry your account could not be found\n";
            }
            //Else display all content.
            else
            {
                    cout<<endl<<file.rdbuf()<<"\n";
                    file.close();

            }           
        }
        //Deposit
        else if(x==2)
        {
            cout<<"You have selected option number 2. Deposit.\n";
            cout<<"Please enter you account ID: ";
            cin>>ID;
            file.open("C:\\Users\\Raggulddon\\Desktop\\C++ supplement
                        \\Cust_"+ID+".dat", ios::in|ios::out|ios::app);

            if(!file)
            {
                cout<<"Sorry the requested account could not be located.\n";
            }
            else
            {
            file>>firstname>>lastname;
            cout<<endl<<firstname<<" "<<lastname<<endl; 
                while(!file.eof())
                {
                    file>>date>>amount;
                    //
 %%%%%%%%%%%%%%%%%%%//This is mainly where I am missing the lines of code..
                    //                      
                    float atof(string& amount);
                    cout<<date<<"\t\t"<<amount<<endl;
                }


                    cin.get();cin.get();
                cout<<"How much would you like to deposit today.";
                cin>>amountinput;

                cout<<endl;
                file.close();

            }
        }

            else if(x==3);
            else if(x==4);


}       
cin.get();cin.get(); //or system("PAUSE");
return 0;
}

示例文本文件看起来像这样,但是具有不同的行数。

James Bond
01/01/12 200010
03/30/12 -40000
04/30/12 -40000
05/30/12 -40000
06/30/12 -40000
07/30/12 -40000

我设置了数量=字符串,然后我希望每个字符串彼此独立(以便我可以添加它们),然后将它们转换为float或double。

基本上是这样的:

fstream file;
do
file>>date>>amount++; //I know this doesn't work, what will..
while(!file.eof());
float deposit;
finalAmount= deposit+amount;
一些程序员哥们

C ++输入运算符的好处>>是它可以接受不同类型的参数,例如a double

std::string date;
double amount;

file >> date >> amount;

但是,我建议使用std::getlinestd::istringstream解析它:

std::string line;
std::getline(file, line);

std::istringstream iss(line)
iss >> date >> amount;

在有些相关的注释上,请勿使用while (!file.eof())eof实际输入操作失败之前,将不会设置标志,因此,除非您在循环内检查错误,否则将尝试一次读取多次。

相反,例如

while (std::getline(file, line))
{
    ...
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在C中将浮点数转换为没有sprintf的字符串?

来自分类Dev

如何将浮点数转换为字符串而不会丢失用户在 C 中输入的精度?

来自分类Dev

将字符串转换为浮点数在C中?

来自分类Dev

在C ++中,如何从1行中获取字符串,浮点数和整数的输入?

来自分类Dev

将浮点数转换为字符串的最快C ++方法

来自分类Dev

在C ++中将字符串转换为浮点数的问题

来自分类Dev

将字符串转换为浮点数在C问题?

来自分类Dev

尝试在C中使用sscanf()从文件中读取浮点数,整数和字符串的列表无法正常工作

来自分类Dev

C ++将字符串,整数,浮点数从文本文件中读取到数组/字符串中

来自分类Dev

在目标C中浮点数到字符串的转换

来自分类Dev

如何将 txt 文件转换为浮点数组(在 C# 中)?

来自分类Dev

如何在C中的用户输入字符串中将浮点数/整数加倍?

来自分类Dev

如何将具有浮点数的文本文件读取到C中的浮点数组

来自分类Dev

解析浮点数的C字符串

来自分类Dev

c#将字符串十六进制转换为ascii吗?从csv文件读取字符串

来自分类Dev

浮点数转换为C中的24位整数

来自分类Dev

带有字符串和浮点数的 C++ 文件 IO

来自分类Dev

从 Python 中的 csv 文件读取时将字符串转换为整数/浮点数

来自分类Dev

将浮点数传递到C中的字符串中

来自分类Dev

如何在C#中将字符串拆分为浮点数?

来自分类Dev

无法在C中的单行中将字符串和浮点数作为输入

来自分类Dev

如何在C中成对读取浮点数?

来自分类Dev

如何在C中成对读取浮点数?

来自分类Dev

在C中从文件读取字符串输入

来自分类Dev

目标C:从CSV文件中读取字符串

来自分类Dev

UnimplementedError:不支持将字符串强制转换为浮点数[[节点强制转换(在C:/Users/User/.spyder-py3/LSTM.py:132中定义)]]

来自分类Dev

从文件C ++读取字符串

来自分类Dev

在C ++中读取字符串

来自分类Dev

在C ++中读取字符串

Related 相关文章

  1. 1

    如何在C中将浮点数转换为没有sprintf的字符串?

  2. 2

    如何将浮点数转换为字符串而不会丢失用户在 C 中输入的精度?

  3. 3

    将字符串转换为浮点数在C中?

  4. 4

    在C ++中,如何从1行中获取字符串,浮点数和整数的输入?

  5. 5

    将浮点数转换为字符串的最快C ++方法

  6. 6

    在C ++中将字符串转换为浮点数的问题

  7. 7

    将字符串转换为浮点数在C问题?

  8. 8

    尝试在C中使用sscanf()从文件中读取浮点数,整数和字符串的列表无法正常工作

  9. 9

    C ++将字符串,整数,浮点数从文本文件中读取到数组/字符串中

  10. 10

    在目标C中浮点数到字符串的转换

  11. 11

    如何将 txt 文件转换为浮点数组(在 C# 中)?

  12. 12

    如何在C中的用户输入字符串中将浮点数/整数加倍?

  13. 13

    如何将具有浮点数的文本文件读取到C中的浮点数组

  14. 14

    解析浮点数的C字符串

  15. 15

    c#将字符串十六进制转换为ascii吗?从csv文件读取字符串

  16. 16

    浮点数转换为C中的24位整数

  17. 17

    带有字符串和浮点数的 C++ 文件 IO

  18. 18

    从 Python 中的 csv 文件读取时将字符串转换为整数/浮点数

  19. 19

    将浮点数传递到C中的字符串中

  20. 20

    如何在C#中将字符串拆分为浮点数?

  21. 21

    无法在C中的单行中将字符串和浮点数作为输入

  22. 22

    如何在C中成对读取浮点数?

  23. 23

    如何在C中成对读取浮点数?

  24. 24

    在C中从文件读取字符串输入

  25. 25

    目标C:从CSV文件中读取字符串

  26. 26

    UnimplementedError:不支持将字符串强制转换为浮点数[[节点强制转换(在C:/Users/User/.spyder-py3/LSTM.py:132中定义)]]

  27. 27

    从文件C ++读取字符串

  28. 28

    在C ++中读取字符串

  29. 29

    在C ++中读取字符串

热门标签

归档