-2

first iteration is successful but later unable to input name. Also i heard about not using gets, so i tried fgets also but it also didn't work either. Please help !!

i am working in codeblocks with c++ language

struct stu
{
    char n[25];
    int r;
}s[3];

void getinfo()
{
    for(int i=0;i<3;i++)

       {
            cout<<"name: ";
            gets(s[i].n);           
            cout<<"R.no: ";
            cin>>s[i].r;
       }
}

int main()
{
getinfo();

    for(int l=0;l<3;l++)
        {
            cout<<s[l].n;
            cout<<s[l].r;
        }
}

output, getting first iteration correctly but next time unable to input name

Zyke
  • 11
  • 1
  • I'd recommend you shouls use `std::string n;` instead of `char n[25];` and dump the `gets()`. – πάντα ῥεῖ Mar 16 '19 at 17:08
  • Adding to what @πάντα ῥεῖ said: Replace `gets` which is a C function with the C++ `std::getline(cin, )` too, also don't forget to do a `cin.ignore(, '\n')` to get rid of the newline that stays after using `getline` – mamg22 Mar 16 '19 at 17:13
  • can i use namespace std instead of std::string n ???? – Zyke Mar 16 '19 at 17:15
  • @Zyke _"can i use namespace std instead of std::string n ????"_ You shouldn't. – πάντα ῥεῖ Mar 16 '19 at 17:19
  • and using getline also not working!! same error – Zyke Mar 16 '19 at 17:19
  • isn't it the same thing @πάνταῥεῖ ?? – Zyke Mar 16 '19 at 17:20
  • @Zyke Read [here](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) why I don't recommend it. – πάντα ῥεῖ Mar 16 '19 at 17:21
  • ok thanks @πάνταῥεῖ, but what to use instead of gets?? getline also not working – Zyke Mar 16 '19 at 17:25
  • How are you using it? – mamg22 Mar 16 '19 at 17:26
  • @Zyke `std::getline()` would work well with `std::string` obeying [certain conditions](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). – πάντα ῥεῖ Mar 16 '19 at 17:27
  • @mamg22 std::getline(cin,n) – Zyke Mar 16 '19 at 17:28
  • Is the `cin.ignore(, '\n')` called before or after `getline`? – mamg22 Mar 16 '19 at 17:33
  • ok it is working now, i have no idea how cin.ignore() works. But thanks to both of you – Zyke Mar 16 '19 at 17:41
  • `cin.ignore(, '\n')` makes the program throw away characters of the input or until it finds a newline `'\n'`. When you extract something using `cin >> number`, it will leave the newline (enter) there, and when it's `getline` turn to read, it will find the newline and think that the user has just pressed enter without entering anything, making it appear that `getline` didn't get called. Placing `cin.ignore` before `getline` wipes that newline, so when `getline` gets to read, it will be prepared to read a string as usual. – mamg22 Mar 16 '19 at 17:47
  • thank you so much!! @mamg22 – Zyke Mar 17 '19 at 12:50

1 Answers1

0

Note:

cin.getline() – is used to read unformatted string (set of characters) from the standard input device.

The extraction operator >> does not remove the end of line character, so if you mix it with getline(), you will need to call cin.ignore(number,'\n'); to get rid of '\n'

#include <iostream>
#include <limits> 


struct stu
{
    char n[25];
    int r;
}s[3];

void getinfo()
{

    for(int i=0; i<3; i++)
    {
        std::cout << "name: ";
        std::cin.getline(s[i].n,25);


        std::cout << "R.no: ";
        std::cin >> s[i].r;

        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n');
    }
}

int main()
{
   getinfo();

   std::cout << std::endl;

   for(int l=0; l<3; l++)
   {
        std::cout << s[l].n << "  ";

        std::cout << s[l].r << std::endl;
   }

    return 0;
}

OUTPUT:

name: Henry the VIII                                                                                                                                                                                                                            
R.no: 1                                                                                                                                                                                                                                         
name: Ronald Regan                                                                                                                                                                                                                              
R.no: 2                                                                                                                                                                                                                                         
name: Adam K.                                                                                                                                                                                                                                   
R.no: 3                                                                                                                                                                                                                                         

Henry the VIII  1                                                                                                                                                                                                                               
Ronald Regan  2                                                                                                                                                                                                                                 
Adam K.  3  
sg7
  • 5,365
  • 1
  • 30
  • 39