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

  • SEARCH
  • Home
  • 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 8551253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:09:25+00:00 2026-06-11T14:09:25+00:00

Below I have a string called line that is coming from a file. The

  • 0

Below I have a string called line that is coming from a file. The string has commas to separate it, where the first part goes to a string vector, and the second to a float vector. Anything after that is not used.

The first part comes out as “text”, which is correct. But the second one shows “248”, while it’s supposed to say “1.234”

I need help converting this correctly. Any help would be greatly appreciated, thanks.

I’m very new to programming. Sorry for any horrible style.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main ()
{
  string line ("test,1.234,4.3245,5.231");
  string comma (",");
  size_t found;
  size_t found2;
  string round1;
  float round2;
  vector<string> vector1;
  vector<float> vector2;

  // Finds locations of first 2 commas
  found = line.find(comma);
  if (found!=string::npos)

  found2 = line.find(comma,found+1);
  if (found2!=string::npos)





    //Puts data before first comma to round1 (string type)
    for (int a = 0; a < found; a++){
    round1 = round1 += line[a];
    }

    //Puts data after first comma and before second comma to round2 (float type)
    for (int b = found+1; b < found2; b++){
    round2 = round2 += line[b];
    }


    //Puts data to vectors
    vector1.push_back(round1);
    vector2.push_back(round2);


cout << vector1[0] << endl << vector2[0] << endl;


  return 0;
}
  • 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-06-11T14:09:26+00:00Added an answer on June 11, 2026 at 2:09 pm

    Your problem is that you’re adding chars to a float value, you can’t convert it in this way. What you were actually doing is adding up the hex values of the characters that made up your number, if you look in an ASCII table you’ll notice 1=49, .=46, 2=50, 3=51, 4=52. If you sum those 5 up you get 248. Also, even though it’s wrong in this case:

    round2 = round2 += line[b];
    

    should be:

    round2 += line[b];
    

    The reason being that the += operator will is equivalent to:

    round2 = round2 + line[b];
    

    so it makes no sense to add an extra round2 = in front of it.

    In order to do this properly use stringstreams like so:

    #include <string>
    #include <sstream>
    
    int main()
    {
        float f;
        std::stringstream floatStream;
        floatStream << "123.4";
        floatStream >> f;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file called new.js.erb that contains this line: if( $('#user_password').val().length < 6
I have below string String str=select * from m_menus; select * from m_roles; I
I have a string as below: $str = /dir1/dir2/dir3/file.txt I want to remove the
I have a string which has the below content String msg = The variables
I have the below String assignment statement String items[] = line.split(\,\,15); String fileNamet =
I have an array of dictionaries loaded from a plist (below) called arrayHistory. <plist
Below is my string called $output it is derived from and fsockopen and fread.
I have a string called word and a function called infinitive such that word.infinitive
i have a string like below $string = ' florida' how can i remove
I have a string of below type. [test1,3,(257,305,435,900)] Now i want to split this

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.