473,513 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

wanna ask about string and converting it into float

2 New Member
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 1591
weaknessforcats
9,208 Recognized Expert Moderator Expert
Are you using C or C++?
Apr 28 '15 #2
Maria123
2 New Member
i'm using c++ ..........
Apr 28 '15 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
RabahMabhoul
6 New Member
//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
1438
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
16294
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
14074
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
2461
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
487
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
1360
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
1783
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
2425
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
1953
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
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.