ive included stdio.h
and this bit of code doesnt want to work,
gets(sTicket.sStreetName[iArrayCount])
im trying to get it to store a string.
any ideas?
ive included stdio.h
and this bit of code doesnt want to work,
gets(sTicket.sStreetName[iArrayCount])
im trying to get it to store a string.
any ideas?
Well, for starters gets only takes in a series of characters up to a "\n" and stores them in a string, I could be wrong, but it kinda looks to me like you're confusing gets with fgets.
char string[256];
gets (string)
fgets takes a specified number of characters from a stream and stores them in a string.
something like
fgets( string, sizeof(string), stdin );
I think
Last edited by Azuth; 05-16-2002 at 06:45 AM.
so if i want to store a string that is liable to have a space, and wont quit until someone presses enter, what shall i use, im not quite sure what you are trying to explain to me. i want to store something in the string, sTicket.sStreetName[iArrayCount], no more than about 50 characters really.
>gets(sTicket.sStreetName[iArrayCount])
We would need to see how sTicket is declared, but this should probably be:
gets(sTicket.sStreetName)
or if sTicket is an array:
gets(sTicket[iArrayCount].sStreetName)
However I think I have heard that gets() is dangerous to use, because it has no error checking, so it's best to use fgets():
Assuming a declaration for sStreetName of:
char sStreetName[50];
then either:
fgets(sTicket.sStreetName, 50, stdin);
or:
fgets(sTicket[iArrayCount].sStreetName, 50, stdin);
Or using c++ style streams, getline().
cin.getline(sTicket[iArrayCount].sStreetName, 50);
This code could pose as a problem because when you use getline, it automatically accepts the fact that the variable being used is either a character pointer or a character array, and by the looks of what the code is supposed to do it is not a multi-dimensional array, therefore I would use the following:cin.getline(sTicket[iArrayCount].sStreetName, 50);
cin.getline(sTicket.sStreetName, 50, '\n');
The 3rd argument is a default argument which doesn't have to be put in, it is by default '\n'.
tudehopet uses Borland Compiler 5.5
and Visual C++ 6.