I have 2 functions in my main(). When I call the first function void patient_main_menu(); it will leave me input all the required fields and it also will print to screen EXACTLY what I had inputed originally.

When the second function is called void doctor_main_menu(); it SKIPS/ Will not leave me INPUT the first field. I dont understand what I am doing wrong as it should be similar to the 1st function.

thx in advance for any advice

TIm
(main file attached for clarity)

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "patient.h"
#include "list.h"
#include "doctor.h"

void patient_main_menu();
void doctor_main_menu();

doctor DA[5];
static int i=0;
    
int main(int argc, char *argv[])
{
  
  
  patient_main_menu();
  
  doctor_main_menu();
  
  
  system("PAUSE");	
  return 0;
}



void patient_main_menu()
{

  char name[30];
  char dob[10];
  char illness[20];
  int t=1;
  int priority;
  int wardno;
  
  Sequence S;
  patient P;

  cout<<"Enter name of Patient:"<<endl;
  cin.getline(name,30);
  P.set_Name(name);
  
  cout<<"Enter Date of Birth eg 20/11/76:"<<endl;
  cin.getline(dob,10);
  P.set_dob(dob);
  
  cout<<"Enter Illness:"<<endl;
  cin.getline(illness,20);
  P.set_illness(illness);  
  
  cout<<"Enter Priority:"<<endl;
  cin>>priority;
  P.set_priority(priority); 
  
  cout<<"Enter Ward No:"<<endl;
  cin>>wardno;
  P.set_ward_no(wardno);   
        
  S.insert(t,P);    
  S.display();

}

void doctor_main_menu()
{

  char newname[20];
  char address[50];   
  int age;   
  int hours;   
  int payrate; 
  
 
  
  cout<<"Enter name of Doctor:"<<endl;                               //Problem is here with my cin //       cin.getline(newname,20);
  DA[i].set_Name(newname);
  
  cout<<"Enter Address:"<<endl;
  cin.getline(address,50);
  DA[i].set_Address(address);
  
  cout<<"Enter Age:"<<endl;
  cin>>age;
  DA[i].set_Age(age);  
  
  cout<<"Enter Hours Worked:"<<endl;
  cin>>hours;
  DA[i].set_Hours(hours); 
  
  cout<<"Enter Payrate:"<<endl;
  cin>>payrate;
  DA[i].set_Payrate(payrate); 
 
   
  cout<<DA[i].get_Name();
  cout<<DA[i].get_Address();
}

Dani AI

Generated

The symptom here is the classic "getline gets skipped" when you mix formatted extraction (operator>>) and line-based input. As pointed out (and confirmed), the numeric reads leave the newline in the input buffer, so the next line-oriented read immediately returns an empty string.

Explanation in short: cin >> someInt reads the number but stops before the trailing '\n'. A following getline sees that '\n' and consumes it as an empty line. The usual fix is to discard the rest of the current input line before using a line-based read.

A minimal, robust fix (use <limits> for the call below):

#include <limits>

int n;
std::cin >> n; // formatted extraction
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // flush rest of line

std::string name;
std::getline(std::cin, name); // now reads the intended line

Better practices to avoid this class of bugs:

  • Prefer std::string + std::getline for text input, and parse integers from those strings (with std::stoi or std::istringstream). That avoids mixing styles.
  • If you keep operator>>, always follow it with cin.ignore(...) before the next getline.
  • Never use fflush(stdin) (undefined behavior). If operator>> fails (bad input), call cin.clear() and then ignore.
  • Prefer std::getline(std::cin >> std::ws, s) or std::cin >> std::ws; std::getline(std::cin, s); to skip leading whitespace safely.

Applying this will stop the "skipped input" and is safer than juggling C-style char arrays.

Recommended Answers

All 2 Replies

that link solved my problem,

thx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.