Terminating input upon encountering a blank line

This is a discussion on Terminating input upon encountering a blank line within the C++ Programming forums, part of the General Programming Boards category; edit: I figured out Code: int getinfo(student pa[], int n) { int i; for(i = 0; i < n; i++) ...

  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Question Terminating input upon encountering a blank line

    edit: I figured out
    Code:
      int getinfo(student pa[], int n) 
    { 
      int i;
      for(i = 0; i < n; i++) 
      {   
        cout << "Enter student's full name: "; 
        cin.get(pa[i].fullname, SLEN).get();
          if(strlen(pa[i].fullname) == 0)
            break;
        cout << "Enter student's hobby: ";
        cin.get(pa[i].hobby, SLEN).get();
        cout << "Enter student's OOP level: "; 
        (cin >> pa[i].ooplevel).get();
      }   
      return i;      
    }

    I need to break the for inside getinfo() if the user inputs a blank line for his full name. But I don't know how to do it. I tried with cin.get() and cin.getline() without success.

    Code:
     
    #include <iostream> 
    #include <cstring>
    
    using namespace std; 
    
    const int SLEN = 30; 
    
    struct student { 
        
     char fullname[SLEN]; 
     char hobby[SLEN]; 
     int ooplevel;    
    };
    
    int getinfo(student *pa, int n);
    void display1(student st);
    void display2(const student *ps);
    void display3(const student *pa, int n); 
    
    int getinfo(student pa[], int n) 
    { 
      int i;
      for(i = 0; i < n; i++) 
      {   
        cout << "Enter student's full name: "; 
        cin >> pa[i].fullname;
        cout << "Enter student's hobby: ";
        cin >> pa[i].hobby;
        cout << "Enter student's OOP level: "; 
        cin >> pa[i].ooplevel;  
      }   
      return i;      
    }     
    
    void display1(student st) 
    { 
       cout << "Student full name: " << st.fullname << endl; 
       cout << "hobby: " << st.hobby << endl; 
       cout << "OOP level: " << st.ooplevel << endl;     
    }     
    
    void display2(const student *ps) 
    { 
       cout << "Student full name: " << ps->fullname << endl; 
       cout << "hobby: " << ps->hobby << endl; 
       cout << "OOP level: " << ps->ooplevel << endl;    
    }     
    
    void display3(const student *pa, int n) 
    { 
       for(int i = 0; i < n; i++) 
       { 
          cout << "Student full name: " << pa[i].fullname << endl; 
          cout << "hobby: " << pa[i].hobby << endl; 
          cout << "OOP level: " << pa[i].ooplevel << endl; 
       }         
    }     
    
    int main(void)
    {  
      
      cout << "Enter class size: ";
      int class_size;
      cin >> class_size;    
      while(cin.get() != '\n') 
        continue;
      student *ptr_stu = new student[class_size];
      int entered = getinfo(ptr_stu, class_size);
      for(int i = 0; i < entered; i++) 
      { 
         display1(ptr_stu[i]);
         display2(&ptr_stu[i]);      
      }       
      display3(ptr_stu, entered);
      delete [] ptr_stu; 
      cout << "Done\n";
      return 0;
    }
    Last edited by thames; 12-16-2012 at 06:30 PM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    If the user entered a blank line, than the current student's full name would compare equal to an empty string.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    What book are you reading, exactly? What edition?
    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.

  4. #4
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    What book are you reading, exactly? What edition?
    C++ Primer Plus, 6th edition

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    I don't know about the 6th edition, but the 4th edition is strongly discouraged.
    Seeing as the author is throwing in new and char* for strings, I can't imagine this edition is any better.
    I would recommend you switch book as soon as possible, if you can. This book is a recommended one.
    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.

  6. #6
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Code:
     Seeing as the author is throwing in new and char* for strings, I can't imagine this edition is any better.
    In fact, he mentions a lot about string too.

    Also, this book has great reviews at amazon.com .
    Last edited by thames; 12-20-2012 at 05:49 PM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Many books mention a lot of things, good and bad.
    Don't trust reviews from non-C++ experts. Amazon allows non-C++ experts to review books, and obviously newbies in any topic cannot know if they are misled. If you want reviews on books, I suggest you take a look at ACCU :: Book Reviews Search.
    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.

  8. #8
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    thanks for the link Elysia. I found the review about the c++ primer plus book to be very informative and helpful.
    Last edited by thames; 12-21-2012 at 11:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Search for Blank Line
    By uconnhuskies in forum C Programming
    Replies: 9
    Last Post: 07-21-2011, 08:09 PM
  2. Terminating input on EOF
    By vyn in forum C Programming
    Replies: 7
    Last Post: 03-12-2009, 02:33 AM
  3. Getchar terminating with new line or EOF
    By rocksteady in forum C Programming
    Replies: 1
    Last Post: 10-19-2007, 01:19 PM
  4. terminating input
    By Chaplin27 in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2004, 05:32 PM
  5. testing for blank line
    By rippascal in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2002, 08:50 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