We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,605 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

separating strings in a structure

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.

2
Contributors
1
Reply
24 Minutes
Discussion Span
1 Year Ago
Last Updated
2
Views
Question
Answered
king03
Junior Poster
183 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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);

Ancient Dragon
Achieved Level 70
Team Colleague
32,109 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 68
Question Answered as of 1 Year Ago by Ancient Dragon

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0765 seconds using 2.85MB