Take3
Take3

Reputation: 11

Unable to enter a character and it results in infinite loop

The problem is I can't enter the value for 'c',its an infinite loop. I cant see what I did wrong it. I am very new to c++.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class manu
{
    private:
        int sqdno;
        string name,speciality,take;
        ofstream fileip;
        ifstream fileop;
    public:
        manu()
        {
            fileip.open("Manchester United.txt");
            fileop.open("Manchester United.txt");
        }    
    public:
        int input()
        {

            while(cin>>sqdno>>name>>speciality)
            {
              fileip<<sqdno<<' '<<name<<' '<<speciality<<endl;
            }
        }
    public:
        int display()
        {

            fileop>>take;
            while(fileop.good())
            {
                cout<<take<<endl;
                fileop>>take;
            }
        }   
};

int main()
{

    int c;
    manu m;
    cout<<"Enter squad details(press 'Ctrl+z' to exit on input):\n";

    do
    {
    cout<<"Select a choice:"<<endl;
    cout<<"1.Input to file"<<endl;
    cout<<"2.Display from file"<<endl;
    cout<<"3.Exit"<<endl;
    cin>>c;
    switch(c)
    {
        case 1:
            cout<<"Input squad_no,name and speciality of players:";
            m.input();
            break;
        case 2:
            m.display();
            break;
        default:
            cout<<"enter a valid choice";
    }
    }while(c<3);
}

Upvotes: 1

Views: 392

Answers (1)

Astro - Amit
Astro - Amit

Reputation: 767

Above code will not compile under visual stdio 8 compiler so because display and input function needs a return value.

So either change return type to void or return some int type of value from function.

I can't enter the value for 'c',its an infinite loop:

You have to modify your switch case if you want to run in infinite loop.

And your function input also have bug as it is also an infinite loop.You can take the reference of below code and modify your code accordingly....

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class manu
{
    private:
        int sqdno;
        string name,speciality,take;
        ofstream fileip;
        ifstream fileop;
    public:
        manu()
        {
            fileip.open("Manchester United.txt");
            fileop.open("Manchester United.txt");
        }    
    public:
        void input()
        {

            cin>>sqdno>>name>>speciality;

            fileip<<sqdno<<' '<<name<<' '<<speciality<<endl;

        }
    public:
        void display()
        {

            fileop>>take;
            while(fileop.good())
            {
                cout<<take<<endl;
                fileop>>take;
            }
        }   
};

int main()
{

    int c;
    manu m;
    cout<<"Enter squad details(press 'Ctrl+z' to exit on input):\n";

    while (1)
    {
        cout<<"Select a choice:"<<endl;
        cout<<"1.Input to file"<<endl;
        cout<<"2.Display from file"<<endl;
        cout<<"3.Exit"<<endl;
        cin>>c;
        switch(c)
        {
            case 1:
                cout<<"Input squad_no,name and speciality of players:";
                m.input();
                break;
            case 2:
                m.display();
                break;
            case 3:
            exit(1);
            default:
                cout<<"enter a valid choice";
                break;
        }
    }
}

Upvotes: 1

Related Questions