I have this program that I finally finished however I am trying to get it after finishing once to prompt the user "Do you wish to run again? Yes(Y), No(N)". But I also want it to ask for s2 again, and the ch again as well. Keeping s1 to be the same random string if that makes sense.
This is my code:
#include <stdio.h>
#include <stdlib.h>
void s1(char *random);
void s2(char *s2_input, int index);
void strfilter(char *random, char *s2_input, char replacement);
int main()
{
char run = 'Y';
while(run != 'N')
{
int s1_index = 41;
char s1_random[s1_index];
s1(s1_random);
printf("\ns1 = ");
puts(s1_random);
printf("s2 = ");
int s2_index = 21;
char s2_input[s2_index];
s2(s2_input, s2_index);
if(s2_input[1] == '\0')
{
printf("size too small");
exit(0);
}
printf("ch = ");
char replacement = getchar();
printf("\n");
int filter_index = 41;
strfilter(s1_random, s2_input, replacement);
printf("\ns1 filtered = ");
puts(s1_random);
printf("Do you wish to run again? Yes(Y), No(N) ");
scanf("%c", &run);
}
}
void s1(char *random)
{
int limit = 0;
char characters;
while((characters = (('A' (rand() % 26))))) /* random generatro */
{
if(limit == 41)
{
*(random 41 - 1) = '\0';
break;
}
*(random limit) = characters;
limit ;
}
}
void s2(char *s2_input, int index)
{
char array[21] = "123456789012345678901"; /* populated array to make sure no random memory is made */
char input;
int count = 0;
int check = 0;
while((input = getchar() ))
{
if(input == '\n')
{
*(s2_input count) = '\0';
break;
}
else if(input < 65 || input > 90)
{
printf("invalid input");
exit(0);
}
*(s2_input count) = input;
count ;
}
index = count;
}
void strfilter(char *random, char *s2_input, char replacement) /* replacement function */
{
while(*s2_input)
{
char *temp = random;
while(*temp)
{
if(*temp == *s2_input)
*temp = replacement;
temp ;
}
s2_input ;
}
}
At first I tried a do-while loop within the main function. But it doesn't seem to work. It just messes up the output of my program and still doesn't prompt the user. Should I create a new function with the sole purpose of prompting the User? If so how would I? Thanks in advance.
CodePudding user response:
To avoid running afoul of trailing data, call readchar() till you get a newline or EOF
after each prompt:
for(;;)
{
int s1_index = 41;
char s1_random[s1_index];
s1(s1_random);
printf("\ns1 = ");
puts(s1_random);
printf("s2 = ");
int s2_index = 21;
char s2_input[s2_index];
s2(s2_input, s2_index);
if(s2_input[1] == '\0')
{
printf("size too small");
exit(0);
}
printf("ch = ");
int replacement = getchar();
if(replacement == EOF)
break;
while(getchar() != '\n');
printf("\n");
strfilter(s1_random, s2_input, replacement);
printf("\ns1 filtered = ");
puts(s1_random);
printf("Do you wish to run again? Yes(Y), No(N) ");
int run = getchar();
// or include ctype.h and do:
// run == EOF || toupper(run) == 'N'
if(run == EOF || run == 'N' || run == 'n')
break;
while(getchar() != '\n');
}
and example run:
s1 = NWLRBBMQBHCDARZOWKKYHIDDQSCDXRJMOWFRXSJY
s2 = NWLRBBMQBHCDARZOWKKYHIDDQSCDXRJMOWFRXSJY
ch = B
s1 filtered = BBBBBBBB
Do you wish to run again? Yes(Y), No(N) y
s1 = DBEFSARCBYNECDYGGXXPKLORELLNMPAPQFWKHOPK
s2 = NWLRBBMQBHCDARZOWKKYHIDDQSCDXRJMOWFRXSJY
ch = B
s1 filtered = BBBBBBBB
Do you wish to run again? Yes(Y), No(N) N
CodePudding user response:
A very common mistake in C programs taking user input from stdin using getchar
and scanf
is to forget that these functions may leave newlines (or other characters) in the input stream.
In this case the line
scanf("%c", &run);
will leave (at least) a newline in the stream. That newline will be read by the next getchar
and thereby make the program have unexpected behavior.
So your code should remove that newline just after the scanf
. Actually it should remove all characters until it sees a newline.
Further, I would also remove any white space present in the stream before scanning for the user input. That can be done simply by putting a space before %c
Finally, your prompt suggests that you expect the user to input either Y or N but your program continues on any input not being an N
Something like this should fix the above mentioned problems:
char run = 'Y';
while(run != 'N')
{
// ... do your stuff
while(1)
{
// notice the space before %c to remove initial white spaces
if (scanf(" %c", &run) != 1) exit(1); // Input error
// Empty the input stream until a newline is found
while (1)
{
int temp = getchar();
if (temp == '\n') break; // Found end-of-line so break this while-loop
if (temp == EOF) exit(1); // Input error
}
if (run == 'Y' || run == 'N') break; // Valid input so break this while-loop
}
}