cin.getline();

This is a discussion on cin.getline(); within the C++ Programming forums, part of the General Programming Boards category; Need help please, why is it skipping cin.getline(); Code: #include <cstdlib> #include <iostream> #include <fstream> using namespace std; struct data ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    142

    cin.getline();

    Need help please, why is it skipping cin.getline();

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    struct data {
           
           int item;
           char Des[256];
           int qty;
           
           };
    
    int main(int a, int b, char c[256])
    {
    
    
    char string[10];
    data items;
    
    
    cout<<"Please enter the item number > ";
    cin>> a;
    cout<<"Please enter the item's decription > ";
    cin.getline(string,10);
    cout<<"\nPlease enter the quantity on site > ";
    cin>> b; 
    
    
    
    0fstream  filea;
    filea.open("test.txt",ios::app);
    
    
    
        
        
    }
    WhAtHA hell Is GoInG ON

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I'd recommend you to use std::string over character arrays, but to solve your problem, you would need to insert a cin.ignore(1000, '\n); statement before the cin.getline() call.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by Desolation
    I'd recommend you to use std::string over character arrays, but to solve your problem, you would need to insert a cin.ignore(1000, '\n); statement before the cin.getline() call.
    Cheers, that worked but i'm still getting used to working with strings so i will just stick with what i am doing at the mo
    WhAtHA hell Is GoInG ON

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The reason the ignore helps is that when th euser enters the item number, they time a number and hit enter. The number is read in but the enter is left around. Your next call to getline reads from cin and stops at the first newline (enter), so it stops write away. Adding the ignore causes all characters to be ignored up to and including the next newline, so the enter is ignored and getline can wait for the user to type the description.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    struct what{
           
           char jk[256];
           
           };
    
    int main()
    {
        
        char b[256];
        
        
        cout<<"decrip > ";
        cin.getline(b,256);
        
        what item;
        
        cout<< b;
        
        item.jk[256]=b[256];
        
        
        cout<< item.jk[256]; 
       
       
       cin.get();
       
       
       
        
        
        
        
        
        return 0;
        
    }

    Can anyone tell me what the hell i've done, it just outputs some crap from memory ( I think ), the answer is probably obvious but i can't see it. (I want to pass the variable in b to item.jk then display it through cout, but it comes out as jibberish?)
    Last edited by wart101; 11-30-2006 at 11:44 PM.
    WhAtHA hell Is GoInG ON

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > item.jk[256]=b[256];

    > cout<< item.jk[256];
    Code:
    #include <cstring>
    .
    .
        strcpy(item.jk, b);    
        
        cout<< item.jk;
    Another reason strings are recommended. To copy a string, it would just be:
    Code:
        item.jk = b;

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    You also went past the range of the array. For an array declared like this : array[256] you have indices ranging from 0 - 255, not 1 - 256.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline help
    By Cool Dude 2k in forum C Programming
    Replies: 2
    Last Post: 07-27-2005, 07:55 PM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  3. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 06:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21