C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 27th, 2009, 04:32 AM
marlson marlson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 6 marlson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 11 m 2 sec
Reputation Power: 0
CIN getline problem and ignore statement

Code:
#include <iostream>
#include <conio.h> //getch() header file
#include <cstring>
#include <string>

using namespace std;

//--------------------------------------------------------
struct courseSection{
       
      char sectionNumber[6]; //first 3 character, last 3 digit
      char startTime[6];
      char startDate[10];
      char roomNumber[4];
      
//      char courseNo[6];
      courseSection *next;
      
}*startCourseSection=NULL;

//-------------------------------------------------------
struct course{

       char courseNumber[7];
       char title[20];
       char creditHours[10];
       course *next;

}*startCourse=NULL;


//--------------------------------------------------------
struct student{

       int studentID; //first digit must non-zero
       char name[20];
       char major[20]; 
       
       char sectionNo[6];
       student *next;

} *startStudent=NULL, *tempStudent, *temp2Student;
//---------------------------------------------------------

//struct enrollSection{
      
//       char sectionID[6];
//       char studentID[6];

//};

void addStudent();
void addCourseSection();
void addCourse();

void checkSection(struct courseSection *);
void checkCourse(struct course *);

int main (){

	void studentMain();
	void courseSectionMain();
    void courseMain();
   
      
	int mainchoice;
	char choice[2];

	do
	{
	system("cls");
	cout<<"\t==========================================="<<endl;
	cout<<"\t\t  HELP UNIVERSITY COLLEGE"<<endl;
	cout<<"\t==========================================="<<endl;
	cout<<"\t 1. Student Department"<<endl;
	cout<<"\t 2. Course Section Department"<<endl;
	cout<<"\t 3. Course Department"<<endl;
	cout<<"\t 4. EXIT"<<endl;
	cout<<"\t==========================================="<<endl;
	cout<<"\t Enter selection: ";

	cin>>choice;

	if (isdigit(choice[0])) /*check the first character of the input*/
		{
		mainchoice = atoi (choice); 
			
		} 
	else 
		{
			mainchoice = 999; /*case 999=case default*/
		}

	switch (mainchoice)
	{
	case 1: 
		
		{
    	studentMain();
		break;
		}
	
	case 2:
		
		{
		courseSectionMain();
		break;
		}
		
	case 3:
		
		{
		courseMain();
		break;
		}

	case 4:
		{
		cout<<"\n\t Exiting the program \n"<<endl;
		system("pause");
        break;
		}
	default:
		{
		cout<<"\n\t Invalid selection, please select again \n "<<endl;
		system("pause");
		break;
		}
	}
	
	}while (mainchoice!=4);
	
}
//-----------------------------------Main Menu for Student------------------------------
void studentMain(){

    int studentmainchoice;
	char studentchoice[2];

	do
	{
	system("cls");
	cout<<"\t==========================================="<<endl;
	cout<<"\t\t    STUDENT DEPARTMENT"<<endl;
	cout<<"\t==========================================="<<endl;
	cout<<"\t 1. Add Student"<<endl;
	cout<<"\t 2. Enrollment to Course Section"<<endl; // only if the course is available
	cout<<"\t 3. Withdrawal from Course Section"<<endl; // only the Course is available
	cout<<"\t 4. Displaying student's enrollment"<<endl; //
	cout<<"\t 5. EXIT to MainMenu"<<endl;
	cout<<"\t==========================================="<<endl;
	cout<<"\t Enter selection: ";

	cin>>studentchoice;

	if (isdigit(studentchoice[0])) /*check the first character of the input*/
		{
		studentmainchoice = atoi (studentchoice); 
			
		} 
	else 
		{
		studentmainchoice = 999; /*case 999=case default*/
		}

	switch (studentmainchoice)
	{
	case 1: 
		
		{
//    	addStudent();
		break;
		}
	
	case 2:
		
		{
//		enrollStudent();
		break;
		}
		
	case 3:
		
		{
	    //withdrawStudent();
		break;
		}
	
	case 4:
		
		{
	    //printList();
		break;
		}

	case 5:
		
		
        break;
	
	default:
		{
		cout<<"\n\t Invalid selection, please select again \n "<<endl;
		system("pause");
		break;
		}
	}
	
	}while (studentmainchoice!=5);

}
//---------------------------------Student Main Menu End--------------------------------

//--------------------------------Course Section Menu------------------------------------
void courseSectionMain(){

    int numcourSec_choice;
	char courSec_choice[2];
	

	do
	{
	system("cls");
	cout<<"\t==========================================="<<endl;
	cout<<"\t\t    Course Section Department"<<endl;
	cout<<"\t==========================================="<<endl;
	cout<<"\t 1. Create Course Section"<<endl;
	cout<<"\t 2. Delete Course Section"<<endl; // only if the course is available
	cout<<"\t 3. List of students by Course Number"<<endl; // only the Course is available
	cout<<"\t 4. EXIT to MainMenu"<<endl; //
	cout<<"\t==========================================="<<endl;
	cout<<"\t Enter selection: ";

	cin>>courSec_choice;

	if (isdigit(courSec_choice[0])) /*check the first character of the input*/
		{
		numcourSec_choice = atoi (courSec_choice); 
			
		} 
	else 
		{
		numcourSec_choice= 999; /*case 999=case default*/
		}

	switch (numcourSec_choice)
	{
	case 1: 
		
		{
    	addCourseSection();
		break;
		}
	
	case 2:
		
		{
		//deleteCourseSection();
		break;
		}
		
	case 3:
		
		{
	    //withdrawStudent();
		break;
		}
	
	case 4:
		
		{
	   
       	break;
		}

	
	default:
		{
		cout<<"\n\t Invalid selection, please select again \n "<<endl;
		system("pause");
		break;
		}
	}
	
	}while (numcourSec_choice!=4);

}
//-------------------------------Course Section Menu------------------------------------

//--------------------------------Course Menu------------------------------------
void courseMain(){

    int numcour_choice;
	char cour_choice[2];

	do
	{
	system("cls");
	cout<<"\t==========================================="<<endl;
	cout<<"\t\t    Course Department"<<endl;
	cout<<"\t==========================================="<<endl;
	cout<<"\t 1. Create Course"<<endl;
	cout<<"\t 2. Delete Course"<<endl; // only if the course is available
	cout<<"\t 3. Listing of students by course"<<endl; // only the Course is available
	cout<<"\t 4. EXIT to MainMenu"<<endl; //
	cout<<"\t==========================================="<<endl;
	cout<<"\t Enter selection: ";

	cin>>cour_choice;

	if (isdigit(cour_choice[0])) /*check the first character of the input*/
		{
		numcour_choice = atoi (cour_choice); 
			
		} 
	else 
		{
		numcour_choice= 999; /*case 999=case default*/
		}

	switch (numcour_choice)
	{
	case 1: 
		
		{
    	addCourse();
		break;
		}
	
	case 2:
		
		{
		//deleteCourse();
		break;
		}
		
	case 3:
		
		{
	    ;
		break;
		}
	
	case 4:
		
		{
       	break;
		}

	
	default:
		{
		cout<<"\n\t Invalid selection, please select again \n "<<endl;
		system("pause");
		break;
		}
	}
	
	}while (numcour_choice!=4);

}
//-------------------------------Course Menu Ended------------------------------

/*
void addStudent()
{
        tempStudent= new student();
        
        system("cls");
		cout<<"======================================"<<endl;
		cout<<"\t   Add Student Record"<<endl;
    	cout<<"======================================\n"<<endl;

		cout<<"\tEnter Student ID: "; //validaion - the starting digit is non-zero digit
/*
        char checkID[6];

        for(int i=0; i<6; i++){
                
                if( isalpha(checkID[i]) || (!isdigit(checkID[i])) )
                    {
                     cout<<"Invalid Input with non-numeric character"<<endl;
                     break;
                     }
                else if (checkID[0] != '0')
                     {
                     cout<<"Student ID must not start with a non-zero digit!"<<endl;
                     break;
                     }
                else continue;
        }        
        
        cin.get(checkID, 6); 
        strcpy(tempStudent->studentID,checkID); 
*/

/*
        cin>>tempStudent->studentNumber;
        

		cout<<"\tEnter Student Name: ";
		cin.getline(tempStudent->name, 20);
		cin.ignore(20, '\n');
	
		
		cout<<"\tEnter Major: ";
		cin>>tempStudent->major;

		tempStudent->next=NULL; //insert at the end of a list
		

		if (startStudent==NULL)
		
			startStudent=tempStudent;
		
		else 
		{
			temp2Student=startStudent;
			
			while(temp2Student->next!= NULL)
            {
            temp2Student=temp2Student->next;              
            } 
                                       
           temp2Student->next=tempStudent;
		}
       
}
*/


void addCourseSection()
{
     courseSection *tempSection, *temp2Section;
     tempSection = new courseSection;
//   char sectionNo[6];

 
        system("cls");
		cout<<"==============================================================================="<<endl;
		cout<<"\t\t\t\tAdd Course Section"<<endl;
    	cout<<"===============================================================================\n"<<endl;

		cout<<"\tEnter Section Number: ";
        cin>>tempSection->sectionNumber;
         
		cout<<"\tEnter start time: ";
		cin>>tempSection->startTime; 
		
		cout<<"\tEnter start date: ";
		cin>>tempSection->startDate;
		
		cout<<"\tEnter room number: ";
		cin>>tempSection->roomNumber;
		
		tempSection->next=NULL; //insert at the end of a list
	//	strcpy(temp->sectionNumber,sectionNo);

		if (startCourseSection==NULL)
		
			startCourseSection=tempSection;
		
		else 
		{
			temp2Section=startCourseSection;
			
			while(temp2Section->next!= NULL)
            {
            temp2Section=temp2Section->next;              
            } 
                                       
           temp2Section->next=tempSection;
		}
        checkSection(startCourseSection); //pass the head to section Table
     
}
//------------------------Add Course Section Menu End--------------------------

//bool validSection(&sectionNo){


//}

//-----------------------Course Section Table List ----------------------------
void checkSection(struct courseSection *ps)
{
     courseSection *temp;
     temp = new courseSection;
        
     temp=ps;
     
     cout<<"\n\n\t\t\t   LIST OF COURSE SECTION"<<endl;
     cout<<"\t--------------------------------------------------------------"<<endl;
     cout<<"\tSection Number\t    StartTime\t  StartDate\t RoomNumber"<<endl;
     cout<<"\t--------------------------------------------------------------"<<endl;
do{
   
       
       
   if (temp == NULL)
        cout<<" End of list" <<endl;
                  
   else
   
       {
   
       cout<<"\t    "<<temp->sectionNumber<<"\t      "<<temp->startTime<<"\t    "<<temp->startDate<<"\t\t     "<<temp->roomNumber<<endl;
   
       temp = temp -> next;
   
       }
       

    }while (temp != NULL);

   getch(); //pause the screen 
   delete temp; //release pointer
   
}

//----------------------------------Course Section Table/List End------------------------------

void addCourse()
{
        
        course *tempCourse, *temp2Course;
        tempCourse= new course();
   
        system("cls");
        //   checkSection(startCourseSection);
		cout<<"================================================================="<<endl;
		cout<<"\t\t\t\tAdd Course"<<endl;
    	cout<<"=================================================================\n"<<endl;

      	cout<<"\tEnter Course Number: "; //validaion - the starting digit is non-zero digit
        
        std::cin.getline(tempCourse->courseNumber, 6, '\n') ;
        cin.ignore('\n');
 
        cout<<"\tEnter title of Course: ";

        std::cin.getline(tempCourse->title, 20) ;
        
		cout<<"\tEnter credit hours: ";
        
        std::cin.getline(tempCourse->creditHours, 6, '\n') ;


		tempCourse->next=NULL; //insert at the end of a list
		

		if (startCourse==NULL)
		
			startCourse=tempCourse;
		
		else 
		{
			temp2Course=startCourse;
			
			while(temp2Course->next!= NULL)
            {
            temp2Course=temp2Course->next;              
            } 
                                       
           temp2Course->next=tempCourse;
		}

		checkCourse(startCourse);
      
}

//---------------------add Course end----------------------------------

//-----------------------Course Section Table List ----------------------------
void checkCourse(struct course *ps)
{
     course *temp;      
     temp=ps;
     
     cout<<"\n\n\t\t\t   LIST OF COURSE"<<endl;
     cout<<"\t----------------------------------------------------"<<endl;
     cout<<"\tCourse Number\t    Title\t  Credit Hours"<<endl;
     cout<<"\t----------------------------------------------------"<<endl;

do{
   
   if (temp == NULL)
        cout<<" End of list" <<endl;
                
   else
       {
   
       cout<<"\t"<<temp->courseNumber<<"\t\t"<<temp->title<<"\t\t"<<temp->creditHours<<endl;
       temp = temp -> next;
   
       }
       

    }while (temp != NULL);

   getch(); //pause the screen 
   delete temp; //release pointer
   
}

//----------------------------------Course Section Table/List End------------------------------


Problem lies in the addCourse function where i stored charatcer in array. i should be use cin.getline to store the courseNumber followed by a cin.ignore() statement.

How does cin clear the whitespace in an character array. let's say if i insert ABC 103.
without affecting the following input stream of startTime array.

Reply With Quote
  #2  
Old September 27th, 2009, 05:03 AM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398 jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
Quote:
Originally Posted by marlson
How does cin clear the whitespace in an character array.


Apparently that was supposed to be a question; yes?

cin doesn't "clear white space in an character array". If can be used in a number of ways to write data from the std input to whatever you decide to use to store that data internally.

Describe the results you expect to get from your program and then tell us what results you are actually getting. We're not mind readers.
__________________
My worst nightmare was a pointless infinite loop.
Work in progress; don't poke the curmudgeon!
http://www.odonahue.com/

Reply With Quote
  #3  
Old September 27th, 2009, 05:12 AM
marlson marlson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 6 marlson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 11 m 2 sec
Reputation Power: 0
Here is the part:
cout<<"\tEnter Course Number: ";
cin>>tempCourse->courseNumber;

cout<<"\tEnter title of Course: ";
cin>>tempCourse->title;

cout<<"\tEnter credit hours: ";
cin>>tempCourse->creditHours;


Let say user input: DIT 205 with space in between when prompt to "Enter course number: "

the next line would appear like this
Enter title of Course: Enter credit hours: (which skip the input of title which i don't want)

how can i make the compiler ignore the space so that it wouldn't skip like above.

Reply With Quote
  #4  
Old September 27th, 2009, 05:27 AM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398 jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
If you have cin >> somestring, then cin will skip all leading white space and copy all non-whitespace characters to somestring. Then it's done. If you wish to process strings with white space in them, use std::getline() or cin.getline(). That will require your users to hit enter when they are done inputting whatever you asked them for and then you can do whatever you like with that string.

Reply With Quote
  #5  
Old September 27th, 2009, 05:37 AM
marlson marlson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 6 marlson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 11 m 2 sec
Reputation Power: 0
so i alter
cin.getline(tempCourse->courseNumber, 6);

it skip my first input becoming like this

Enter course number: Enter title of course:
>.<

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > CIN getline problem and ignore statement

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap