Jump to content

is this correct this far? displaying students record...

- - - - -

  • Please log in to reply
8 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi

This is an incomplete code. I just wanted to know if it's correct so far. Please have a look and let me know what you think. Thanks. My purpose is to enter data of some students, then display that data inside some "asterisk box" using some user-defined function.


// displaying students' data using a user-define function


#include <iostream>

#include <cstdlib>


using namespace std;


/////////////////////////////////////////////////////////////////

struct STUDENT

{ string name, DofAdm; int age, marks; char sex; };

/////////////////////////////////////////////////////////////////


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;

    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;


    return 0;


    system("pause");


}


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
That looks right to me. Personally, I like to put lines between cout and cin statements. Also, you don't need to put endl;'s on separate cout statements. you can have more than one, like this for example:

cout << "enter the details" << endl << endl;


Latinamne loqueris?

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

mebob said:

That looks right to me. Personally, I like to put lines between cout and cin statements. Also, you don't need to put endl;'s on separate cout statements. you can have more than one, like this for example:

cout << "enter the details" << endl << endl;


Thank you, mebob, for the suggestions.

But I don't think all is okay with the incomplete code I posted. Here is the output:


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


[B]enter name: enter sex: enter age: enter date of admission: enter marks:[/B]


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.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
I just extended the code.

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.


// 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;


}

//--------------------------------------------------------


As I mentioned in the previous post the code all is not okay. Here is the output:


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


[B]enter name: enter sex: enter age: enter date of admission: enter marks:[/B]


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.images...formatiwant.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, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Your problem lies here (at least part of it):

cout << "enter date of admission: "; cin >> s1.DofAdm;

std::cin reads up to white space, and judging from your sample input you enter date with spaces - white space, so April and 2011 are fed to other std::cin's. You'll have to use getline to read whole line or use a different date format.

And a small note.

return 0;

system("pause");

That system call will never execute because return statement is above it. It seems you're using Code::Blocks which does that for you, running your program in another console, so you don't need that system call at all.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

Flying Dutchman said:

Your problem lies here (at least part of it):

cout << "enter date of admission: "; cin >> s1.DofAdm;

std::cin reads up to white space, and judging from your sample input you enter date with spaces - white space, so April and 2011 are fed to other std::cin's. You'll have to use getline to read whole line or use a different date format.

Thanks a lot, Dutchman. How do I use "getline" feature? Could you please give me an example?

Flying Dutchman said:

And a small note.

return 0;

system("pause");

That system call will never execute because return statement is above it. It seems you're using Code::Blocks which does that for you, running your program in another console, so you don't need that system call at all.

Yes, I'm using Code::Blocks. I have corrected this in my signature. I still don't understand the bold part.

The amended code is:

// 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(char, int);


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;


    cout << "STUDENTS' DATA" << endl << 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)


{

    for (int j=0; j<=n; j++)

        {

        cout << ch;

        }

    cout << endl;

}

//--------------------------------------------------------


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#7
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
On this page you'll find getline example along with parameter and return values explanation.

When you run your program with Code::Blocks, you'll notice that in the bottom (Build log tab) something like "running program.exe via cb_console_runner.exe", which is pretty much like opening a terminal/cmd window and run it from there but CB does that for you. Console based programs are/were meant to be ran from a console and there you don't really need to pause before closing.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#8
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thanks, Dutchman.

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? Perhaps I would have make use of setw in some way. Is there any way to automate this process of generating vertical columns of "*" or some other such character such as "="?


// 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:

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?


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#9
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
There are libraries that let you manually position the cursor but those are system dependant. I think it's better to just print spaces.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users