Like Tree1Likes
  • 1 Post By Elkvis

Weird cin.getline() glitch

This is a discussion on Weird cin.getline() glitch within the C++ Programming forums, part of the General Programming Boards category; Code: #include <cstdlib> #include <iostream> #include <fstream> using namespace std; int get_int(int default_value); int main(int argc, char *argv[]) { char ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    Weird cin.getline() glitch

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int get_int(int default_value);
    int main(int argc, char *argv[])
    {
        char filename[MAX_PATH + 1];
        int n = 0;
        char model[20];
        char make[20];
        char year[5];
        int mileage;
        int recsize = sizeof(model) + sizeof(make) + sizeof(year) + sizeof(int);
        
        cout << "Enter file name: ";
        cin.getline(filename, MAX_PATH);
        
        fstream fbin(filename, ios::binary | ios::out);
        if (!fbin)
        {
           cout << "Could not open" << filename << endl;
           system("PAUSE");
           return -1;
        }
        
        cout << "Enter file record number: ";
        n = get_int(0);
        
        cout << "Enter model: ";
        cin.getline(model, sizeof(model) -1);
        cout << "Enter make: ";
        cin.getline(make, sizeof(make) -1);
        cout << "Enter year: ";
        cin.getline(year, sizeof(year) -1);
        cout << "Enter mileage: ";
        mileage = get_int(0);
        cout << "your current mileage is " << mileage;
        
        fbin.seekp(n * recsize);
        fbin.write(model, sizeof(model) -1);
        fbin.write(make, sizeof(make) -1);
        fbin.write(year, sizeof(year) -1);
        fbin.write((char*)(&mileage), sizeof(int));
        fbin.close();
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    #define COL_WIDTH 80
    
    int get_int(int default_value)
    {
        char s[COL_WIDTH + 1];
        
        cin.getline(s, COL_WIDTH);
        if(strlen(s) == 0)
                     return default_value;
        return atoi(s);
    }
    (please note this is Exercise 8.3.1 from the book Without Fear by Brian Overland)
    Essentially this is just supposed to create a record by asking the User to input the file to write into or create and write into, the record number to write on, the model, make, year, and mileage.
    After that it will write the data into the new file in binary - then exit.

    Problem I get is when I compile and run the program it works fine until it gets to the mileage part. It prompts the user to enter the mileage but immediately skips the cin.getline() located in the get_int(). The results for mileage always come out as the default_value. Not sure what is happening here.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    It's hard to tell you what's wrong without a real example of the input to the program.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    fbin.seekp(n * recsize);
    fbin.write(model, sizeof(model) -1);
    fbin.write(make, sizeof(make) -1);
    fbin.write(year, sizeof(year) -1);
    fbin.write((char*)(&mileage), sizeof(int));
    Well if you're trying to seek in multiples of recsize, why are you subtracting 1 from these fields?

    > char year[5];
    So all you can type in (without overflowing this) is a 3-digit year.
    The other two characters will be the \n and \0.

    If you type in a 4-digit year, the \n is NOT stored, but left for the next getline call to deal with.

    Ideally, you read into a large buffer, then you validate and copy from the large input buffer to the smaller data buffer when you're sure the content is acceptable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,370
    unless this is a school assignment, and you haven't covered std::string yet, I would strongly recommend using std::string and std::getline instead of char arrays and cin.getline(). also, in places where you read an int from the console, why not just use

    Code:
    cin >> int_value;
    cin.ignore(<some_large_number>, '\n');
    a proper C++ book should not be teaching C-sytle string handling. I'd suggest a different book. perhaps one from the book recommendations thread in this forum.
    Last edited by Elkvis; 11-01-2011 at 06:38 AM.
    Elysia likes this.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    Quote Originally Posted by Salem View Post
    The other two characters will be the \n and \0.
    This is exactly what I didn't notice. This is the answer to my problem as well.

    This lesson is from one of the suggested books, C++ Without Fear, Overland.
    Last edited by Arieken; 11-01-2011 at 07:56 AM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,370
    well it's unfortunate that it's teaching you to use char arrays, although I'll admit that for random file access with fixed-length records, there really aren't a lot of other ways to do it.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Don't forget that there is std::vector that can work as a buffer, and std::array that can work as a fixed-size array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird google glitch?
    By stevesmithx in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-01-2009, 12:11 AM
  2. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 09:40 AM
  3. getline function is being weird
    By bradleym83 in forum C++ Programming
    Replies: 3
    Last Post: 07-27-2005, 10:43 AM
  4. SmartFTP glitch?
    By alphaoide in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 02-27-2004, 07:37 PM
  5. Okay its running with a serious glitch
    By Blizzarddog in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2002, 01:33 PM

Tags for this Thread


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