Endless loop

This is a discussion on Endless loop within the C++ Programming forums, part of the General Programming Boards category; It works through the code then when I get to the "Do you wish to continue with the Database" and ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    32

    Unhappy Endless loop

    It works through the code then when I get to the "Do you wish to continue with the Database" and hit yes it LOOPs forever. To me it looks like it should work back though the code and then ask "Do you wish to continue with the Database" again. Where did I go wrong?
    Code:
    #include<iostream>
    
    using namespace std;
    
    struct video{
    	char* title;
    	char* num;
    	char* category;
    };
    
    void GetYesOrNo(char& response);
    
    int main()
    {
    	int    index = 0;
    	char   response;
    	char   temp[100] = " ";
    	video* vptr;
    
    	vptr = new video[index];
    
    	do
    	{
    		cout << "Enter video title: ";	
    		cin.get(temp, 100, '\n');
    		cin.ignore(100, '\n');
    		vptr[index].title = new char[strlen(temp) + 1];
    		strcpy(vptr[index].title, temp);
    		strcpy(temp, " ");
    		cout << endl;
    
    		cout << "Enter number in store: ";	
    		cin.get(temp, 100, '\n');
    		cin.ignore(100, '\n');
    		vptr[index].num = new char[strlen(temp) + 1];
    		strcpy(vptr[index].num, temp);
    		strcpy(temp, " ");
    		cout << endl;
    
    		cout << "Enter video category: ";	
    		cin.get(temp, 100, '\n');
    		cin.ignore(100, '\n');
    		vptr[index].category = new char[strlen(temp) + 1];
    		strcpy(vptr[index].category, temp);
    		strcpy(temp, " ");
    		cout << endl;
    
    		cout << "***************************************" << endl << endl;
    		cout << "You have entered the following: " << endl << endl;
    		cout << "Title:     " << vptr[index].title << endl;
    		cout << "Stock:     " << vptr[index].num << endl;
    		cout << "Category:  " << vptr[index].catagory << endl;
    
    		cout << "Do you wish to continue with the Database? (y or n): ";
    		GetYesOrNo(response);
    		if(response == 'y')
    		{
    			index++;
    		}
    	}	while(response == 'y');
    
    	return 0;
    }
    
    
    //----------------------------------------------------------------------------------------
    
    void GetYesOrNo(char& response)
    {
    	do
    	{
    		cin >> response;
    		if(response != 'y' && response != 'n')
    			cout << "Please enter 'y' or 'n': " << endl << endl;
    	}while(response != 'y' && response != 'n');
    
    	cout << endl;
    }
    Last edited by Zalbik; 01-30-2003 at 07:00 PM.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    i've looked through your code, i advice you to check it yourself
    cause there are a lot of "unesesary" variables in it.
    i think you will be able to find that out yourself

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    I think your problem may be that you need to flush the input buffer, but i'm not sure.

  4. #4
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    oh yeah, you have a few spelling errors which don't effect the programs execution, but the make the code annoying to read.

    they include your spelling of category and responce.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include<iostream>
    
    using namespace std;
    
    const max_videos = 100;
    struct video{
    	char* title;
    	char* num;
    	char* catagory;
    };
    
    void GetYesOrNo(char& response);
    
    int main()
    {
    	int    index = 0;
    	char   response;
    	char   temp[100] = " ";
    	video* vptr;
    
    	vptr = new video[max_videos];
    
    	do
    	{
    		cout << "Enter video title: ";	
    		cin.get(temp, 100, '\n');
    		cin.ignore(100, '\n');
    		vptr[index].title = new char[strlen(temp) + 1];
    		strcpy(vptr[index].title, temp);
    		strcpy(temp, " ");
    		cout << endl;
    
    		cout << "Enter number in store: ";	
    		cin.get(temp, 100, '\n');
    		cin.ignore(100, '\n');
    		vptr[index].num = new char[strlen(temp) + 1];
    		strcpy(vptr[index].num, temp);
    		strcpy(temp, " ");
    		cout << endl;
    
    		cout << "Enter video catagory: ";	
    		cin.get(temp, 100, '\n');
    		cin.ignore(100, '\n');
    		vptr[index].catagory = new char[strlen(temp) + 1];
    		strcpy(vptr[index].catagory, temp);
    		strcpy(temp, " ");
    		cout << endl;
    
    		cout << "***************************************" << endl << endl;
    		cout << "You have entered the following: " << endl << endl;
    		cout << "Title:     " << vptr[index].title << endl;
    		cout << "Stock:     " << vptr[index].num << endl;
    		cout << "Catagory:  " << vptr[index].catagory << endl;
    
    		cout << "Do you wish to continue with the Database? (y or n): ";
    		GetYesOrNo(response);
    		if(response == 'y')
    		{
    			index++;
    		}
    	}	while(response == 'y');
    
    	return 0;
    }
    
    
    //----------------------------------------------------------------------------------------
    
    void GetYesOrNo(char& response)
    {
    	do
    	{
    		cin >> response;
    		cin.ignore(100, '\n');
    		if(response != 'y' && response != 'n')
    			cout << "Please enter 'y' or 'n': " << endl << endl;
    	}while(response != 'y' && response != 'n');
    
    	cout << endl;
    }

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    Thanks...

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >const max_videos = 100;

    Oops, left off the type here. It should be:

    const int max_videos = 100;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fscanf endless loop
    By krum in forum C Programming
    Replies: 3
    Last Post: 12-05-2007, 12:14 AM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 09:15 AM

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