HELP

Why does the name and section ask at the same time when it your about to fill up the 2nd borrower?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  #include <iostream>
using namespace std;


struct student

{ 	char name1[1000], sec1[5], name2[2000], sec2[6], name3[3000], sec3[7];
	int exp1, exp2, exp3;
	string course1, course2, course3;
};

int main() {
	
	student a, b, c;	

	
//Borrower 1
	cout << "Enter Full name: "<<endl;
    cin.getline (a.name1, 150);
    cout << "Section: "<<endl;
    cin.getline (a.sec1, 5);
    cout << "Enter subject: "<<endl;
    cin >>a.course1;
    cout<<"Experiment number: "<<endl;
    cin>>a.exp1;
    
   	cout<<"Name: "<<a.name1<<endl;
	cout<<"Section: "<<a.sec1<<endl;
	cout<<"Subject: "<<a.course1<<endl;
	cout<<"Experiment number: "<<a.exp1<<endl;
    
//Borrower 2
	cout << "Enter Full name: "<<endl;
   	cin.getline (b.name2, 150);
    cout << "Section: "<<endl;
    cin.getline (b.sec2, 5);
    cout << "Enter subject: "<<endl;
    cin >>b.course2;
    cout<<"Experiment number: "<<endl;
    cin>>b.exp2;

    cout<<"Name: "<<b.name2<<endl;
	cout<<"Section: "<<b.sec2<<endl;
	cout<<"Subject: "<<b.course2<<endl;
	cout<<"Experiment number: "<<b.exp2<<endl;   

//Borrower 3
	cout << "Enter Full name: "<<endl;
    cin.getline (c.name3, 150);
    cout << "Section: "<<endl;
    cin.getline (c.sec3, 5);
    cout << "Enter subject: "<<endl;
    cin >>c.course3;
    cout<<"Experiment number: "<<endl;
    cin>>c.exp3;
    
cout<<"Name: "<<c.name3<<endl;
	cout<<"Section: "<<c.sec3<<endl;
	cout<<"Subject: "<<c.course3<<endl;
	cout<<"Experiment number: "<<c.exp3<<endl;

    



	
return 0;
	
}
The version of “std::getline()” you are using
basic_istream& getline( char_type* s, std::streamsize count );
, defined in <istream>, is not as user-friendly as its counterpart
std::basic_istream& getline( std::basic_istream& input, std::basic_string& str, CharT delim );
, defined in <string>, when it comes to reading from keyboard.

I suggest you the second one, in case getting rid of the unwanted characters.

The first version of std:.getline() sets failbit “if it extracts no characters, if it fills in the provided buffer without encountering the delimiter, or if the provided buffer size is less than 1”.
http://en.cppreference.com/w/cpp/io/ios_base/iostate
It means that in your code:
1
2
cout << "Section: "<<endl;
cin.getline (a.sec1, 5);

failbit is set if a word like “alpha” (or longer) is met. Afterwards it must be restored.

I’ve slightly modified your code to emphasize those problems. There are many workarounds, but I think you can find them quite easily.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <limits>
#include <string>

using namespace std;

struct student
{
    char name1[1000], sec1[5], name2[2000], sec2[6], name3[3000], sec3[7];
    int exp1, exp2, exp3;
    string course1, course2, course3;
};

int main()
{
    student a, b, c;

//Borrower 1
    cout << "Enter Full name: ";
    string fullname;
    getline(cin, fullname);
    if(fullname.length() > 5) {
        fullname.resize(5);
        std::cout << "fullname is now " << fullname << '\n';
    }

//    cin.getline (a.name1, 150);
    cout << "Section: ";
    cin.getline (a.sec1, 5);
    if (!cin) {
        cout << "\nstd::cin is currently in a incorrect state.\n"
                "Testing if std::ios_base::failbit...\n";
        if (cin.fail()) {
                cout << "std::cin is set to std::ios_base::failbit: "
                        "input/output operation failed (formatting or extraction error).\n";
        } else {
                cout << "failbit is not set: no problem formatting or extracting.\n"
                        "Now testing std::ios_base::badbit...\n";
        }
        if (cin.bad()) {
                cout << "std::cin is set to std::ios_base::badbit: "
                        "irrecoverable stream error.\n";
        } else {
                cout << "badbit is not set.\n";
        }
        cout << "Now restoring std::cin state\n";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    cout << "Enter subject: ";
    cin >>a.course1;
    cout<<"Experiment number: ";
    cin>>a.exp1;

    cout<<"Name: "<<a.name1<<endl;
    cout<<"Section: "<<a.sec1<<endl;
    cout<<"Subject: "<<a.course1<<endl;
    cout<<"Experiment number: "<<a.exp1<<endl;

//Borrower 2
    cout << "Enter Full name: "<<endl;
    cin.getline (b.name2, 150);
    cout << "Section: "<<endl;
    cin.getline (b.sec2, 5);
    cout << "Enter subject: "<<endl;
    cin >>b.course2;
    cout<<"Experiment number: "<<endl;
    cin>>b.exp2;

    cout<<"Name: "<<b.name2<<endl;
    cout<<"Section: "<<b.sec2<<endl;
    cout<<"Subject: "<<b.course2<<endl;
    cout<<"Experiment number: "<<b.exp2<<endl;

//Borrower 3
    cout << "Enter Full name: "<<endl;
    cin.getline (c.name3, 150);
    cout << "Section: "<<endl;
    cin.getline (c.sec3, 5);
    cout << "Enter subject: "<<endl;
    cin >>c.course3;
    cout<<"Experiment number: "<<endl;
    cin>>c.exp3;

    cout<<"Name: "<<c.name3<<endl;
    cout<<"Section: "<<c.sec3<<endl;
    cout<<"Subject: "<<c.course3<<endl;
    cout<<"Experiment number: "<<c.exp3<<endl;

    return 0;
}

Topic archived. No new replies allowed.