-
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;
}
-
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
:)
-
I think your problem may be that you need to flush the input buffer, but i'm not sure.
-
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.
-
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;
}
-
-
>const max_videos = 100;
Oops, left off the type here. It should be:
const int max_videos = 100;