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.
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.
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++.
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.
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.
«_Superman_» I love work. It gives me something to do between weekends.
#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
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
Bookmarks