while and For loops with semicolon

Hi All,
There is this function to accept input from the keyboard

1
2
3
4
5
6
7
8
9
char*getstring(char*mesg)
{     char s[82],i *destin;
    cout<<mesg<<endl;
    cin.get(s,80);
    while(cin.get(s[81])&&s[81]!='\n');
    destin=new char(strlen(s)+1);
    for(i=0;destin[i]=toupper(s[i]));i++);
    return destin;
}


I am unable to understand syntax for this simple function
I am clueless about what
1
2
while(cin.get(s[81])&&s[81]!='\n');
for(i=0;destin[i]=toupper(s[i]));i++);


these two statements are doing?
and what actually is the purpose of while loop above?


In the for loop destin[i]=toupper(s[i])) they have added this line instead of a condition.How this for loop terminates?


Please help.

Thanks
Prasad
Get one character from cin and store it in s[81]. Repeat this until get() fails or the character is a newline.

This seems to consume content from the input stream until EOF, endline, or some error is encountered. There is no clear reason to store it into s[81], as any char variable could be used. s[79] (or some earlier element) is '\0', and s[80] is never used. Besides, there are more than one way to flush the buffer.


toupper() takes a char and returns a char. The return value is either the input or uppercase version of it.
The return value is assigned to element in the destin array.

The value that is returned from destin[i]=toupper(s[i]) is the same value as the toupper returns. It is essentially an integer and integers can convert to bool. If the char is '\0', the integer is 0, and the bool is false. Therefore, when the loop reaches c-string terminator, terminator is copied to the destin, and the loop terminates.

1
2
3
4
5
6
7
8
9
10
for(i=0;destin[i]=toupper(s[i]);i++);

// is equivalent to

int i=0;
do {
  destin[i] = toupper( s[i] );
  bool continue = ( '\0' != s[i] );
  ++i;
} while ( continue );
Topic archived. No new replies allowed.