if else statement ????

This is a discussion on if else statement ???? within the C++ Programming forums, part of the General Programming Boards category; I`m trying to to use if statement but something going wrong. statement must be 2 steps IF There is still ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    24

    Arrow if else statement ????

    I`m trying to to use if statement but something going wrong.
    statement must be 2 steps
    IF There is still room in the array and
    IF there is not an Appointmen on the same Date and Time!
    Apointment will be accept......
    im not getting any error or worning
    but its not chaking second if statement and both else printing together
    Code:
    void addAppointmenRecords( )
    {  
    	int number, i,k;
    	system("cls");         //clear screen
    		cout<<"\nHow many Appointmen you wish to add? ";
    		cin>>number;		   //read number
    		cin.get();            //read newline character left in the buffer
    
    	if((number + currentSize ) <= listSize)        //There is still room in the array
    	{
    	  for( i = 1; i<=number; i++ )
               {
    			cout<<"\nEnter Person name: ";
    			getline(cin, AppointmenList[currentSize].name);
    			cout<<"Enter Appointmen Descriptions: ";
    			getline(cin, AppointmenList[currentSize].description);
    			cout<<"Enter Appointmen date: ";
    			cin>>AppointmenList[currentSize].appdate.day;
    			cin>>AppointmenList[currentSize].appdate.mounth;
    			cin>>AppointmenList[currentSize].appdate.year;
    			cin.get();							//read a character
    			cout<<"Enter Appointmen time: ";
                                                    getline(cin, AppointmenList[currentSize].time);
    			cout<<endl;
    			currentSize += 1; //update CurrentSize
    	  
    			
    			for(k=0; k<currentSize; k++)  //chech if there an Appointmen on the same Date and Time!
    			if( AppointmenList[currentSize].appdate.day == AppointmenList[k].appdate.day && 
    				AppointmenList[currentSize].appdate.mounth == AppointmenList[k].appdate.mounth && 
    				AppointmenList[currentSize].appdate.year == AppointmenList[k].appdate.year && 
    				AppointmenList[currentSize].time == AppointmenList[k].time )
    			{
    		        cout<<"\nGot a match\n";
    				cout<<"Appointmen Date and Time already filled"<<endl;
    				currentSize-=1;
    	        }
    			else
    			{
    				cout<<"Apointment accepted"<<endl;
    				currentSize+=1;
    			}
    	  }
    	}
    			else
    			{
    				cout<<"Overflow!!!! Appointmen List is full"<<endl;
    				cout<<"\nPress any key to continue"<<endl;
    				cin.get();     //read a character
    			}
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    It's almost impossible to see where blocks begin and end due to the mismatched braces in there. The indentation must be improved! Clearly it is going haywire due to the mixed tabs and spaces in there. Stick to one or fix it if your editor is being a bastard.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625

    Thumbs up

    Yeah, she're right. Clean up your code a bit, then post it again here! But, as i was looking through it, i saw you have 2 else statements the one next the other! Check this first.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Oh, sorry, NOW i saw it. Ahh, its such a mess in here. Forget what i said

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    sorry for my codes i tryed to clean a bit.
    its not checking
    if there an Appointmen on the same Date and Time!
    and printing
    "Apointment accepted"
    and
    Got a match
    Appointment Date and Time already filled
    can anybody see mistakes now??????

    Code:
    void addAppointmenRecords( )
    {  
    	int number, i,k;
    	system("cls");       //clear screen
    	cout<<"\nHow many Appointments do you wish to add? ";
    	cin>>number;        //read number
    	cin.get();            //read newline character left in the buffer
    
    	if((number + currentSize ) <= listSize)//There is still room in the array
    	 {
    		for( i = 1; i<=number; i++ )
    		 {
    			cout<<"\nEnter Person name: ";
    			getline(cin, AppointmenList[currentSize].name);
    			cout<<"Enter Appointment Descriptions: ";
    			getline(cin, AppointmenList[currentSize].description);
    			cout<<"Enter Appointment date: ";
    			cin>>AppointmenList[currentSize].appdate.day;
    			cin>>AppointmenList[currentSize].appdate.mounth;
    			cin>>AppointmenList[currentSize].appdate.year;
    			cin.get();//read a character
    			cout<<"Enter Appointment time: ";
    			getline(cin, AppointmenList[currentSize].time);
    			cout<<endl;
    			currentSize += 1; //update CurrentSize
    			for(k=0; k<currentSize; k++)  //check if there an Appointmen on the same Date and Time!
    			 {
    				if(AppointmenList[currentSize].appdate.day == AppointmenList[k].appdate.day && 
    				AppointmenList[currentSize].appdate.mounth == AppointmenList[k].appdate.mounth && 
    				AppointmenList[currentSize].appdate.year == AppointmenList[k].appdate.year && 
    				AppointmenList[currentSize].time == AppointmenList[k].time )
    				 {
    					cout<<"\nGot a match\n";
    					cout<<"Appointment Date and Time already filled"<<endl;
    					currentSize-=1;
    					break;
    				 }
    				else
    				 {
    					cout<<"Apointment accepted"<<endl;
    					currentSize+=1;
    				 }
    			 }
    		 }
    	 }
    	else
    	 {
    		cout<<"Overflow!!!! Appointment List is full"<<endl;
    		cout<<"\nPress any key to continue"<<endl;
    		cin.get();     //read a character
    	 }
    }

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Your second if statement is waaay big! Try encapsulating like this:
    Code:
    if ( )
    {
       if ( )
       { 
          if ( )
          { 
             /*.....*/
             if ( )
             { 
                 /*Your code here*/  
             }
              /*.....*/
           }
        }
    }

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your second if statement is waaay big!
    You probably have some sort of date_time class or struct. What you could do is overload the == operator for it, and then go:
    Code:
    if(AppointmenList[currentSize].appdate == AppointmenList[k].appdate)
    Perhaps your problem is incrementing currentSize twice per succesfully added appointment. Don't increment it before checking and only increment it in the "successfully added" branch. (In other words, the entered appointment is not what you are checking since you have changed currentSize in the meantime.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    you right anon
    i have date struct (day, mounth ,year) time is string i tried
    if(AppointmenList[currentSize].appdate == AppointmenList[k].appdate)
    but didn`t work and incrementing currentSize once now

    i couldnt figure out how im gonna use this
    Code:
    if ( )
    {
       if ( )
       { 
          if ( )
          { 
             /*.....*/
             if ( )
             { 
                 /*Your code here*/  
             }
              /*.....*/
           }
        }
    }
    here is my codes i still have problem with
    //check if there an Appointment on the same Date and Time!
    i can add more then one record on the same date and time which is not supposed to happen

    Code:
    void addAppointmentRecords( )
    {  
    	int number;
    	system("cls");       //clear screen
    	cout<<"\nHow many Appointments do you wish to add ";
    	cin>>number;        //read number
    	cin.get();         //read newline character left in the buffer
    
    	if((number + currentSize ) <= listSize)//There is still room in the array
    	 {
    		for(int i = 1; i<=number; i++)
    		 {
    			cout<<"\nEnter Person name: ";
    			getline(cin, AppointmentList[currentSize].name);
    			cout<<"Enter Appointment Descriptions: ";
    			getline(cin, AppointmentList[currentSize].description);
    			cout<<"Enter Appointment date: ";
    			cin>>AppointmentList[currentSize].appdate.day;
    			cin>>AppointmentList[currentSize].appdate.mounth;
    			cin>>AppointmentList[currentSize].appdate.year;
    			cin.get();            //read a character
    			cout<<"Enter Appointment time: ";
    			getline(cin, AppointmentList[currentSize].time);
    			cout<<endl;
    			currentSize += 1;    //update CurrentSize
    			for(int k=0; k<=currentSize; k++)
    			 {					//check if there an Appointment on the same Date and Time!
    				 if( AppointmentList[currentSize].appdate.day == AppointmentList[k].appdate.day && 
    				     AppointmentList[currentSize].appdate.mounth == AppointmentList[k].appdate.mounth && 
    				    AppointmentList[currentSize].appdate.year == AppointmentList[k].appdate.year && 
    				      AppointmentList[currentSize].time == AppointmentList[k].time )
    				{
    					cout<<"\nGot a match\n";
    					cout<<"Appointment Date and Time already filled"<<endl;
    					currentSize -= 1; //update CurrentSize
    					cin.get();
    				}
    				else
    				{
    					cout<<"Apointment accepted"<<endl;
    					cin.get();     //read a character
    					break;
    				}
    			 }
    		 }
    	 }
    	else
    	 {
    		cout<<"Overflow!!!! Appointment List is full"<<endl;
    		cout<<"\nPress any key to continue"<<endl;
    		cin.get();     //read a character
    	 }
    }

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suggest you write a function called CompareAppointMents() that take two appointment items and compare them, then use that instead of the four lines of
    Code:
                                         AppointmentList[currentSize].appdate.day == AppointmentList[k].appdate.day && 
    				     AppointmentList[currentSize].appdate.mounth == AppointmentList[k].appdate.mounth && 
    				    AppointmentList[currentSize].appdate.year == AppointmentList[k].appdate.year && 
    				      AppointmentList[currentSize].time == AppointmentList[k].time
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    you right anon
    i have date struct (day, mounth ,year) time is string i tried
    ...
    but didn`t work and incrementing currentSize once now
    You need to write code to make it work:

    Code:
    bool operator==(const date_time& a, const date_time& b)
    {
        return a.year == b.year && a.month == b.month && a.day == b.day && a.time == b.time;
    }
    (Appointments probably take some time and shouldn't overlap, though. In which case you might need a bit more complicated function here, and might call it AppointmentsOverlap or something like that.)

    As to incrementing currentSize, you shouldn't do it before checking. Your new appointment is at index currentSize and you want to check it against all appointments with index less than currentSize. If you increment it before checking, you will be comparing all appointments, including the new one, against an appointment that has no data yet.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    24

    Arrow

    thanks anon
    i done
    if(AppointmenList[currentSize].appdate == AppointmenList[k].appdate)
    but didnt solve my problem i even tried just
    if(AppointmentList[currentSize].time == AppointmentList[k].time)
    but still same
    so i dont think its a problem
    Your second if statement is waaay big!
    Last edited by pczafer; 04-29-2009 at 04:15 AM.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    but didnt solve my problem i even tried just
    This is a style issue (of breaking larger functions up into smaller and simpler functions) and not meant to solve the problem.

    I think I've mentioned the possible cause why your program isn't working right a couple of times. It's not how you compare appointment times but which appointments you compare in the first place.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 06:18 AM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21