473,480 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to Split a string into various types (integer, float, char)?

1 New Member
Hello there,

I'm reading lines from a text file in C++ which contains integer + string + float number(like 3,67 with comma) + string in this order. I need the float number to sort the lines but I couldn't manage to separate the data into the types I can use so far. I tried different kind of functions and the best I could do was such a code;

Expand|Select|Wrap|Line Numbers
  1. void main (){
  2.      ifstream records;
  3.      records.open("records.txt");
  4.  
  5.     int id;
  6.     string line;
  7.     char name[100];
  8.     float gpa;
  9.     string depart;
  10.     if (records.is_open()) {
  11.         while (!records.eof()) {
  12.             getline(records, line);
  13.             istringstream deneme(line);
  14.                     deneme >> id;
  15.                     deneme >> name;
  16.                     deneme >> gpa;
  17.                     deneme >> depart;
  18.  
  19.     //             cout << id << " -- " << name << " -- " <<  gpa1 <<  " -- " << depart << " -- " << endl;
  20.  
  21.  
  22.         }
  23.     }
  24.     records.close();
  25.     system("PAUSE");
  26. }
This fails at reading the floating number which has comma in it and then last string is read as string starting with the comma and rest of the number. An output example is:

698 John 3 ,67

It doesn't read last string on the line as well. I understand that part but simply I need another read but what I want exactly is to separate one line using "tab" as a seperator into proper data types and then using the numbers as integers, and the grades as floating numbers. How Can I do this?
Nov 28 '12 #1
5 5485
Rabbit
12,516 Recognized Expert Moderator MVP
c and c++ uses a dot to separate the whole number from the decimal part. Read it in as a string, replace the comma with a dot, and then convert it to a float.
Nov 28 '12 #2
donbock
2,426 Recognized Expert Top Contributor
Could the string in the middle column ever contain a space?
Nov 28 '12 #3
solidpoint
5 New Member
To Rabbit's point, if the file is small, and you don't need an ongoing solution, just load the file into notepad and replace "," with "."


You might want to try the Unix/Linux utility Awk. MS Access has a good import Wizzard that will make these problems very manageable, and you you'll have a trial & error environment to see if you guessed right. Once in Access you have lots of output options - like .csv
Dec 2 '12 #4
divideby0
131 New Member
Not sure if it will help or not, but if you need to replace the ',' with a '.', then maybe

Expand|Select|Wrap|Line Numbers
  1. long pos;
  2.  
  3. getline(records, line);
  4.  
  5. if((pos = line.find(",")) != string::npos)
  6. {
  7.     line.at(pos) = '.'; // replace the comma
  8.     --pos; 
  9.     while(isspace(line.at(pos))) 
  10.     {
  11.         // remove any spaces if need be
  12.         line.erase(pos, 1);
  13.         --pos;
  14.     }
  15. }
  16.  
  17. isstringstream dename(line);
  18. ...
just check the output before actually trying to use the string stream
Dec 3 '12 #5
Rock R
3 New Member
Can you make a struct containing member variables that map the data types you read when you parse a line.eg (int, string, float).

after you read a line (in getline function in your code) create a new struct of this type and implement conversion of data to member variables in its constructor(argument for constr will be string). Inside constructor you can use string tokenizer to parse string and assign appropriate values in member variables. I dont think this should be difficult. After creating the struct implement a predicate so you can sort the struct as per your need. Hope this helps.
Dec 3 '12 #6

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

Similar topics

5
31153
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
24
3123
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
5
2905
by: sathyashrayan | last post by:
Group, I have some doubts in the following program. ------------------program--------------------- /* ** Make an ascii binary string into an integer. */ #include <string.h> unsigned int...
8
1396
by: El Camino | last post by:
I need to switch a string to an integer if its strLetter = "A" integer value of 1 if its strLetter = "B" then integer value of 2 if its strLetter = "C" then integer value of 3
3
1925
by: Pete | last post by:
Only just started using VB and I've got a problem, not sure how to solve this one. I'm receiving a string from a comm port containing hex values which need to be converted into characters using...
10
2986
by: arnaudk | last post by:
Hello, I am relatively new to C++ and have been milling over a container related problem for a while now. I have a ~2Gb ASCII file of time-ordered data with 4 comma-separated columns. The first...
16
14702
by: skanemupp | last post by:
which is the best way to check if a string is an number or a char? could the 2nd example be very expensive timewise if i have to check a lot of strings? this value = raw_input() try:...
4
10162
by: dmitrey | last post by:
hi all, howto split string with both comma and semicolon delimiters? i.e. (for example) get from string "a,b;c" I have tried s.split(',;') but it don't work Thx, D.
4
7719
by: N9 | last post by:
Hi Anyone who can help about split string. string text = "History about a boy, who loves to play baseball with his friends." I like to find indexOf "play" and read the string 10 char left...
3
2054
by: Daniel DePasqua | last post by:
I do not understand the concept of inputing a number and having the program convert it to an integer, float and string. How would you write a program that does that
0
6904
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...
1
6735
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
5326
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,...
1
4770
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...
0
2992
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...
0
2977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
176
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.