Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem is that infinite looping is taking place and i can't figure out why this is  happening. It happens when I enter the choice as 1 and after entering Name, Role and Subject, it goes infinite. I've created a class "Professor" with data members as 3 vectors of type string and an "int count" along with three member functions as "Recruit", "DisplayInfo" and "Remove".  I'm basically trying to build a data of college professors.
I tried debugging the code. After first iteration it enters the loop and the switch and then directly to default case without me choosing the value for "choice"


What I have tried:

C++
<pre>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

class Professor
{
private:
    vector<string> Name;
    vector<string> Role;
    vector<string>Subject;
    static int count1;
public:
    void Recruit()
    {
        string name,role,subject;
        cout<<"Enter the Name: ";
        getline(cin, name);
        cin.ignore();
        Name.push_back(name);

        cout<<"Enter the Role: ";
        getline(cin, role);
        cin.ignore();
        Role.push_back(role);

        cout<<"Enter the Subject to be assigned: ";
        getline(cin, subject);
        cin.ignore();
        Subject.push_back(subject);
        count1++;
    }
    void DisplayInfo()
    {
        //cout<<"\nName   Role   Subject\n";
        for(int i=0; i<count1; i++)
        {
            cout<<"Name: "<<Name[i]<<"\n";
            cout<<"Role: "<<Role[i]<<"\n";
            cout<<"Subject: "<<Subject[i]<<"\n";   
        }
        cout<<"\n\n";
    }
    void Remove()
    {
        string name, role, subject;
        cout<<"Enter the name of the Professor, his/her Role and the subject assigned to him/her: ";
        cin>>name>>role>>subject;
        for(int i=0; i<count1; i++)
        {
            if(Name[i]==name && Role[i]==role && Subject[i]==subject)
            {
                vector<string>::iterator iter1 = find(Name.begin(), Name.end(), name);
                
                if(iter1 == Name.end())
                {
                    cout<<"";
                }
                else{
                    Name.erase(iter1);
                }
                vector<string>::iterator iter2 = find(Role.begin(), Role.end(), role);
                if(iter2 == Role.end())
                {
                    cout<<"";
                }
                else{
                    Role.erase(iter2);
                }
                vector<string>::iterator iter3 = find(Subject.begin(), Subject.end(), subject);
                if(iter3 == Role.end())
                {
                    cout<<"";
                }
                else{
                    Role.erase(iter3);
                }
            }
        }
    }
};


int main()
{
    Professor p;
    int choice;
    do{
        cout<<"You have the following Choices:\n";
        cout<<"1) Recruit a Professor\n";
        cout<<"2) Display the Information about Professors\n";
        cout<<"3) Remove a Professor\n";
        cout<<"4) Quit\n";
        cout<<"\n\nEnter the choice:";

        cin>>choice;

        switch(choice)
        {
            case 1:
            {
                p.Recruit();
                break; 
            }
            case 2:
            {
                p.DisplayInfo();
                break;
            }
            case 3:
            {
                p.Remove();
                break;
            }
            case 4:
            {
                break;
            }
            default:
            {
                cout<<"Sorry Wrong Choice!!\n";
                break;
            }
        }
    }while(choice!=4);
    return 0;
}
Posted
Updated 10-Apr-20 22:18pm
v4

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Your organization seems backwards to me. In my opinion, the name, role, and subject should be strings and the class should represent ONE professor. Then you should have a collection of those which could be a vector or list. Those collection have a size() method so you do not need a count member. Your main function would contain the instance of the collection. You could also make a class to contain the collection and the methods to display, remove, and add entries to the collection or those could be plain function that operate on the collection of professors. Here's one possibility :
C++
class Professor   // data for one professor
{
public:
    void Display()
    {
        cout << "Name   : " << Name << endln;
        cout << "Role   : " << Role << endln;
        cout << "Subject: " << Subject << endln;   
    }

private:
    std::string Name;
    std::string Role;
    std::string Subject;
};

class ProfCollection
{
public:
    Insert();
    Remove();
    Display();

private:
    std::vector< Professor >  Profs;
};
The next steps would be to implement the Insert, Remove, and Display methods of the collection class. They would be similar to what you have but different because they work on the collection of Professor objects.
 
Share this answer
 
Comments
Member 14798231 11-Apr-20 4:26am    
so ProfCollection would have to inherit Professor class, right?
Rick York 11-Apr-20 19:16pm    
No, not at all. ProfCollection owns the collection and manipulates it. There are more methods needed for accessing the members of Professor that were not included in the solution.
Member 14798231 12-Apr-20 4:31am    
How am i supposed carry out the remove operation. I tried by making the user input the name, role and subject of the professor to be removed and after that a for loop to iterate through the "Profs" vector and the using an if statement. But how am i supposed to remove that from the vector. erase() function gives an error
Quote:
The problem is that infinite looping is taking place and i can't figure out why this is happening.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

[Update]
Quote:
I tried debugging the code. After first iteration it enters the loop and the switch and then directly to default case without me choosing the value for "choice"

Pay attention to the value choice, how it evolve as the code is looping.
 
Share this answer
 
v2
Comments
Member 14798231 10-Apr-20 11:08am    
I tried debugging the code. After first iteration it enters the loop and the switch and then directly to default case without me choosing the value for "choice"
Patrice T 10-Apr-20 11:19am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Patrice T 10-Apr-20 11:20am    
Pay attention to value of choice.
did you press Enter ?
k5054 10-Apr-20 11:26am    
What does Professor::name[0] hold after adding it to the vector?
Why?
Also check the value of role and subject.
1. You have declared count1 as static, it should not be. It should be a normal instance variable.
2. You call cin.ignore() after every call to getline() in the Recruit method; these are unnecessary.
3. And more important. You do need to call cin.ignore after the line
C++
cin>>choice;

in the main loop. That will clear the input stream for the first getline call in Recruit.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900