I have a question in regards to scanning in a character array with spaces for a project im working on for an introductory class in C programming.
I know that I cant use the scanf function becasue it will stop scanning in the characters once it sees a space. Our teacher told us to use the "gets" function.
The string can have a maxium length of 50 characters. So I defined the string variable as
char str[51];
to leave an extra space for the the \0
I can get the gets function to work in an easy case
Code:char str[51]; printf("Enter the string:"); gets(str);
it works fine for this. This is probally a dumb question, but will the gets function be skipped, if it is inside a loop. My program just seems to skip right over the gets function, but if I replace it with a scanf function everything works fine(except for the fact that I cant scan in more than one word).
Here is the basic structure of the part of the program that I am having trouble with.
Code:#include<stdio.h> #define SIZE 50 int selection(void); //prototype of function that chooses encryption method int main() { int choice=0; char str[SIZE]; while(choice!=5) { choice=selection(); printf("Enter the string now: "); gets(str); } return(0); } int selection(void) { int choice; int lcv=1; printf("Chose method 1-5 by entering integer, ect.") while(lcv==1) { scanf("%d",&choice); if(//input validation) { correct while loop while quit } else { while loop will continue } } return(choice); }
In this case, the program doesnt stop and give the user a chance to input a string like it did in the first example.
any help would be appreciated
thanks,
Zack