Hi there guys I need a little help about separation of two worded strings. Below is a program that will ask a user to input first name, middle name, and last name. Whenever I input a two worded string in the "Enter First Name" part, for example: "Michael Jordan" and then I press enter, it jumps to "Enter Last Name", skipping the Enter Middle Name part.

To make things clear this is what happens:

Enter First Name: Francis //If one word only it doesn't skip the "Enter Middle Name" part.
Enter Middle Name: Jordan
Enter Last Name: Kobe

But when I input two words for the First Name, say:

Enter First Name: Derrick Rose
Enter Middle Name: //this part is skipped and I will be prompted to input Last Name. I am unable to input a Middle Name anymore when two words are inputted in first name.
Enter Last Name:


Here is the specific part of my program that I am having problems with:

void input()
{
     
     int x;
     studre record[100];
    for (int i=0;i<x;i++)
    {
        cout<<"Enter Student Number:"<<"["<<i+1<<"]:"<<endl;
        cin>>record[i].Sno;
        cout<<"Enter First Name:"<<"["<<i+1<<"]:"<<endl;
        cin>>record[i].Fname;
        cout<<"Enter Middle Name:"<<"["<<i+1<<"]:"<<endl;
        cin>>record[i].Mname;
        cout<<"Enter Last Name:"<<"["<<i+1<<"]:"<<endl;
        cin>>record[i].Sname;
        cout<<"Enter Course:"<<"["<<i+1<<"]:"<<endl;
        cin>>record[i].course;
        cout<<"Enter Year:"<<"["<<i+1<<"]:"<<endl;
        cin>>record[i].year;
        ClearScreen();
        }

And Here is my entire program code:

#include<iostream>
#include<string>
#include<windows.h>
#include<fstream>
#include <iomanip>
#include<cctype>
#include<cstdlib>

using namespace std;

struct TF
{int units, punits, misc;};

struct studre
{
       string Sno;
       string Fname;
       string Mname;
       string Sname;
       string course;
       int year;
       int fees;
      
};

void ClearScreen();
void input();
int main()
{
    int x;

    while(true)
    {
    cout<<"Enter number of students that will enroll (Max. of 5 students only)"<<endl;
    cin>>x;

try
{
if(x<1||x>5)
{
throw x;
}

}
catch(int a)
{
ClearScreen();
cout<<"Invalid input. Please try again."<<endl;
continue;
}
input();

}    

system ("pause");
    return 0;
    
}


void input()
{
     
     int x;
     studre record[100];
    for (int i=0;i<x;i++)
    {
        cout<<"Enter Student Number:"<<"["<<i+1<<"]:"<<endl;
        cin>>record[i].Sno;
        cout<<"Enter First Name:"<<"["<<i+1<<"]:"<<endl;
        cin>>record[i].Fname;
        cout<<"Enter Middle Name:"<<"["<<i+1<<"]:"<<endl;
        cin>>record[i].Mname;
        cout<<"Enter Last Name:"<<"["<<i+1<<"]:"<<endl;
        cin>>record[i].Sname;
        cout<<"Enter Course:"<<"["<<i+1<<"]:"<<endl;
        cin>>record[i].course;
        cout<<"Enter Year:"<<"["<<i+1<<"]:"<<endl;
        cin>>record[i].year;
        ClearScreen();
        }
    
}

    
void ClearScreen()
  {
  HANDLE hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD count;
  DWORD cellCount;
  COORD homeCoords = { 0, 0 };
  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi ))
  return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;
  SetConsoleCursorPosition( hStdOut, homeCoords );
  }

My program is not yet finish because of my problem with the Enter First Name part. Please help me guys I would really appreciate it.

cin stops reading keyboard input at the first space or the enter key. The next time cin is called it will just take whatever is already in the keybord buffer, if anything. call getline() instead of cin to allow for spaces in the string. getline(cin,record[i].Sno);

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.