473,324 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

wanna ask about string and converting it into float

i need to write a code that accepts string form user like this {1 2 3 ; 1 2 3}
and then make a function that converts this string into float to put it in an array as a matrix and make operations on it ... could you help me please what to do ?
Apr 28 '15 #1
4 1587
weaknessforcats
9,208 Expert Mod 8TB
Are you using C or C++?
Apr 28 '15 #2
i'm using c++ ..........
Apr 28 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
Then you should be using a stringstream for this:

Expand|Select|Wrap|Line Numbers
  1. string input("{123;456}");
  2.     stringstream str;
  3.     char junk;
  4.     float a;
  5.     float b;
  6.  
  7.     str << input;
  8.  
  9.     str >> junk >> a >> junk >> b >> junk;
Here you capture your input in a C++ string. Then you insert the string into a stringstream object. These stringstream objects contain a string but operate like a stream (like cin/cout).

On the input is inserted you extract it using a variable (junk) to hold the {. Then extract the first float. Then use junk again to hold the ;. Then extract the second float. Then use junk again to hold the }.
Apr 28 '15 #4
//Input request ! you will get a string in the variable
getline(cin,variable);

//with stod() you can turn a string to a float or double.
float variable_that_will_hold_the_numerical_value = stod(variable);
//I hope it helps you!
May 18 '15 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mark Rae | last post by:
Hi, Say you are working with a CSV file generated by a legacy mainframe which stores dates in the format 'ddMMMyy' e.g. ,16JUN05, Extracting the individual date parts is easy enough, but is...
15
by: Daren | last post by:
Hi, I need to be able to split large string variables into an array of lines, each line can be no longer than 70 chars. The string variables are text, so I would additionally like the lines...
9
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6)....
5
by: eric.goforth | last post by:
Hello, float.parse(MyString) seems to work to convert a string to a float, but how do I change it back? e.g. (string) (Myfloat1 - Myfloat2)
10
by: steve.lorimer | last post by:
I'm looking for a pre-processor command that will allow me to resolve const strings into const char literals at compile time. Looking at the code below, I can take 5 characters and create a...
3
by: mtaylor314 | last post by:
I am needing some help converting a string to a float value. The string could be in any of the following formats: x x.y x/y. Obviously, I am having issues w/ the fraction string. The other...
1
by: =?Utf-8?B?VmlkZHM=?= | last post by:
Hi All, Could anyone help me in resolving this issue? I have an Iframe on my aspx page and I want to insert a String value into that through the code behind.I dont want to use the src attribute...
3
by: Alenik1989 | last post by:
how can i chang a datatype int into float in the middle of a function that uses arrays? my func is int avrg(int x,int y) { int counter; float average,sum=0;...
1
by: sahasrani | last post by:
Hii all, I want to take string input into a char array.Can someone help me with the functionality for the above problem.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.