Thread: Help with truncation problem... Should be simple! :-S

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    27

    Help with truncation problem... Should be simple! :-S

    Hi, I'm a complete new C++ newbie, I'm trying to graduate from batch scripting to real programming, and in doing so, also trying to convert some of my programs. This is of course long and laborious as I wade through the tutorials.
    I'm sorry therefore if any of my questions seem obvious but as of yet I have very little idea what I am doing, so let me thank you in advance for your patience and help.

    I'm trying to write some code to help me input single addresses, which will eventually develop into a program which will let me input an address and print it to a local printer. I'm struggling with the input though.

    Here is my code so far, and what is wrong below.
    Thanks
    mikeyp

    Code:
    int main()
    {
    
        cout<<"This program prints single address labels input by the user.\n\n";
        // address enter function
        // str length is 23 as 22+terminating.char "\0"
        char address[7][23];
        cout<<"Enter the address to be printed.\n";
        for ( int x = 0; x < 7; x++ ) {
            //Address input here
            cout<<"Line "<< x + 1 <<": ";
            cin.getline ( address[x], 23, '\n' );
        };
        cout<<"\n\nYour address was: \n\n";
        for ( int x = 0; x < 7; x++ ) {
            //Address output here
            cout<<address[x]<<endl;
        };
        cin.get();
    I *need* the address to be 22 chars wide max. If I enter fewer chars, everything is fine (example 1) If I enter something which needs truncating, everything goes horribly wrong (example 2). It doesn't allow me to enter the latter lines, and rushes through till it finishes.

    Example 1
    Code:
    This program prints single address labels input by the user.
    
    Enter the address to be printed.
    Line 1: Arthur Bond
    Line 2: 34 Tepe Avenue
    Line 3: Great Malvern
    Line 4: Some Postcode
    Line 5:
    Line 6:
    Line 7:
    
    
    Your address was:
    
    Arthur Bond
    34 Tepe Avenue
    Great Malvern
    Some Postcode
    
    
    
    
    
    Process returned 0 (0x0)   execution time : 67.236 s
    Press any key to continue.
    Example 2
    Code:
    This program prints single address labels input by the user.
    
    Enter the address to be printed.
    Line 1: Sir Arthur Reginald Debug
    Line 2: Line 3: Line 4: Line 5: Line 6: Line 7:
    
    Your address was:
    
    Sir Arthur Reginald De
    
    
    
    
    
    
    
    Process returned 0 (0x0)   execution time : 19.938 s
    Press any key to continue.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If your try to give getline more input that it is accepting (23 in this case), cin will go into a fail state and all subsequent attempts to read from it will fail, until you deal with it. (Look up ignore() and clear() methods.)

    You could try much larger char arrays to be ready for longer input, or just use the std::string object:

    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string s;
        std::getline(std::cin, s); //type practically as much as you can
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    Thanks, would this cause problems with the array if I put in more data than the array provides for? I was warned in the tutorial against trying to input more data than was allocated for in the array.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    Sorted it, thanks for your help, prodding me in the right direction helped me find this.
    (Sorry about the messy pasting, it's late, i'm tired and can't be bothered)
    Code:
        string address[7];
        string addressin;
          getline(cin, addressin);
            address[x] = addressin.substr(0, 22);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM