int main()
{
char x[20] = "200705241325 C27.5";
BioData Filt;
char year[5];
char month[3];
char day[3];
char time[5];
char temp[6];
char scale;
for (int i = 0; i < 4; i++) {
year[i] = x[i];
}
for (int i = 0; i < 2; i++) {
month[i] = x[i + 4];
}
for (int i = 0; i < 2; i++) {
day[i] = x[i + 6];
}
for (int i = 0; i < 4; i++) {
time[i] = x[i + 8];
}
scale = x[13];
for (int i = 0; i < 5; i++) {
temp[i] = x[i + 14];
}
cout << month << endl;
cout << year << endl;
cout << day << endl;
cout << time << endl;
cout << temp << endl;
cout << scale << endl;
/*
cout << "year: " << Filt.year << " month: " << Filt.month << " day: "
<< Filt.day << " time: " << Filt.time << " temp: " << Filt.temp
<< " scale: " << Filt.scale;
*/
system("PAUSE>NUL");
return 0;
}
and here's the output
05u⌡■ ∙¡█tC«█t2007☻
2007☻
24åü
1325
27.5
C
that last three strings are correct, but the first three are very wonky. if i use a struct and try to print it out, it does something similar. it prints out the correct string, then every string that's declared after it in the struct declaration, but still with random characters in between.
if i initialize all those c-strings with spaces,it doesn't happen, though. i'd like to not have to do that, though, because i feel like this is probably indicative of something i'm doing wrong in the first place
A c-string is an array of characters terminated with a 0. Your strings do not have that 0. You could add a line year[4] = 0; after each for loop.
Though this is a good place for a function. It could be void extract(char* source, char* destination, int first, int last);. I'll leave figuring out its body to you. Good luck.
aha! so, the compiler only adds the '\0' character automatically when i'm assigning the whole string all at once. for instance, in this code
1 2
char name[20]
cin >> name;
the compiler will automatically add it, but any time a treat a c-string like any old array, stepping through it one-by-one, i have to manually add it, right?
we haven't "learned" it yet. i know what the string class is, but not in depth enough to do all the operations i have to do with the data once i have them properly written into these c-strings (convert them to int/double, etc)