#include <fstream>
#include <iostream>
#include <conio2.h>
#include <stdio.h>
#include <stdlib.h>
usingnamespace std;
int main (int argc, char * argv [])
{
unsignedint quan = 0;
char txtinput[255] = "";
unsignedint * pquan = &quan;
printf("Please, indicate the quantity of messages you want to display:");
fscanf (stdin, "%u", &quan);
while (quan != 0)
{
staticint i = 1;
cout << "Insert the " << i << "ยบ message:";
/*->problem*/gets(txtinput);
puts(txtinput);
quan--;
i++;
}
system ("pause");
}
The problem is that when the loop (while (quan != 0)) starts, the first input (puts(txtinput)) doesn't work. I mean, the program doesn't give me the possibility to write anything, but in the second part of the loop it does. What's the problem there?
That doesn't answer the question though, and the very same caveat will hit you with the standard C++ streams as in C.
The scanf() functions and the C++ stream extraction operators all halt on whitespace. So on line 15 you get an unsigned int, but leave the '\n' in the input.
So the first time you hit line 20 it reads that '\n' and returns an empty string. To fix that, you need to first rid the input of that extraneous newline. In C, you can change line 15 to
immediately after your fscanf (stdin, "%u", &quan);
This will flush out any input/output buffer of stream. In this case, it is stdin; the buffer is still in there waiting to be used and you are attempting to input some more immediately, then it would skip the next immediate input attempt.