Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Is it a cin.getline problem???

menelaosmenelaos Member Posts: 45
I have written this code which compiles fine without giving me any errors. When i run it, it acts as if it already has an entry for the variable that the user is supposed to fill.
However, when i compile and run the subfuctions on their own, meaning as beeing standalone programs, everything runs as it should!
Can somebody take i look at it and tell me what might be wrong?
I attach the coding below.
The two separate .txt files used by the program are not essential to verify its correct operation.

#include
#include
#include
#include
#include
#include
#include

char name[30]={0};
int search_number(void)
{
int found = 0;
int i = 0;
char numbers[10][20];
char names[10][30];
// char name[30]= {0};

FILE *stream,*stream1;

cout<<"Please enter a name>> ";
cin.getline(name,30);
// clrscr();
strlwr(name);

if((stream = fopen("names.txt", "r")) == NULL)
{
cout <<"cannot open file";
return 1;
}

if((stream1 = fopen("numbers.txt", "r")) == NULL)
{
cout <<"cannot open file";
return 1;
}
fseek(stream, 0, SEEK_SET); //Set to begining of file
fseek(stream1,0, SEEK_SET);

for(i=0;i<5;i++)
{
fgets(names[i],30, stream);
fgets(numbers[i],20,stream1);

if(strstr(names[i], name)!=0)
{
found++;
cout<<"Name: "<<names[i];
cout<<"Number: "<<numbers[i];
}
}

if(found==0)
cout<<"The name you requested was not found in the database...
";

fclose(stream);
fclose(stream1);

return 0;
}

void store_number(void)
{
char name[30]={0};
char number[20]={0};

ofstream outfile; //Create object used to output data

cout<<"Please enter a name>> ";
cin.getline(name,30);
strlwr(name);

outfile.open("names.txt" , ios::app); //Open file for output.
if(!outfile) //Test file
{
cout<<"
Failure to open file!
";
exit(1);
}

outfile<<"
"<<name;
outfile.close();

cout<<"Please enter a number>> ";
cin.getline(number,20);

outfile.open("numbers.txt" , ios::app); //Open file for output.
if(!outfile)
{
cout<<"
Failure to open file!
";
exit(1);
}
outfile<<"
"<<number;
outfile.close();
}


int main(void)
{
int choice;


cout<<"Please make a choice from the
";
cout<<"following menu and press Enter>> ";

for(;;)
{
cout<<"

1.Search for a phone number...";
cout<<"
2.Store a new phone number...";
cout<<"
3.Edit an existing phone number...";
cout<<"
4.Exit the program...";
cout<<"
>> ";
cin>>choice;

switch(choice)
{
case 1:
search_number();
break;

case 2:
store_number();
break;

case 3:
//CODE THAT WILL ALLOW EDITING OF EXISTING NAMES TO BE ENTERED
break;

case 4:
cout<<"
Thank you for using this PROKSoft product.";
cout<<"
Check our website regularly for updates and new releases.";
cout<<"
*********http://www.proksoft.com**********";
exit(1);
break;

default:
cout<<"
Invalid Entry. Please try again.";
}
}
}

Comments

  • tmlutmlu Member Posts: 15
    : I have written this code which compiles fine without giving me any errors. When i run it, it acts as if it already has an entry for the variable that the user is supposed to fill.
    : However, when i compile and run the subfuctions on their own, meaning as beeing standalone programs, everything runs as it should!
    : Can somebody take i look at it and tell me what might be wrong?
    : I attach the coding below.
    : The two separate .txt files used by the program are not essential to verify its correct operation.
    : int main(void)
    : {
    : int choice;
    :
    : cout<<"Please make a choice from the
    ";
    : cout<<"following menu and press Enter>> ";
    :
    : for(;;)
    : {
    : cout<<"

    1.Search for a phone number...";
    : cout<<"
    2.Store a new phone number...";
    : cout<<"
    3.Edit an existing phone number...";
    : cout<<"
    4.Exit the program...";
    : cout<<"
    >> ";
    : cin>>choice;

    Here, cin reads an interger but leaves a '
    ' in the buffer.
    When cin.getline is invoked, it stops reading when it encounters a '
    ' key, which is the first keystroke in the buffer.
    You may use fflush before calling cin.getline.
    -Lu.
  • menelaosmenelaos Member Posts: 45
    Thanx.
    Must be that... However, i'm not sure about the syntax of the fflush function for this case. Can you help me out?
  • tmlutmlu Member Posts: 15
    : Thanx.
    : Must be that... However, i'm not sure about the syntax of the fflush function for this case. Can you help me out?
    :

    From MSDN,
    Require Header : stdio.h
    function prototype : int fflush( FILE *stream );
    fflush returns 0 if the buffer was successfully flushed. The value 0 is also returned in cases in which the specified stream has no buffer or is open for reading only. A return value of EOF indicates an error.

    try fflush(stdin);

    -Lu.




  • wcwrogerwcwroger Member Posts: 41
    This might be helpful.
    [code]
    cin.ignore(INT_MAX, '
    '); [green]//get rid of the return carriage [/green]
    [/code]

    Roger


  • menelaosmenelaos Member Posts: 45
    I tried both your suggestions, but the problem persists.
    It compiles properly, so there cannot be a syntax error and i believe i have called the function properly (for the fflush idea).
    Can't understand what's wrong there...
  • tmlutmlu Member Posts: 15
    : I tried both your suggestions, but the problem persists.
    : It compiles properly, so there cannot be a syntax error and i believe i have called the function properly (for the fflush idea).
    : Can't understand what's wrong there...
    :
    Well, I traced into it and found that fflush does not flush the stream. cin.peek still returns 10h, which means there's an '
    ' character in the buffer, after calling fflush. I don't know why. However, the following works: use an extra char array and call cin.getline twice. Store the '
    ' character first.
    -Lu.
  • menelaosmenelaos Member Posts: 45

    : Well, I traced into it and found that fflush does not flush the stream. cin.peek still returns 10h, which means there's an '
    ' character in the buffer, after calling fflush. I don't know why. However, the following works: use an extra char array and call cin.getline twice. Store the '
    ' character first.
    : -Lu.
    :

    Of course! It's like using a temp array to get rid of any buffers before calling the actual array! Bit of a backdoor solution but works fine and it'll do for me!
    Thnx a lot!
Sign In or Register to comment.