473,400 Members | 2,163 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,400 software developers and data experts.

Hoping for help with separating a single string into a char and float

I searched the faq and came up empty...so I apologize if I overlooked this one...

I am needing to separate a string into a char and float respectively...

I can get the char decently easy using the aString.at(i) but am unsure if using atof() is the best method for converting the remaining string to a float (or rather - I am unsure if I am missing something incredibly stupid that I should have noticed)...

For example:
stringName = "A 12345" (user input)
then:
charName = "A"
floatNum = "12345"

I also have to test whether the values are valid...I was planning on testing the charName data based on the ordinal values and making sure they are within a specific range of ordinal values...Is the best way to test the numbers to leave it as a string, test each individual character and then if it passes as valid, run it through atof()?

Thank you!
Oct 16 '06 #1
3 1511
Banfa
9,065 Expert Mod 8TB
rather than atof use strtof, this returns a pointer to the place where the conversion stopped, if that character is not the end of the string or there is not only white space between that character and the end of the string then the string is invalid. You also get the result at the same time which is handy.
Oct 16 '06 #2
Awesome, thank you for the suggestion. This stuff is way over my head at this point, but I think just jumping in head first is probably the best way to learn.
Oct 17 '06 #3
vermarajeev
180 100+
Why not try using istringstream defined in sstream like this

Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.     string str;
  4.     string name;
  5.     float flt;
  6.     cout<<"Enter the name"<<endl; 
  7.     getline(cin, str, '\n');  //I entered A 1234
  8.  
  9.     istringstream is(str);
  10.     is >> name;
  11.     is >> flt;  
  12.  
  13.     cout<<name<<endl;
  14.     cout<<flt<<endl;
  15.     return 0;
  16. }
Oct 17 '06 #4

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

Similar topics

8
by: drose0927 | last post by:
Please help! I can't get my program to exit if the user hits the Escape button: When I tried exit(EXIT_SUCCESS), it wouldn't compile and gave me this error: Parse Error, expecting `'}''...
18
by: James Radke | last post by:
Hello, We are currently using a user DLL that when working in VB 6.0 has a user defined type as a parameter. Now we are trying to use the same DLL from a vb.net application and are having...
2
by: James Radke | last post by:
Hello, I have a vb.net windows application that is calling an older DLL provided by a third party. They supplied a VB 6 application that, when run on my systemn successfully passes data to the...
8
by: SK | last post by:
Hi I am trying to write a simple C program with devc++ as the complier using the concept of arrays. I cannot get the program to compile without mutiple errors. If anyone has the time to help me...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
1
by: yuriy_zubarev | last post by:
Greetings, I apologize for re-posting this, but I'm running out of options on my side. I've got a DLL that's not COM compatible and I'm stuck using interop for the first time. I got couple of...
14
by: subaruwrx88011 | last post by:
I am using a map that takes an string as the key and a structure that is being stored. struct StateType { int i; float f; char* s; }
1
by: vv1 | last post by:
Write a C program for reading in a message string (with no blanks) and decoding the message. Store the decoded message in another string called outString. After decoding is complete, print...
19
by: mohammaditraders | last post by:
a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions Cal_percentage() which calculate the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.