I have a file that has several lines of text with each line having a person's name followed by his/her shoe number. The length of the name varies from one to several words in the file. I need to read the name and the number into individual variables.
I've tried to use getline to read a line in the file until it reaches a number. After this the program would read the number into a different variable, jump to the next line and repeat. The problem is the first part. At the moment my code looks like this:
#include <iostream>
#include <string>
#include <cctype>
usingnamespace std;
int main()
{
string str;
string num;
double stringDouble;
int indexOfDigit = -1;
cout << "PLease enter a string and a number ";
getline(cin, str);
// Finds the index of the first digit
for(int i = 0; i < str.length(); i++)
{
if(isdigit(str.at(i)))
{
indexOfDigit = i;
break;
}
}/*end of for loop*/
//saves number as string
num = str.substr(indexOfDigit, str.length() - indexOfDigit);
//removes the number part of the string
str.resize(indexOfDigit);
//converts num to a double
stringDouble = stod(num);
cout << "The string is " << str << endl;
cout << "The double is " << stringDouble << endl;
cin.ignore();
return 0;
}/*end of main*/