2

Here is my C++ code

student.h

#include <string>

using namespace std;

class student
{
public:
void setname ();
string getname ();

void setstudentid ();
int getstudentid ();

student ();


private:
string name;
int studentid;

};

student.cpp

#include "student.h"
#include <iostream>

using namespace std;

student::student ()
{
    setname ();

    setstudentid ();

cout << "Student's Name: " << getname () << endl;
cout << "Student's ID: " << getstudentid () << endl;
}

void student::setname ()
{
    string x;

    cout << "Enter student's name: ";
    getline (cin , x);


    name = x;
}

string student::getname ()
{
    return name;
}

void student::setstudentid ()
{
    int x;

    cout << "Enter student's code: ";
    cin >> x;

    studentid = x;
}

int student::getstudentid ()
{
    return studentid;
}

main.cpp

#include <iostream>
#include <string>
#include "student.h"

using namespace std;

int main ()
{
    int a;

    cout << "1- stundent's info" << endl
        << "2- list of courses" << endl
        << "3- PickUp a course" << endl
        << "Please enter an option: ";
        cin >> a;

    if (a == 1)
    {
        student x;
    }
}

The problem is in the picture below:

enter image description here

How can I print them separately with initialized variable?

Bo Persson
  • 86,087
  • 31
  • 138
  • 198
  • Don't use `std::endl` unless you really need all the stuff that it does. `'\n'` starts a new line. – Pete Becker Dec 27 '15 at 15:32
  • Don't include pictures but the text, unless a picture is the only reasonable description. Further, reduce your code to the bare minimum required to reproduce the problem. Typically, that doesn't involve any unnecessary output and replaces user input with constants. – Ulrich Eckhardt Dec 27 '15 at 16:40

1 Answers1

1

After cin >> a, \n is left in the input buffer which is consumed in getline(cin,x).
Reset and flush cin buffer before reading input again.
How do I flush the cin buffer?

Community
  • 1
  • 1
Acha Bill
  • 1,197
  • 6
  • 16