Question

In: Computer Science

can someone fix the infinite loop, it's stupid question, I don't know why for add field...

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++

Solutions

Expert Solution

#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.


Related Solutions

It's NOT letting me upload a photo. I don't know why. Maybe you can google a...
It's NOT letting me upload a photo. I don't know why. Maybe you can google a photo? I'm sorry. Didn't think this would be such a hassle. Required Prepare a vertical analysis of both the balance sheets and income statements for 2019 and 2018.  Analysis Bal Sheet Analysis Inc Stmt  Complete this question by entering your answers in the tabs below. Prepare a vertical analysis of the balance sheets for 2019 and 2018. (Percentages may not add exactly...
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
Can someone please explain the acid cleavage reaction in detail? I know it's SN2 if its...
Can someone please explain the acid cleavage reaction in detail? I know it's SN2 if its a primary or secondary, and the less hindered carbon is attacked. I know SN1 occurs if a carbon is tertiary, but I don't know how these reactions look. Please help!
CAN SOMEONE WRITE THIS PAPER FOR ME PLEASE, I DON'T KNOW HOW TO DRIVE RECOMMENDATION REPORT,...
CAN SOMEONE WRITE THIS PAPER FOR ME PLEASE, I DON'T KNOW HOW TO DRIVE RECOMMENDATION REPORT, PLEASE HELP ME !!!!!! RECOMMENDATION REPORT ON HONDA ACCORD ( WORD 250)
Can someone please tell me why I am getting errors. I declared the map and it's...
Can someone please tell me why I am getting errors. I declared the map and it's values like instructed but it's telling me I'm wrong. #include <iostream> #include <stdio.h> #include <time.h> #include <chrono> #include <string> #include <cctype> #include <set> #include <map> #include "d_state.h" using namespace std; int main() { string name; map<string,string> s; map<string,string>::iterator it; s[“Maryland”] = "Salisbury"; s[“Arizona”] = "Phoenix"; s[“Florida”] = "Orlando"; s[“Califonia”] = "Sacramento"; s[“Virginia”] = "Richmond"; cout << "Enter a state:" << endl; cin >> name;...
I know the what the answers are but I don't know how to get them. Can...
I know the what the answers are but I don't know how to get them. Can you please explain the process? Thank you. Part VII. Discontinued Operations and Earnings per Share (11 points) Todd Corporation had pre-tax income for 2017 of $2,500,000. On December 31, 2017, Boyd disposed of a component of its business that represented a strategic shift in operation. That component had a Loss on Discontinued Operations of $450,000 (pre-tax). Boyd received $1,000,000 net cash proceeds from the...
HI, I hope you are doing well. I really don't understand this question and don't know...
HI, I hope you are doing well. I really don't understand this question and don't know how to solve it at all because I am completely new to this c++ programming. can you please explain each line of code with long and clear comments? please think of me as someone who doesn't know to code at all. and I want this code to be written in c++ thank you very much and I will make sure to leave thumbs up....
I'm Getting an "unindented error" Please fix the bolded codes. Because I don't know whats going...
I'm Getting an "unindented error" Please fix the bolded codes. Because I don't know whats going on. Thank You. # This program exercises lists. # The following files must be in the same folder: # abstractcollection.py # abstractlist.py # arraylist.py # arrays.py # linkedlist.py # node.py # input.txt - the input text file. # Input: input.txt # This file must be in the same folder. # To keep things simple: # This file contains no punctuation. # This file contains...
I need Answers to Question from P - V! I know it's a lot but they...
I need Answers to Question from P - V! I know it's a lot but they are all small questions related to the same case so I didn't know how to split it into multiple questions. I would really appreciate the help, as I need a way to compare my answers. Dallas & Associates Financial Statement Preparation & Analysis You have been hired as a senior financial analyst for Dallas and Associates and you are in charge of preparing the...
IMPORTANT: I know the answer is "C". However, I don't know why. Could you please explain...
IMPORTANT: I know the answer is "C". However, I don't know why. Could you please explain why? Thank you A linear total cost curve that passes through the origin implies that a.         average cost is constant and marginal cost is variable. b.         average cost is variable and marginal cost is constant.             c.         average and marginal costs are constant and equal.             d.         you need more information to answer question.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT