Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

why is this an infinite loop?

fflush loop

  • Please log in to reply
4 replies to this topic

#1 Jo_

Jo_

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 06 April 2010 - 03:14 AM

I don't know why it doesn't stop asking for the date. Even if I enter quit to go back to menu, it doesn't work and asks for another entry instead. I need to check if the date is in valid format but even if I enter a valid format, the program still says it is invalid. Please help. Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

typedef struct dvdorg
{
    char title[101];
    char genre[31];
    char actor[101];
    char actress[101];
    char release[51];
    char acqui[51];
    float price;
    char location[51];
    char note[101];

};

dvdorg *dvdpointer;
int size=sizeof(dvdorg);
int dvdcount = 0;

void menu (void);
void addentry (void);
void quit (char entry[101]);
int checkdate (char date[31]);
void search ();//dvdorg *stitle, dvdorg *sactor, dvdorg *sactress);

int main (void)
{
    printf ("\n\n\n\t\t\t\tDVD ORGANIZER\n\n\n\n\n\t\tpress any key to continue to the menu page...");
    getch();
    system ("cls");
    menu();
}

void menu (void)
{
    int action;
    do {
        printf ("\n\n\n\t\t\t\tMENU\n\n\n\t\t\t[1]Add DVD\n\t\t\t[2]Edit DVD\n\t\t\t[3]Delete DVD\n\t\t\t[4]View DVD Info");
        printf ("\n\t\t\t[5]Search\n\t\t\t[6]List all DVDs\n\t\t\t[7]Clear All\n\n");
        action=getch();
        system ("cls");
        switch(action)
        {
            case '1':
                addentry();
                break;
        //    case '2':
        //        editdvd();
        //    case '3':
        //        deletedvd();
        //    case '4':
        //        viewdvd();
            case '5':
                search();
        //    case '6':
        //        listall();
        //    case '7':
        //        clearall();
            default:
                printf ("Invalid input!\n\nPress any key to go back to main menu.\n");
                break;
        }
    getch();
    system ("cls");
    } while ((action!='1')||(action!='2')||(action!='3')||(action!='4')||(action!='5')||(action!='6')||(action!='7'));
}

void addentry (void)
{
    char saventry, temp[31], datetemp[51];
    int dateflag;
    FILE *add;
    add=fopen("dvdorg.bin", "wb");
    if (add==NULL)
    {
        printf ("Error. File does not exist.\nPress any key to go back to the main menu.");
        menu();
    }
    else
    {
        dvdpointer=(dvdorg *)malloc(size);
        printf ("ADD DVD\nNotes:\n\t-all fields are required.\n\t-type in 'quit' in any of the fields to go back to main menu.\n\n");
        printf ("Title: ");
        gets (dvdpointer[dvdcount+1].title);
        quit (dvdpointer[dvdcount+1].title);
        printf ("Genre: ");
        gets (dvdpointer[dvdcount+1].genre);
        quit (dvdpointer[dvdcount+1].genre);
        printf ("Actor(s): ");
        gets (dvdpointer[dvdcount+1].actor);
        quit (dvdpointer[dvdcount+1].actor);
        printf ("Actress(es): ");
        gets (dvdpointer[dvdcount+1].actress);
        quit (dvdpointer[dvdcount+1].actress);
        do {
            printf ("Release Date (YYYY-MM-DD): ");
            gets (datetemp);
            quit (temp);
            dateflag = checkdate(datetemp);
            if (dateflag==0)
                printf ("Invalid entry of date. Try Again.\n\n");
        } while (dateflag!=1);
        dvdpointer[dvdcount+1].release=datetemp;
        quit (dvdpointer[dvdcount+1].release);
        do {
            printf ("Acquisition Date (YYYY-MM-DD): ");
            gets (datetemp);
            quit (temp);
            dateflag = checkdate(datetemp);
            if (dateflag==0)
                printf ("Invalid entry of date. Try Again.\n\n");
        } while (dateflag!=1);
        dvdpointer[dvdcount+1].acqui=datetemp;
        quit (dvdpointer[dvdcount+1].acqui);
        printf ("Price: ");
        scanf ("%s", temp);
        quit (temp);
        dvdpointer[dvdcount+1].price=atof(temp);
        printf ("Location: ");
        gets (dvdpointer[dvdcount+1].location);
        quit (dvdpointer[dvdcount+1].location);
        printf ("Notes: ");
        gets (dvdpointer[dvdcount+1].note);
        quit (dvdpointer[dvdcount+1].note);
        printf ("Do you want to save this entry? (y/n) ");
        scanf ("%c", &saventry);
        if (saventry=='y')
        {
            dvdcount++;
            fflush (stdin);
            fwrite(&dvdpointer, size, 1, add);
            fclose(add);
        }
        printf ("Press any key to go back to main menu.\n");
        menu();
    }
}
//checks date validity:
void quit (char entry[101])
{
    if ((strcmp(entry, "quit"))==0)
    {
        system ("cls");
        menu();
    }
}

int checkdate (char date[51])
{    //0123 4 56 7 89
    //YYYY - MM - DD
    if ((date[4]!=' ')||(date[7]!=' '))
    {
        return 0;
    }
    if (date[5]=='1')
    {
        if ((date[6]!='0')||(date[6]!='1')||(date[6]!='2'))
        {
            return 0;
        }
        else if ((date[6]=='0')||(date[6]=='2'))
        {
            if (date[8]=='3')
            {
                if ((date[9]!='0')||(date[9]!='1'))
                    return 0;
            }
        }
        else if (date[6]=='1')
        {
            if (date[8]=='3')
            {
                if (date[9]!='0')
                {
                    return 0;    
                }
            }
        }
    }
    else if (date[5]=='0')
    {
        if (date[6]=='0')
            return 0;
        else if (date[6]=='2')
        {
            if (date[8]=='3')
                return 0; //feb days is never >=30!!!
        }
        else if ((date[6]=='1')||(date[6]=='3')||(date[6]=='5')||(date[6]=='7')||(date[6]=='8'))
        {
            if (date[8]=='3')
            {
                if ((date[9]!='0')||(date[9]!='1'))
                {
                    return 0;
                }
            }
        }
        else if ((date[6]=='4')||(date[6]=='6')||(date[6]=='9'))
        {
            if (date[8]=='3')
            {
                if (date[9]!=0)
                {
                    return 0;
                }
            }
        }
    }
    else
    {
        return 1;
    }
}

Please help. I'm not expert, i just began with C.

Edited by ZekeDragon, 06 April 2010 - 03:31 AM.
Please use [code] tags (the # button) when posting code.

  • 0

#2 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 06 April 2010 - 03:42 AM

There are several things wrong with your checkdate function that makes it so it frequently returns 0, for nearly every case. Let me give you a few examples...
if ((date[6]!='0')||(date[6]!='1')||(date[6]!='2'))
If date[6] does equal '0', then it most certainly will not equal '1', therefore this if statement will always be true. This means that the rest of your else if statements will never be executed.

Similarly, this
if ((date[9]!='0')||(date[9]!='1'))
will also always be true.

I think that due to this, your function will return 0 on most valid dates. Interestingly, due to this...
if (date[5]=='1')
    {
        // ...
    }
    else if (date[5]=='0')
    {
        // ... 
    }
    else
    {
        return 1;
    }
Your function will say the date is fine if you input something like the 32nd month or so. :P
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 Jo_

Jo_

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 06 April 2010 - 04:38 AM

thank you Mr.ZekeDragon

should I separate the conditions to different if-statements?
I took off the else statement and left alone return 1;
  • 0

#4 Jo_

Jo_

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 06 April 2010 - 04:54 AM

I changed the checkdate function but it still is not working:

int checkdate (char date[51])
{	//0123 4 56 7 89
	//YYYY - MM - DD
	if ((date[4]!=' ')||(date[7]!=' '))
	{
		return 0;
	}
	if (date[5]=='1')
	{
		if ((date[6]!='0')&&(date[6]!='1')&&(date[6]!='2'))
		{
			return 0;
		}
		else if ((date[6]=='0')||(date[6]=='2'))
		{
			if (date[8]=='3')
			{
				if ((date[9]!='0')&&(date[9]!='1'))
					return 0;
			}
		}
		else if (date[6]=='1')
		{
			if (date[8]=='3')
			{
				if (date[9]!='0')
				{
					return 0;	
				}
			}
		}
	}
	else if (date[5]=='0')
	{
		if (date[6]=='0')
			return 0;
		else if (date[6]=='2')
		{
			if (date[8]=='3')
				return 0; //feb days is never >=30!!!
		}
		else if ((date[6]=='1')||(date[6]=='3')||(date[6]=='5')||(date[6]=='7')||(date[6]=='8'))
		{
			if (date[8]=='3')
			{
				if ((date[9]!='0')&&(date[9]!='1'))
				{
					return 0;
				}
			}
		}
		else if ((date[6]=='4')||(date[6]=='6')||(date[6]=='9'))
		{
			if (date[8]=='3')
			{
				if (date[9]!=0)
				{
					return 0;
				}
			}
		}
	}
	return 1;
}

  • 0

#5 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 06 April 2010 - 11:59 AM

    do {
    //stuff
    } while ((action!='1')||(action!='2')||(action!='3')||(action!='4')||(action!='5')||(action!='6')||(action!='7'));
It will ALWAYS be the case that action is not 1 or action is not 2. ALWAYS.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/






Also tagged with one or more of these keywords: fflush, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download