Read Function Dilemma

I want to say
for(i = 0; txt != ' '; i++)
where txt is a char variable and i is an int variable.
Is this legal or is there a more specific way to refer to a blank space?
Last edited on
Well, when in doubt you should write a small program and see if it compiles.

This time I'll save you the trouble and say that it is legal syntax.
Well, I was trying to include that line of code in a program. But I fear that I'm stuck in an infinite loop somehow. I've isolated the problem to be in my read function.

void read(char txtstr[], int& size)
{
int n, i, x;
char txt, tempstr[MAX];

cin.get(txt);

for(n = 0; txt != '.'; )
{
for(i = 0; txt != ' '; i++)
{
tempstr[i] = txt;
cin.get(txt);
}

for(x = 0; x < i; n++, x++)
txtstr[n] = tempstr[x];

cin.get(txt);
}

size = n;
}
Last edited on
It is difficult to understand what exactly you are trying to accomplish with your read function. Are you trying to get a string, excluding spaces and full-stop, and terminating on the full-stop?
(eg
"Hello world."

becomes
"Helloworld"
)?
Well, my program is supposed read a sentence in which the spacing and/or capitalizations are assumed to be off. For example, " i AM the World." and it is assumed to end with a period. So what I'm trying to do with my read function is to read every group of words as a temporary string including the space (which signifies the end of the word) so that I can then store it in a larger string in which the spacing will be corrected.
Moreover, I just realized that I don't read the next character after executing the loop once. I have modified this, but that still doesn't solve the problem.
Post your modified code please.
void read(char txtstr[], int& size)
{
int n, i, x;
char txt, tempstr[MAX];

cin.get(txt);

for(n = 0; txt != '.'; )
{
if(txt != ' ')
{
for(i = 0; txt != ' '; i++)
{
tempstr[i] = txt;
cin.get(txt);
}

tempstr[i] = txt;

for(x = 0; x < i; n++, x++)
txtstr[n] = tempstr[x];
}
cin.get(txt);
}

size = n;
}
Topic archived. No new replies allowed.