Jump to content

Converting string to float in c++ HELP

Keeping it short:

     i'm trying to convert PART of a 2 dimensional array into a float. i've tried to use stoi (testing with an integer) but it just kicks back an error saying 'Thread 1: signal SIGABRT'

    for(int y = 0; y <= lineNum; y++){
        int price = stoi(purchases[2][y]);
        cout << price << "\n";

any ideas? Google isn't helping...

 

Thanks - H

cc

Link to comment
https://linustechtips.com/topic/739197-converting-string-to-float-in-c-help/
Share on other sites

Link to post
Share on other sites

12 minutes ago, fizzlesticks said:

Sounds like you're going out of bounds of the array. Would need more code to be sure.

//
//  main.cpp
//  Shopping list
//
//  Created by Harry O'Brien on 16/02/2017.
//  Copyright (c) 2017 Harry O'Brien. All rights reserved.
//

#include <iostream>     //required for basic i/o
#include <fstream>      //required to read from files
#include <vector>
#include <string>       //string tings
#include <sstream>      //basically a data waterfallll
#include <stdlib.h>     /* atoi */


using namespace std;

int main(int argc, const char * argv[]) {

vector<vector<string>> purchases;
    
    int row = 12;
    int width = 3;
    
    purchases.resize( row );
    
    for( auto &i : purchases )
        i.resize( width );
    
    //Reading numbers from file
    string line;
    int lineNum;
    
    ifstream myFile ("list.txt");
    if (myFile.is_open()) {
        
        while ( getline (myFile, line) ) {
            
            stringstream stream(line);
            
            string n;
            int j = 0;
            while(1) {
                stream >> n;
                if(!stream) break;
                
                purchases[lineNum][j] = n;
                j++;
                
            }
        
        lineNum++;
        }
        
        cout <<"Done! " << lineNum << " lines read\n\n";
        myFile.close();
    }
    
    else cout << "Unable to open file";
    
//////////////////////////////////////////////////////////////////////////////////////////
//main script
//////////////////////////////////////////////////////////////////////////////////////////
    
//    int decoratingTotal;
//    int tempDiscount;
//    int total;
//    bool discount = false;
    
    for(int y = 0; y <= lineNum; y++){
        double price = stof(purchases[2][y]);
        //int num_dec = std::stoi(str_dec);
        cout << price << "\n";
        //if(purchases[1][y] == decorating) decTotal =
    }
    
    
    
    return 0;
}

/*
 Int decTotal
 Int tempDiscount
 Int total
 Bool discount = false;
 
 For(y = 0, y < purchases.height(), y++) {
	If (purchases [1][y] == decorating) decTotal += purchases[2][y]
	If decTotal >= 20{
 Discount = true
 Break;
 
 For(y = 0, y < purchases.height(), y++) {
	Print << purchases[0][y]
 
	If (purchases[1][y] == gardening && discount == true){
 tempDiscount = (purchases [2][y] * 0.1)
 purchases[2][y] *= 0.9
 
 print << purchases[2][y] <<” \n” << -tempDiscount << “discount\n”
	
	else print << purchases[2][y] << “\n”
 
	total += purchases [2][y]
 print << --------------------------------
 print << “TOTAL: £” << total
*/

i don't think it's an out of bounds error

Link to post
Share on other sites

1. You don't initialize lineNum to 0. Depending on your compiler and optimization settings this may be done for you but don't rely on it, once you turn on optimizations your program will break.

 

2. In your loop reading the file you do 

purchases[lineNum][j] = n;

but in the other loop you do

stof(purchases[2][y]);

where y is a counter based on lineNum, did you mean to do purchases[y][2]?

 

3. In the second loop you do 

for(int y = 0; y <= lineNum; y++)

it should probably be y < lineNum, not <=

1474412270.2748842

Link to post
Share on other sites

You can always make sure you're going out of bounds on the vector(s) by temporarily using std::vector::at in stead of std::vector::operator[] to access elements in the problem code. at throws a std::out_of_range exception when accessing out of bounds.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×