Tuesday, May 22, 2012

Issue on a do-while form in Strings

Ok, i'm a student in his first experiences with programmaing so be kind ;) this is the correct code to print "n" times a string on screen...



#include <stdio.h>
#include <string.h>

#define MAX 80+1+1 /* 80+\n+ */

int main(void)
{
char message[MAX];
int i, n;

/* input phase */
printf("Input message: ");
i = 0;
do {
scanf("%c", &message[i]);
} while (message[i++] != '\n');
message[i] = '';

printf("Number of repetitions: ");
scanf("%d", &n);

/* output phase */
for (i=0; i<n; i++) {
printf("%s", message);
}

return 0;
}


why in the do-while form he needs to check if message[i++] != '\n' and not just message[i] != '\n'??





No comments:

Post a Comment