Results 1 to 1 of 1

Thread: Having problems capturing and sending all input, problem may be cin.ignore()?

Threaded View

  1. #1

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Having problems capturing and sending all input, problem may be cin.ignore()?

    Hello everyone!
    i'm back, same project, different problem

    Alright, When the user enters the input in the console, i used cin.getline(discription,101); This is just 1 example of what i used, and when i check out the output file, the first letter of the input is missing! Here is the code, and i also am attaching the output file... so u can see for yourself! here is the code for the addRecord();

    PHP Code:
    void Animal::addRecord()
    {
        
    ofstream outfile("inventory.txt",ios::ate);
        if(!
    outfile)
        {
            
    cout<<"Unable to write to file\n";
        }
        else
        {
            
            
    cout <<"Type: ";
            
    cin.ignore();
            
    cin.getline(type,31);
            
            
            
    cout <<"health: ";
            
    cin.ignore();
            
    cin.getline(health,101);
            

            
    cout <<"Age: ";
            
    cin >> age;

            
    cout <<"Sex: ";
            
    cin >> sex;
            
            
            
    cout <<"Discription: ";
            
    cin.ignore();
            
    cin.getline(discript,101); 
            

            
            
    cout <<"Location: ";
            
    cin.ignore();
            
    cin.getline(location,51);
            
            
            
    cout <<"Other: ";
            
    cin.ignore();
            
    cin.getline(other,101); 
            
        
            
    //output to file
            
    outfile <<"Type: " << type 
                    
    <<" Health: " << health 
                    
    <<" Age: " << age
                    
    << " Sex: " << sex 
                    
    <<" Discription: " << discript 
                    
    <<" Location: " << location
                    
    <<" Other: " << other << "\n\n";

        }
        
    outfile.close();


    Here is the class data members
    Code:
    private:
    	int age;
    	char type[30];
    	char health[100];
    	char sex;
    	char discript[100];			
    	char location[50];			
    	char other[100];
    I had to add taht cin.ignore, so it would prompt the user for each feild, such as: Type: German Shedpard [enter] Health: Good [enter] etc... , if you take them out, it will skip and do,
    Type: Health: Good [enter], etc....
    I have a feeling its skippping the first character of some of the lines, is becuase i put cin.ignore(); but if i take it out, it will cause even more damange, anyone have any suggestions on what i did wrong here is the output file...
    As you can see, it cut off, in Location: it should say, Brockway or Dubois, PA, but insteed it says rockway,PA and ubois,PA


    Thanks for listening
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width