1

My csv file data is look like this:

Palmer Shannon,1.66805E+12,7500,3020
Jonas Kelley,1.62912E+12,9068,1496
Jacob Doyle,1.61608E+12,1112,3502
Iola Hayden,1.60603E+12,6826,4194

This is my header file :

#ifndef client_h
#define client_h
#include <string>

const int MAX_CLIENT = 20;

struct client {
    std::string name;
    long long  accountNum;
    int pwd;
    double balance;
};

For the first data, the String name is refer to Palmer Shannon, long long is refer to 1.66805E+12, int pwd is perfer to 7500 and double balance is refer to 3020.

This is my main.cpp file and i try to create a vector to hold the csv file data.

string str;
    std::vector<client> readClientProfile;
    while (getline(data, str))
        {

        client Client;
        istringstream iss(str);
        string token;
        getline(iss, Client.name, ',');

        getline(iss, token, ',');
        Client.accountNum = std::stoi(token);


        getline(iss, token, ',');
        Client.pwd = std::stoi(token);

        getline(iss, token, ',');
        Client.balance = std::stoi(token);


        readClientProfile.push_back(Client);
    }
    for (size_t i = 0; i < readClientProfile.size() - 1; i++)
    {

            std::cout << readClientProfile[i].name << "  "<<endl;
            std::cout << readClientProfile[i].accountNum << "  "<<endl;
            std::cout << readClientProfile[i].pwd << "  "<<endl;
            std::cout << readClientProfile[i].balance << "  "<<endl;

        }

The question is when i run the program, the accountNum is just show the first world 1. I can not change the type of the accountNum because this is the assignment request.How to read the long long type with the variable,character and symbol?

1 Answers1

2

The account number in your file is stored as 1.66805E+12. This is a floating point number, not an integer. When you use stoi to convert it to an it parses the string and stops at the . since that isn't a valid symbol in an integer. That means stoi is going to return 1 for all of the account numbers. You can fix this by first converting 1.66805E+12 to a double using stod, and then you can store that double as an integer like

getline(iss, token, ',');
Client.accountNum = std::stod(token);
NathanOliver
  • 171,901
  • 28
  • 288
  • 402