First word dropped off of string.

This is a discussion on First word dropped off of string. within the C++ Programming forums, part of the General Programming Boards category; Hey, my problem is as follows: when I pass a variable into array[a] from c I lose the first word. ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    5

    Question First word dropped off of string.

    Hey, my problem is as follows:

    when I pass a variable into array[a] from c I lose the first word.

    my code:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main() {
    
                    char b;
                    int a; 
                    
                    char c[256];
                    string array[100];
        
                 top:    
                         
                 cout<< "Enter choice, (w)rite / (r)ead / e(x)it: ";
                 cin>> b;
                if ( b == 'w' ) { 
                     goto write;
                     
                     }else if ( b == 'r' ){
                     goto read;
    
                     } else if ( b == 'x' ){
                     goto ntop;
    
                     } else {
                   cout<<"Bad Input!" <<"\n";
                goto top;
                }                             
                     
                     write:
                 cout << "Enter slot: ";
                 cin >> a;
                 cout << "Enter data: ";
                 cin >> c;
                 cin.getline (c, 256);
                 array[a] = c;
                 
                  if (c == array[a]){
                       cout<<"Success! \n";
    
                       } else {
                              cout<<"Failure! \n"; }
                 
                        goto top;
                        
                 read:
                 cout << "Enter slot to read: ";
                 cin >> a;
                 cout << "Data in slot " <<a << ":\n" << array[a] <<"\n";
                        goto top;
                 
                 ntop:
                        cout << "Exiting...goodbye";
                        cin.ignore();
                        cin.get();
                 return 0;
    }
    can anyone help?

    thanks...

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Code:
                 cin >> c;
                 cin.getline (c, 256);
    This code reads a word into c, then reads the rest of the line into c. That might be why the first word isn't in c.

    Note that since you are mixing cin >> and getline in this program, you should call cin.ignore() after each call to cin >> (but not after calls to getline).

    BTW, why are you using both C style and C++ strings? Unless you specifically need to limit the input to 256 characters, you can use the C++ string version of getline directly. Were you aware that it existed?
    Code:
    getline(cin, array[a]);

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    these are the problems I've had with other versions of my code, bear with me:

    If I use a string instead of a char array it ends the write on the first space and tries to pass the rest of the string off as inputs in the next step(s) of the program.

    I've seen it suggested to use cin.getline without the precedeing cin but then it writes a NULL variable into array[a].

    the use of

    Code:
    getline (cin, array[a]);
    automatically writes garbage into array[a].

    I'm interested in why this code won't work as well the solution to the problem.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The solution is to add cin.ignore() after all calls to cin >>. That will ignore the newline character that is left over from when the user types their response and hits enter.

    Then when you call getline, it will pick up the full text instead of stopping immediately when it sees that leftover newline.

    Don't call cin.ignore() after getline because getline ignores the leftover newline automatically.

    Also note that cin.getline (for C-style strings) and getline (for C++ strings) will behave the same in this scenario, so even if you're having trouble getting it to work, you should be using the C++ version.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    I'm still getting errors.

    Will only write first word:
    Code:
    char c[256];
    
    ...
    
                 cin>> c;
                 cin.ignore();
                 getline (cin,array[a]);
    Gives me an infinite loop in program:
    Code:
    string c;
    
    ...
    
                 cin>> c;
                 cin.ignore();
                 getline (cin,array[a]);

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    I still didn't see that you said after every cin>> add cin.ignore, I did that, which paused the program when it reached getline after I removed the preceding cin. however ,if c is a string it writes null, if c is a char array it writes missmashed data.

    I removed

    Code:
    array[a] = c;
    a fairly silly mistake...

    now It nearly works perfectly except now I'm leaving off the first letter...
    Last edited by Arborius; 09-07-2008 at 12:16 PM. Reason: more info to give, didn't need new post

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    ok, If I had spent less time on here, and more time in my code I could have figured it out.
    after fully understanding Daved's explanation I was able to solve my problems:
    fixed code in case anyone else runs into this problem:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main() {
    
                    char b;
                    int a; 
                    
                    string array[100];
        
                 top:    
                         
                 cout<< "Enter choice, (w)rite / (r)ead / e(x)it: ";
                 cin>> b;
                 cin.ignore();
                if ( b == 'w' ) { 
                     goto write;
                     
                     }else if ( b == 'r' ){
                     goto read;
                     } else if ( b == 'x' ){
                     goto ntop;
                     } else {
                   cout<<"Bad Input!" <<"\n";
                goto top;
                }                             
                     
                     write:
                 cout << "Enter slot: ";
                 cin >> a;
                 cin.ignore();
                 cout << "Enter data: ";
                 getline (cin,array[a]);
                        goto top;
                        
                 read:
                 cout << "Enter slot to read: ";
                 cin >> a;
                 cin.ignore();
                 cout << "Data in slot " <<a << ":\n" << array[a] <<"\n";
                        goto top;
                 
                 ntop:
                        cout << "Exiting...goodbye";
                        cin.get();
                 return 0;
    }
    By making silly mistakes and not paying full attention to Daved I have drawn a problem out and wasted more time that I didn't need to.

    the cin.ignore that I didn't remove after I removed cin was dropping off the first letter.

    thanks Daved.

    edit: the string variable c no longer needs to exist, so it was removed.
    Last edited by Arborius; 09-07-2008 at 12:33 PM. Reason: add more info

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    As a side note, get rid of GOTO and use a loop or a funtion call
    I'm just trying to be a better person - My Name Is Earl

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> By making silly mistakes and not paying full attention to Daved I have drawn a problem out and wasted more time that I didn't need to. <<

    Being able to digest and apply the advice given here is not always easy to do and it is actually quite commonly not achieved. So nice work, you seem to have applied my advice perfectly and I'm glad it's working for you.

    I'll mention two additional things. First, I agree with swgh. Now would be a good time to learn how to use alternatives to goto. It is not a good habit to get into and other options are much better.

    Second, your #include should be for <string>, not <cstring>. The <cstring> header includes C style string handling functions like strcpy that you don't need. The <string> header includes C++ string declarations that you are using. Your compiler probably allows it because <iostream> sometimes also includes some of those declarations, but you shouldn't rely on that luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 01:04 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