简体   繁体   中英

how to convert a string from a getline got from .csv file to a int to use it

Hello iam a student and iam trying to use a read from a csv file but I am trying to use the int that I will get in another thing I want to use the actual price that came from the.csv file to use it as an int so I want to convert it from string to int so I can use it i don't here is my c++ version gcc version: 4.7.0

#include <iostream>
#include <stdio.h>
#include <fstream>
#include "Article.cpp"
#include <iomanip>
#include<string>
using namespace std;

class Stock: public Article {
private:
    string articlenumber;
    string actualstock;
    string maximumstock;
    string consumption;
public:

    void read() {
        fstream stock;
        stock.open("input.csv");

        if (!stock.is_open())
            std::cout << "ERROR: File Open" << '\n';

        cout << "no   " << "description  " << "actual stock  "
                << "maximum stock  " << "cost price  " << "consumption  "
                << "order duration  " << endl;
        while (stock.good()) {

            getline(stock, articlenumber, ',');
            getline(stock, description, ',');
            getline(stock, actualstock, ',');
            getline(stock, maximumstock, ',');
            getline(stock, costprice, ',');
            getline(stock, consumption, ',');
            getline(stock, orderduration);
            cout << setw(5) << left << articlenumber << setw(13) << left
                    << description << setw(14) << left << actualstock
                    << setw(15) << left << maximumstock << setw(12) << left
                    << costprice << setw(14) << left << consumption
                    << orderduration << endl;
        }

        stock.close();
    }

    void totalstock() {
        fstream stock;
        stock.open("input.csv");
        int totalstock = 0;
        int Actualstock = 0;
        while (stock.good()) {
            getline(stock, actualstock, ',');
            // I want to do this int Actualstock += actualstock;
            totalstock += Actualstock;

        }
        cout << totalstock;
    }
    void write() {

    }

};
int main() {
    Stock a;
    a.totalstock();

}

Easy solution, just think the ascii table if you subtract 48 you will obtain the number as an int . I have attached a code example to make it easer for you.

There are libraries that do this operation automatically, but internally they are all doing something like this.

#include <iostream>
#include <cstdlib>
#include <string.h>
#include <cmath>
using namespace std;

int main(){
string tal;

tal = "1234";

int coun,finalNum,siz,powa;
siz = tal.length()-1;//get the size but one less in this case it will give you 3
powa=pow(10,siz); // get that number to the power of 10

coun=0;//setting the counter to zero
finalNum=0;
while (siz>=0){//do the loop until the end of the length
      finalNum  = finalNum +(tal[coun]-48)*powa; // after that you can just reassemble 
      powa=powa/10;
      coun++;
      siz--;

}

cout<<finalNum;

}

You can you convert your string to integer like by using stoi like this

    std::string age = "10";
    std::cout << std::stoi(age) << std::endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM