vendredi 15 avril 2022

why the line gets(q) in main function is ignore when i put it below cin>>f; but gets(q) function run when i put it above cin>>f

why the line gets(q) in main function is ignore when i put it below cin>>f; but gets(q) function run when i put it above cin>>f

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
class abc
{
private:
    /* data */
    int a;
    char b[40];

public:
    void read(int s,char c[])
    {
        a=s;
        strcpy(b,c);

    }
    void disp()
    {
        cout<<a<<"\n";
        puts(b);
    }
};


int main()
{   system("cls");
    abc obj1;
    int f;
    char q[40];
    cout<<"enter a number and a string ";
                       
    cin>>f;   #here 
    cout<<endl;
    gets(q);   #and here 

    obj1.read(f,q);
    obj1.disp();

}

but if i do this rest is same so here main function only

int main()
{   system("cls");
   abc obj1;
   int f;
   char q[40];
   cout<<"enter a number and a string ";
                      
   gets(q);   #this time i wrote gets function first
   cout<<endl;
   cin>>f;    #....

   obj1.read(f,q);
   obj1.disp();

}

it runs correctly please explain the reason

Aucun commentaire:

Enregistrer un commentaire