the loop runs infinte for the second ittiration

//attempt two to make the student data base the other way
// this program  runs onlu once and does not hav any data base once u exit

#include<iostream>
#include<conio.h>

using std::cout;
using std::endl;
using std::cin;

int count=0;
class student
{
    double m_roll[100],m_class[100];
    char m_name[100],m_adress[100];
public:
    void getdata(int count)
    {
    cout<<" enter the name"<<endl;
    cin>>m_name[count];
    }
    void read_data(int count)
    {
        if (count==0)
        {
            cout<<"no data found";
        }

        for (int i=0;i<count;i++)
        {
            cout<<m_name[i];
        }
    }

};


int main()
{
    int user_input=0;

    student student1;
    do
    {
    cout<<"enter a choice"<<endl;
    cin>> user_input;
    if(user_input==1)
    {

        student1.getdata(count);
    count=count+1;
    user_input=0;
    }
    else
        if(user_input==2)
        {
            student1.read_data(count);
            user_input=0;
        }

    }while (user_input!=4);
        getch();
    return(0);



}

Recommended Answers

All 4 Replies

The only loops you've got seem to work fine, even after several iterations.

What exactly is your question?

it works gud only at first itiration.. tat is the do-while loop.. after the first run.. it askes me to enter a choice a infinite times

Yes, indeed.
Line 20:

cin>>m_name[count];

should be

cin>>m_name;

Also, it is ill-advised to use the #include <conio.h> header, because is a non-standard include, and is OS dependent (mainly Windows).

You printing function is also wrong.
For example, if I type in option 1, and I put there some string, for example, lol, the count (which would be 0 at first) would be incremented by 1. When calling the printing function, it will print only the first letter from that string, meaning l, because the for goes from 0 to count (which will be 1). I do suggest using strings: Click Here, both for values and for validating the loop entries.
Speaking of loops, your loop is not retard-proof. If I insert for example the character 's' in the option menu, instead of 1, 2 or 4, what would it happen? Do a simple for-based validation over a string in order to get your loop nicely done:

std::string answer; //#include <string>
for (;;){
    cout<<"Insert option: ";
    cin>>answer;
    if (answer == "4") break;
    else if (answer == "1") //do option 1
    else if (answer == "2") //do option 2
    //... conditions
    else cout<<"Invalid command.\n";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.