In: Computer Science
can someone fix the infinite loop, it's stupid question, I don't know why for add field I get an infinite loop, but not for anything else
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int n, n2;
int n3;
int id; int m;
char keno[50]; int x;
struct entity
{
string random_words{};
string title{};
char keno[50]{};
int y_tho{};
int random_numbers{};
int ID{};
int id{};
int I_hate_this{};
int x{};
int m{};
};
int main()
{
cout << "Generic database creation
program.";
//vector to store employee data
vector< struct entity* > v;
entity e;
//loop to take continuous data from user
while (1)
{
cout << "What would you like
to do next?\n" << "1. Add entity\n" << "2. Edit
entity\n" << "3. Remove entity\n" << "4. Print entity
info\n" << "5. Stop the program\n" << ">> ";
cin >> n;
int ID, id, pos;
struct entity* entity = new struct
entity();
switch (n)
{
case 1:
//Please start
with 0 in console, I don't know how to automatically assign
them
cin >>
entity->ID;
cout <<
"The new entity with ID " << entity->ID << " has
been added to the database.\n";
v.push_back(entity); //Learned to use v.push_back from discord
chat, not my own idea
break;
case 2:
cout <<
"Please enter ID of the entity to edit: ";
cin >>
ID;
cout << "Editing entity (ID " << ID << " )... ";
pos =
-1;
for (size_t i =
0; i < v.size(); i++)
{
if (v[i]->ID == ID)
{
pos = i;
}
}
while (1)
{
cout << "What would you like to do
next\n";
cout << "1. Add field\n";
cout << "2. Edit field\n";
cout << "3. Remove field\n";
cout <<
"4. Go back to main menu\n";
cin >>
m;
switch (m)
{
while (1)
{
case 1:
cout << "Adding field
to entity ( ID " << ID << ") named: >>";
cin.get(e.keno, 50);
cout << "Select type :
0 - characters, 1 - integer, 2 - float, 3 - boolean: >>"
;
if (x == 0)
{
getline(cin, entity->random_words);
v.push_back(entity);
}
else if (x == 1)
{
cin
>> entity->y_tho;
v.push_back(entity);
}
else if (x == 2)
{
cin
>> entity->random_numbers;
v.push_back(entity);
}
else
{
getline(cin, entity->random_words);
v.push_back(entity);
}
break;
case 2:
cout << "Please enter
ID of the field to edit: ";
cin >> ID;
cout << "Editing entity (ID " << ID << " )... ";
pos = -1;
for (size_t i = 0; i <
v.size(); i++)
{
if (v[i]->ID == ID)
{
pos = i;
}
}
break;
case 3:
cout << " Please
provide field ID you want to remove: >> ";
cin >> ID;
pos = -1;
for (size_t i = 0; i <
v.size(); i++)
{
if
(v[i]->ID == ID)
{
pos = i;
}
}
v.erase(v.begin() +
ID);
break;
case 4: {
break;
}
}
break;
}
}
v.push_back(entity); //Learned to use v.push_back from discord
chat, not my own idea
break;
case 3:
cout <<
" Please provide entity ID: >> ";
cin >>
ID;
pos = -1;
for (size_t i =
0; i < v.size(); i++) {
if (v[i]->ID == ID) {
pos = i;
}
}
v.erase(v.begin() + ID);
break;
case 4:
cout <<
" Please provide entity ID: >> ";
cin >>
ID;
//search for
that employee in our database
pos = -1;
for (size_t i =
0; i < v.size(); i++) {
if (v[i]->ID == ID) {
ID = i;
}
}
cout <<
"Entity ID " << ID << " has" << ID << "
fields";
cout <<
"[Field " << ID << v[ID]->title;
break;
case 5:
return 0;
}
}
}
use c++
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int n, n2;
int n3;
int id; int m;
char keno[50]; int x;
struct entity
{
string random_words{};
string title{};
char keno[50]{};
int y_tho{};
int random_numbers{};
int ID{};
int id{};
int I_hate_this{};
int x{};
int m{};
};
int main()
{
cout << "Generic database creation program.";
//vector to store employee data
vector< struct entity* > v;
entity e;
//loop to take continuous data from user
while (1)
{
cout << "What would you like to do next?\n" << "1. Add entity\n" << "2. Edit entity\n" << "3. Remove entity\n" << "4. Print entity info\n" << "5. Stop the program\n" << ">> ";
cin >> n;
int ID, id, pos;
struct entity* entity = new struct entity();
switch (n)
{
case 1:
//Please start with 0 in console, I don't know how to automatically assign them
cout<<"Enter entity ID: ";
cin >> entity->ID;
cout << "The new entity with ID " << entity->ID << " has been added to the database.\n";
v.push_back(entity); //Learned to use v.push_back from discord chat, not my own idea
break;
case 2:
cout << "Please enter ID of the entity to edit: ";
cin >> ID;
cout << "Editing entity (ID " << ID << " )... ";
pos = -1;
for (size_t i = 0; i < v.size(); i++)
{
if (v[i]->ID == ID)
{
pos = i;
}
}
while (1)
{
cout << "What would you like to do next\n";
cout << "1. Add field\n";
cout << "2. Edit field\n";
cout << "3. Remove field\n";
cout << "4. Go back to main menu\n";
cin >> m;
switch (m)
{
while (1)
{
case 1:
cout << "Adding field to entity ( ID " << ID << ") named: >>"; cin.get(e.keno, 50);
cout << "Select type : 0 - characters, 1 - integer, 2 - float, 3 - boolean: >>" ;
if (x == 0)
{
getline(cin, entity->random_words);
v.push_back(entity);
}
else if (x == 1)
{
cin >> entity->y_tho;
v.push_back(entity);
}
else if (x == 2)
{
cin >> entity->random_numbers;
v.push_back(entity);
}
else
{
getline(cin, entity->random_words);
v.push_back(entity);
}
break;
case 2:
cout << "Please enter ID of the field to edit: ";
cin >> ID;
cout << "Editing entity (ID " << ID << " )... ";
pos = -1;
for (size_t i = 0; i < v.size(); i++)
{
if (v[i]->ID == ID)
{
pos = i;
}
}
break;
case 3:
cout << " Please provide field ID you want to remove: >> ";
cin >> ID;
pos = -1;
for (size_t i = 0; i < v.size(); i++)
{
if (v[i]->ID == ID)
{
pos = i;
}
}
v.erase(v.begin() + ID);
break;
case 4: {
break; }
}
break;
}
}
v.push_back(entity); //Learned to use v.push_back from discord chat, not my own idea
break;
case 3:
cout << " Please provide entity ID: >> ";
cin >> ID;
pos = -1;
for (size_t i = 0; i < v.size(); i++) {
if (v[i]->ID == ID) {
pos = i;
}
}
v.erase(v.begin() + ID);
break;
case 4:
cout << " Please provide entity ID: >> ";
cin >> ID;
//search for that employee in our database
pos = -1;
for (size_t i = 0; i < v.size(); i++) {
if (v[i]->ID == ID) {
ID = i;
}
}
cout << "Entity ID " << ID << " has" << ID << " fields";
cout << "[Field " << ID << v[ID]->title;
break;
case 5:
return 0;
}
}
}
=============================
No infinite loop now, SEE OUTPUT
-------------------------------------------
Thanks, let me know if you need more information. PLEASE COMMENT if
there is any concern.