Note that there are some explanatory texts on larger screens.

plurals
  1. POgets() not taking input
    primarykey
    data
    text
    <p>I have some college work and as i noticed that the gets() is not working but i can't figure out why.</p> <p>I tried putting getch() and getchar() before gets() but there is something else wrong.</p> <p>When i write a code implementing gets() before do-while (labeled -----> 3) it works!!!</p> <p>Can somebody help me?</p> <pre><code>#include&lt;iostream&gt; #include&lt;stdio.h&gt; #include&lt;conio.h&gt; using namespace std; class student { int rollNo; char department[20]; int year; int semester; public: student() { rollNo=0; year=0; semester=0; } void getData(); void promote(); void changeDepartment(); void display(); }; void student::changeDepartment() { if(rollNo!=0) { cout&lt;&lt;"\nEnter the new Department\n"; gets(department); --------------&gt;1 } else { cout&lt;&lt;"\nStudent not confirmed\n"; } } void student::getData() { cout&lt;&lt;"\nEnter the roll no\n"; cin&gt;&gt;rollNo; cout&lt;&lt;"\nEnter the year\n"; cin&gt;&gt;year; cout&lt;&lt;"\nEnter the semester\n"; cin&gt;&gt;semester; cout&lt;&lt;"\nEnter the department\n"; gets(department); ----------------&gt; 2 } void student::promote() { if(rollNo!=0) { semester+=1; if(semester%2==1) { year+=1; } } else { cout&lt;&lt;"\nStudent not confirmed\n"; } } void student::display() { if(rollNo!=0) { cout&lt;&lt;"\nRoll No : "&lt;&lt;rollNo; cout&lt;&lt;"\nYear : "&lt;&lt;year; cout&lt;&lt;"\nSemester : "&lt;&lt;semester; cout&lt;&lt;"\nDepartment : "&lt;&lt;department; } else { cout&lt;&lt;"\nStudent not confirmed"; } } int main() { student s; int ch; char choice; ----------------&gt; 3 do { cout&lt;&lt;"\nMain Menu"; cout&lt;&lt;"\n1. Enter student details"; cout&lt;&lt;"\n2. Change department of student "; cout&lt;&lt;"\n3. Promote student "; cout&lt;&lt;"\n4. Display student details "; cout&lt;&lt;"\nEnter your choice "; cin&gt;&gt;ch; switch(ch) { case 1 : s.getData(); s.display(); break; case 2 : s.changeDepartment(); s.display(); break; case 3 : s.promote(); s.display(); break; case 4 : s.display(); break; } cout&lt;&lt;"\nDo you want to continue? (Y/n)\n"; cin&gt;&gt;choice; }while((choice=='y')||(choice=='Y')); return(0); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload