Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6788447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:29:13+00:00 2026-05-26T17:29:13+00:00

Basically, I know virtually nothing about C++ and have only programmed briefly in Visual

  • 0

Basically, I know virtually nothing about C++ and have only programmed briefly in Visual Basic.

I want a bunch of numbers from a csv file to be stored as a float array. Here is some code:

string stropenprice[702];   
float openprice[702];
int x=0;
ifstream myfile ("open.csv");
if (myfile.is_open())
{
  while ( myfile.good() )
  {
    x=x+1;
    getline (myfile,stropenprice[x]);
    openprice[x] = atof(stropenprice[x]);
    ...
  }
  ...
}

Anyways it says:

error C2664: ‘atof’ : cannot convert parameter 1 from ‘std::string’ to ‘const char *’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:29:14+00:00Added an answer on May 26, 2026 at 5:29 pm

    Well, you’d have to say atof(stropenprice[x].c_str()), because atof() only operates on C-style strings, not std::string objects, but that’s not enough. You still have to tokenize the line into comma-separated pieces. find() and substr() may be a good start (e.g. see here), though perhaps a more general tokenization function would be more elegant.

    Here’s a tokenizer function that I stole from somewhere so long ago I can’t remember, so apologies for the plagiarism:

    std::vector<std::string> tokenize(const std::string & str, const std::string & delimiters)
    {
      std::vector<std::string> tokens;
    
      // Skip delimiters at beginning.
      std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
      // Find first "non-delimiter".
      std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
    
      while (std::string::npos != pos || std::string::npos != lastPos)
      {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
      }
    
      return tokens;
    }
    

    Usage: std::vector<std::string> v = tokenize(line, ","); Now use std::atof() (or std::strtod()) on each string in the vector.


    Here’s a suggestion, just to give you some idea how one typically writes such code in C++:

    #include <string>
    #include <fstream>
    #include <vector>
    #include <cstdlib>
    
    // ...
    
    std::vector<double> v;
    
    std::ifstream infile("thefile.txt");
    std::string line;
    
    while (std::getline(infile, line))
    {
      v.push_back(std::strtod(line.c_str(), NULL));  // or std::atof(line.c_str())
    }
    
    // we ended up reading v.size() lines
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I want to know how to set center alignment for a cell using
Basically, I want to know what is best to avoid future problems and confusions
Basically, I just want to know if its possible to use Nhibernate to migrate
Okay I know I asked about this before, and the answer was basically cache
Virtually every language I know of is basically a variation on a theme: you
Basically I want an arbitrarily large stack. I know that's not possible, but could
I basically need to know how to import SQL code into Access. I've tried
For instance, I know that basically all languages that are object oriented based are
Does anybody know a way with JavaScript or CSS to basically grey out a
As I know alpha composition uses straight alpha basically. But all GLES blending sample

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.