Thread: displaying students' data within a box

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    displaying students' data within a box

    Hi

    Please have a look and let me know what you think. I don't know how to achieve my end purpose. My purpose is to enter data of some students, then display that data inside some border made from "*" or "=" using some user-defined function.

    Code:
    // displaying students' data using a user-define function
    
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    
    using namespace std;
    
    /////////////////////////////////////////////////////////////////
    struct STUDENT
    { string name, DofAdm, sex; int age, marks; };
    /////////////////////////////////////////////////////////////////
    
    void prntborder();
    
    int main ()
    
    {
        STUDENT s1, s2;
    
        cout << "enter the details" << endl;
    
        cout << endl;
    
        cout << "enter details of first student" << endl;
        cout << "enter name: "; cin >> s1.name;
        cout << "enter sex: "; cin >> s1.sex;
        cout << "enter age: "; cin >> s1.age;
        cout << "enter date of admission: "; cin >> s1.DofAdm;
        cout << "enter marks: "; cin >> s1.marks;
    
        cout << endl;
    
        cout << "enter details of second student" << endl << endl;
        cout << "enter name: "; cin >> s2.name;
        cout << "enter sex: "; cin >> s2.sex;
        cout << "enter age: "; cin >> s2.age;
        cout << "enter date of admission: "; cin >> s2.DofAdm;
        cout << "enter marks: "; cin >> s2.marks;
    
        cout << endl;
    
        prntborder('*', 25);
    
        cout << "Details for first student" << endl;
        cout << "Name is: " << s1.name << endl;
        cout << "Sex is: " << s1.sex << endl;
        cout << "Age is: " << s1.age << endl;
        cout << "Date of Admission is: " << s1.DofAdm << endl;
        cout << "Mark: " << s1.marks << endl << endl;
    
        cout << "Details for second student" << endl;
        cout << "Name is: " << s2.name << endl;
        cout << "Sex is: " << s2.sex << endl;
        cout << "Age is: " << s2.age << endl;
        cout << "Date of Admission is: " << s2.DofAdm << endl;
        cout << "Mark: " << s2.marks << endl << endl << endl;
    
        prntborder('*', 25);
    
        return 0;
    
        system("pause");
    
    }
    
    //--------------------------------------------------------
    // definition for a function to make the data output pretty
    // prntborder()
    // function definition
    
    void prntborder(char ch, int n)
    
    {
    
        cout << "STUDENTS' DATA" << endl << endl << endl;
        for (int j=0; j<=n; j++)
        cout << ch << endl << endl;
    
    }
    //--------------------------------------------------------
    I don't think all is okay with the incomplete above code. Here is the output:

    Code:
    enter the details
    
    enter details of first student
    enter name: John
    enter sex: Male
    enter age: 20
    enter date of admission: 25th April 2011
    enter marks:
    enter details of second student
    
    enter name: enter sex: enter age: enter date of admission: enter marks:
    
    Process returned 0 (0x0)   execution time : 19.798 s
    Press any key to continue.
    It doesn't even ask me to enter the details for the second student. Where did I go wrong? Please let me know. Thanks.

    This is the border I want to display my end result in:
    http://img859.imageshack.us/img859/8...ormatiwant.jpg

    I don't even know how get columns of "*" and how to increase the size of "STUDENTS' DATA" and how place it in the center at the top. Please help me.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > It doesn't even ask me to enter the details for the second student. Where did I go wrong?
    You're inputting "25th April 2011" into a single string, using >>
    Which means, with space as a separator, you get "25th" stored in DofAdm, and then you try to parse "April" as an integer for marks.
    This naturally fails, putting the whole cin stream into an error state, and thus all remaining input is skipped.

    > I don't even know how get columns of "*" and how to increase the size of "STUDENTS' DATA" and how place it in the center at the top
    This is a lot harder than it might first seem (and changing font size/face in a console is pretty much impossible to begin with).

    Assuming you're using win32 consoles, read this tutorial.
    Win32 Console Applications 1
    Once you know how to position the cursor at any point on the screen, you should be able to draw your box.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Salem View Post
    > It doesn't even ask me to enter the details for the second student. Where did I go wrong?
    You're inputting "25th April 2011" into a single string, using >>
    Which means, with space as a separator, you get "25th" stored in DofAdm, and then you try to parse "April" as an integer for marks.
    This naturally fails, putting the whole cin stream into an error state, and thus all remaining input is skipped.
    Thanks a lot, Salem. I understand it now. Perhaps, I should try to use "_" while inputting the date of admission. What do you say? Please let me know. Or, perhaps I could use a structure DATE to declare the date of admission.

    Quote Originally Posted by Salem View Post
    > I don't even know how get columns of "*" and how to increase the size of "STUDENTS' DATA" and how place it in the center at the top
    This is a lot harder than it might first seem (and changing font size/face in a console is pretty much impossible to begin with).

    Assuming you're using win32 consoles, read this tutorial.
    Win32 Console Applications 1
    Once you know how to position the cursor at any point on the screen, you should be able to draw your box.
    I think I should stop worrying too much to make it nice looking rather should focus on learning programming techniques. Suppose, I don't want to change the size or face of the "STUDENTS' DATA", then is this 'easily' possible to get the surrounding vertical columns of "*"?

    Thanks a lot for all the help.

    EDIT:

    I was 'manually' able to get the vertical column of "*" on left side of output. Is there some way to get it 'automatically'? How do I get the column on right of side of the output?
    Code:
    // displaying students' data using a user-define functionS
    
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    
    using namespace std;
    
    /////////////////////////////////////////////////////////////////
    struct STUDENT
    { string name, DofAdm, sex; int age, marks; };
    /////////////////////////////////////////////////////////////////
    
    void prntborder(char, int);
    void prntstudent (STUDENT);
    
    int main ()
    
    {
        STUDENT s;
        char ch;
    
            do
            {
                int j=1;
    
                cout << "enter the details" << endl;
    
                cout << endl;
    
                cout << "enter details of " << "#" << j << " student" << endl;
                cout << "enter name: "; cin >> s.name;
                cout << "enter sex: "; cin >> s.sex;
                cout << "enter age: "; cin >> s.age;
                cout << "enter date of admission: "; cin >> s.DofAdm;
                cout << "enter marks: "; cin >> s.marks;
    
                cout << endl;
    
                cout << "STUDENT DATA" << endl;
    
                prntborder('*', 25);
    
                prntstudent(s);
    
                prntborder('*', 25);
    
                cout << endl;
    
                cout << "Do you want to enter new data? "; cin >> ch;
    
                j = j++;
    
                cout << endl;
    
            }
            while (ch != 'n');
    
        system("pause");
    
        return 0;
    
    }
    
    //--------------------------------------------------------
    // definition for a function which prints student structure
    // prntstudent (STUDENT)
    
    void prntstudent (STUDENT dummy)
    
    {
        cout << "* Name is: " << dummy.name << endl;
        cout << "* Sex is: " << dummy.sex << endl;
        cout << "* Age is: " << dummy.age << endl;
        cout << "* Date of Admission is: " << dummy.DofAdm << endl;
        cout << "* Mark: " << dummy.marks << endl;
    
    }
    //--------------------------------------------------------
    
    //--------------------------------------------------------
    // definition for a function to make the data output pretty
    // prntborder()
    // function definition
    
    void prntborder(char ch, int n)
    
    {
        for (int j=0; j<=n; j++)
            {
            cout << ch;
            }
        cout << endl;
    }
    //--------------------------------------------------------
    Output:
    Code:
    enter the details
    
    enter details of #1 student
    enter name: jack
    enter sex: male
    enter age: 16
    enter date of admission: 1stDec2011
    enter marks: 70
    
    STUDENT DATA
    **************************
    * Name is: jack
    * Sex is: male
    * Age is: 16
    * Date of Admission is: 1stDec2011
    * Mark: 70
    **************************
    
    Do you want to enter new data?
    Last edited by jackson6612; 05-06-2011 at 03:26 PM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Please help!
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Help with what?

    You didn't use any of the information in the last URL I posted, will you do any better with this one?
    stringstream - C++ Reference

    Code:
        cout << "* Name is: " << dummy.name << endl;
    Put the red part into a string stream, get the length, then use setw() to pad with an appropriate number of spaces (do some math), to print a * at the right column on the right hand side.
    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.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Salem View Post
    Help with what?

    You didn't use any of the information in the last URL I posted, will you do any better with this one?
    stringstream - C++ Reference

    Code:
        cout << "* Name is: " << dummy.name << endl;
    Put the red part into a string stream, get the length, then use setw() to pad with an appropriate number of spaces (do some math), to print a * at the right column on the right hand side.
    Sorry, Salem. I had been to the URL you referred me to but I couldn't understand the material there out of my own limited knowledge. So I posted again in the hope that perhaps someone would come up with some 'simple' method. But I believe there is no easy way to do this.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using CreateFileMapping - Displaying Data
    By AnkleSpankle in forum Windows Programming
    Replies: 6
    Last Post: 04-17-2010, 02:21 PM
  2. Displaying Data from a File
    By drkidd22 in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2009, 06:55 PM
  3. Displaying Data in C programmming
    By sboothman in forum C Programming
    Replies: 1
    Last Post: 05-11-2004, 08:02 PM
  4. displaying data
    By julianluthor in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2004, 10:56 AM
  5. Any Computeach International Students/ex students??
    By stevey in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 03-26-2002, 04:12 PM