-
May 9th, 2009, 05:26 AM
#1
Retrieving numbers from an string.
Basically i have started learning c++. My situation is that i have an array of strings that are read from a text file, which contains lines of text which are correctly formatted.
for eg:
AC/DC,Dirty Deeds Done Dirt Cheap,303,
Alison Moyet,All Cried Out,410,
Allan Browne Quintet,Cyclosporin,291,
Now i put these in an array as i read them from the file. I want to read the numbers in the strings, these numbers are the lengths of the songs and i need to find the total length of all the songs in the file.
My prob is finding a way to get just the numbers from the string array index. Basically just the 303 from the 1st string, 410 from the 2nd string and 291 from the 3rd string. If i can do just one i can just throw it in a loop and find the total length.
I am looking for any suggestions, (i hope this is in the right forum section)
Thanks.
-
May 9th, 2009, 05:41 AM
#2
Re: Retrieving numbers from an string.
Best thing to do is to write out the algorithm in psuedocode first. Then use a textbook, online documentation or google to find the neccesary c++ functions to change the psuedocode into c++.
-
May 9th, 2009, 10:15 AM
#3
Re: Retrieving numbers from an string.
You can use strtok or one of its variants to do this.
Convert every 3rd field to numeric using atoi.
-
May 9th, 2009, 08:54 PM
#4
Re: Retrieving numbers from an string.
Thanx for the info i got it working. Now i have another problem may be unusual.
Code:
for(int k = 0; k <= numOfSongs; k++)
{
found = allText[k].find_first_of("0123456789");
strTime = allText[k].substr(found);
time = atoi(strTime.c_str());
totalTime += time;
}
I have that for loop now where the k condition is (k <= numOfSongs) i get a funny error in the console:
Error:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
BUT when i change the condition to k < numOfSongs (without the equals) it works, but i miss out on the last array.
Any1 can shed any light on this?
Thanks.
-
May 9th, 2009, 09:05 PM
#5
Re: Retrieving numbers from an string.
If a string is declared as string str[100], then its valid indexes are from 0 to 99. So the loop for(int k = 0; k < numOfSongs; k++) is correct without the = sign. You missing the last index could be some other problem.
You the debugger to track it down.
-
May 9th, 2009, 09:32 PM
#6
Re: Retrieving numbers from an string.
Are you sure you're missing the last element in the array?
-
May 9th, 2009, 11:43 PM
#7
Re: Retrieving numbers from an string.
Thanks for your help guys i found the problem, couldn't of done it without u guys!
Thanks again.
-
May 11th, 2009, 06:29 AM
#8
Re: Retrieving numbers from an string.
Here's another alternative technique.
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
// Demo string.
string text("AC/DC,Dirty Deeds Done Dirt Cheap,303,");
// Replace commas with newlines.
replace(text.begin(), text.end(), ',', '\n');
// Initialise a string stream.
istringstream iss(text.c_str());
string band;
string title;
string temp;
int time;
// Read the values.
getline(iss, band);
getline(iss, title);
getline(iss, temp);
time = atoi(temp.c_str());
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
May 11th, 2009, 06:51 AM
#9
Re: Retrieving numbers from an string.
If you want to go totally C++, replace the itoa with
Code:
istringstream(temp.c_str()) >> time;
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|