scanf, and gets problems



Help for C/C++ for MVS, OS/390 C/C++, z/OS C/C++ and C/C++ Productivity Tools for OS/390

scanf, and gets problems

Postby silvia » Sat Dec 31, 2011 9:17 pm

My program is:
/****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
char str[80];
FILE *fp;

if((fp = fopen("Taxi Information.txt", "w"))==NULL)
{
printf("Cannot open file.\n"); exit(1);
}
do {
printf("Enter a string (CR to quit):\n");
scanf_s("%s", str); // Type slivia todorof
gets(str);
strcat(str, "\n");
} while(*str!='\n');
}
/****************************************/
If I type: silvia todorof , I have two problems:
1st problem: After typing "silvia todorof", appears "Enter a string (CR to quit)", as required, but if I hit Enter it did not quit and stuck there; but it goes out if I write ^z with two times Enter.
2nd problem: the text file is empty.

Thanks
Silvia
silvia
 
Posts: 1
Joined: Sat Dec 31, 2011 9:14 pm
Has thanked: 0 time
Been thanked: 0 time

Re: scanf, and gets problems

Postby Robert Sample » Sat Dec 31, 2011 9:47 pm

You might want to display in hex the data you're passing to str -- I suspect you'll find that the enter is an actual character and must be recognized by your routine.

The file is empty since you never wrote anything into it.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times


Return to C, C++