Thread: how to read the integer member variable

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    41

    how to read the integer member variable

    this program is not giving to chance to enter the ooplevel value.please help
    Code:
    #include <iostream>
    using namespace std;
    const int SLEN = 30;
    struct student {
    	char fullname[SLEN];
    	char hobby[SLEN];
    	int ooplevel;
    };
    // getinfo() has two arguments: a pointer to the first element of
    // an array of student structures and an int representing the
    // number of elements of the array. The function solicits and
    // stores data about students. It terminates input upon filling
    // the array or upon encountering a blank line for the student
    // name. The function returns the actual number of array elements
    // filled.
    int getinfo(student pa[], int n);
    // display1() takes a student structure as an argument
    // and displays its contents
    void display1(student st);
    // display2() takes the address of student structure as an
    // argument and displays the structure’s contents
    void display2(const student * ps);
    // display3() takes the address of the first element of an array
    // of student structures and the number of array elements as
    // arguments and displays the contents of the structures
    void display3(const student pa[], int n);
    int main()
    {
    	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;
    }
    int getinfo(student pa[], int n)
    {
    	int num = 0;
    	int i;
    	for (i = 0; i < n; i++)
    	{
    		cout << "for student " << i + 1 << ":\n";
    		cout << "enter fullname:\n";
    		cin.getline(pa[i].fullname, SLEN);
    		if (!strcmp(pa[i].fullname, "\n"))break;
    		num++;
    		cout << "enter hobby:\n";
    		cin.getline(pa[i].hobby, SLEN);
    		cout << "enter ooplevel: ";
    		while (!cin >> pa[i].ooplevel)
    		{
    			cin.clear();
    			while (cin.get() != '\n');
    			cout << "Bad input.\n";
    			cout << "enter ooplevel: ";
    		}
    	}
    	return num;
    }
    void display1(student st)
    {
    	cout << "fullname: " << st.fullname << endl;
    	cout << "hobby: " << st.hobby << endl;
    	cout << "ooplevel: " << st.ooplevel << endl << endl;
    }
    void display2(const student * ps)
    {
    	cout << "fullname: " << ps->fullname << endl;
    	cout << "hobby: " << ps->hobby << endl;
    	cout << "ooplevel: " << ps->ooplevel << endl << endl;
    }
    void display3(const student pa[], int n)
    {
    	int i;
    	for (i = 0; i<n; i++)
    	{
    		cout << "for student " << i + 1 << ":\n";
    		cout << "fullname: " << pa[i].fullname << endl;
    		cout << "hobby: " << pa[i].hobby << endl;
    		cout << "ooplevel: " << pa[i].ooplevel << endl << endl;
    	}
    }

  2. #2
    Registered User
    Join Date
    Jul 2014
    Posts
    41
    Code:
    while (!cin >> pa[i].ooplevel)
    		{
    			cin.clear();
    			while (cin.get() != '\n');
    			cout << "Bad input.\n";
    			cout << "enter ooplevel: ";
    		}
    the code is for reading valid number values.
    Last edited by V8cTor; 10-16-2014 at 11:14 PM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Reading some non-string type and then a string type from std::cin causes problems because reading a non-type leaves a newline in the input buffer but strings happily consume this newline and does not ask for additional input.
    See how to clear the input buffer.

    >> char fullname[SLEN];
    >> char hobby[SLEN];
    Use std::string, i.e.:
    std::string fullname;
    std::string hobby;

    >>student * ptr_stu = new student[class_size];
    Use std::vector, i.e.
    std::vector<student> Students(class_size);

    >>display1(ptr_stu[i]);
    Assuming you used std::vector, use Students.at(i). Then you will know if you've accessed a record that does not exist (the [] operator causes undefined behavior if you access undefined elements).

    >>void display1(student st)
    Rule of thumb: Pass structures by const reference, i.e. void display(const student& st).

    >>while (!cin >> pa[i].ooplevel)
    ! binds tigher than >>, so this means while ((!cin) >> pa[i].ooplevel) which is highly unlikely what you want! Add parenthesises to change the evaluation order, i.e. while (!(cin >> pa[i].ooplevel)).

    >> int i;
    >> for (i = 0; i<n; i++)
    Since the variable is only going to be used inside the loop (typical with loop variables), declare it inside the loop, i.e. for (int i = 0; i<n; i++).
    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
    Registered User
    Join Date
    Jul 2014
    Posts
    41
    yeah, i didn't add the required parenthesis.thanks Elysia.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct integer member has wrong value
    By DoomerMrT in forum C Programming
    Replies: 2
    Last Post: 02-23-2013, 08:20 AM
  2. Replies: 7
    Last Post: 09-19-2011, 01:37 PM
  3. Replies: 7
    Last Post: 09-04-2011, 09:29 PM
  4. Passing member variable as default arg to member function
    By Memloop in forum C++ Programming
    Replies: 1
    Last Post: 09-08-2009, 06:49 PM
  5. member variable initialization
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2007, 09:52 AM