-3

I'm trying to build a beginner employee database program. Here is the problem; when counter "i" in main() becomes "1", the 1st line of the loop is skipped; in the other words, it doesn't wait for user to enter the value for "name" string. When I use "cin" rather than "gets" there is no problem...Why this is so? I use ubuntu 16.04.

#include <iostream>
#include <cstdio>
using namespace std;
void enter();
void report();
    char name[2][30],salary[2][30];
int main()
{
    int i;
    for(i=0;i<2;i++){
        cout<< "Name:"<<'\n';
        gets(name[i]);
        cout<< "Salary:"<<'\n'; cin>>(salary[i]);
    }
    report();
    return 0;
}
void report()
{
        int i;
        cout<<"Name"<<'\t'<<"Salary"<<'\n';
        for(i=0;i<2;i++){
            cout<< name[i]<<'\t'<<salary[i]<<'\n';
        }
}
Adrian W
  • 4,563
  • 11
  • 38
  • 52
Naghi
  • 145
  • 5

1 Answers1

1

Instead of using gets(), I would recommend using either std::cin >> name[i]; or cin.getline(name[i], 30);. The latter will fetch spaces.

You will then need a cin.ignore(); after the cin >> salary[i]; because of the extra return character that fills the buffer.

cwbusacker
  • 507
  • 2
  • 12