so i know how to read a standart text file with chars and strings, but i came across an exercise in with a letter line that had numbers in it, so here's the text file sample:
They are not numbers because they have letters in them. Read them as strings.
In fact even so called numbers such as 8739 are strings of ascii characters which the computer program interprets as a number and then converts it to how numbers are actually stored in the computer memory to be processed by the CPU or arithmetic unit. When the computer prints a number it has to convert it back to a string of numeric characters representing that number.
Are you trying to separate the text part from the number part? If you know the string is in [Text][Number] format, one method would be to read the string character by character until you come across the first digit, then you know where the text ends and the number begins, and convert the number substring into an int.
#include <iostream>
#include <sstream> // std::stringstream
#include <string>
#include <cctype> // isdigit()
int main() {
std::string data = "ABC123";
// Find starting index of the number part of the string,
// assuming the "number" part of the data string is always last.
std::size_t number_index;
for (number_index = 0; number_index < data.length(); number_index++)
{
if (isdigit(data[number_index]))
break;
}
// Make a copy of the substring "123"
std::string number_str = data.substr(number_index, data.length() - number_index);
// ("ABC" can be separated by substring(0, number_index))
// Convert the substring "123" into an integer 123
std::stringstream ss(number_str);
int n = -1;
if (ss >> n) // put the stringstreams contents into n
std::cout << "Number = " << n << std::endl;
else
std::cout << "Unable to parse string correctly" << std::endl;
return 0;
}
R33-25KT would be a bit more difficult to parse, but you didn't explain at all what form you need the end-result information to be in.
I just need the info "Riebokslis R33-25KT" to be in one string or in a character array, i will not do anything with them, maybe just print them out or smth.
so basically first two "words" are car parts and the number is how many of them they have. This exercise makes me work with data structures , so somehow i should read first 24 characters (it's always up to 24 until the number) and then the number.