troubleshooting Question

gets problem.

Avatar of hawkm
hawkm asked on
C
5 Comments1 Solution241 ViewsLast Modified:
Hi there,

 
I have a prob, why does my program skip my gets statement when i entered this test data (or simillar test data):

Name of Text File: filename_that_dont_exists.txt
File could not be found!
Enter Text: //this is the problem, the program skips this part.
Enter Width for text: 5

Can anyone help me with this? I tried scanf instead of gets but scanf doesn't give me the result i'm looking for.

What my program is supposed to do is to accept a sentence and then let the user determine how many characters in each line should there be(including blank spaces). For example, if i enter:
text: Today is beautiful, enjoy it to the fullest.  (this part can also be retrieved from existing text file.)
width: 10

output:

Today is b
eautiful,
enjoy it t
o the full
est.

These are my codes. Thanks for your help.

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

main() {

 FILE *fileptr;
 char filename[60];
 char str[100];
 int width = 0;
 int i = 0;
 int j = 0;

    printf("\nName of Text File:");
    scanf("%59s",filename);
    fileptr = fopen(filename,"r");

   
    if(fileptr == NULL){
       printf("\nFile could not be found!");
       printf("\nEnter Text:");
       gets(str); //problem area: gets doesn't take in user input.
    }


    while(fgets(str,100,fileptr) != NULL)
       printf("%s",str);


       printf("\nEnter width for text:");
       scanf("%d",&width);

    while(str[i] != '\0'){
 
       if(j >= width) {
          printf("\n");
          j=0;
       }
 
        printf("%c",str[i]);
        j++;
        i++;

     }

 fclose(fileptr);
 return 0;

}


ASKER CERTIFIED SOLUTION
marcjb

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros