cin.width not working

Hi. I am following along in my book. It has a program written similar to the one I have written here. The book states that cin.width should limit the user to entering the specified amount of characters, but it seems to not do anything when I try it. Here is my program.

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
// This program analyzes data to determine how the runners placed in a race.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main(){
    char name1[5], name2[5], name3[5];
    float time1, time2, time3;
    int place1, place2, place3 = 0;

    cout << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);

    cout << "Enter the name of the first runner." << endl;
    cin.width(6);  // This is supposed to set a limit on the characters entered.
    cin >> name1;
    cout << endl;

    cout << "Enter the name of the second runner." << endl;
    cin >> name2;
    cout << endl;

    cout << "Enter the name of the third runner." << endl;
    cin >> name3;
    cout << endl;

    cout << "Enter the first runner's finish time in seconds." << endl;
    cin >> time1;
    cout << endl;

    cout << "Enter the second runner's finish time in seconds." << endl;
    cin >> time2;
    cout << endl;

    cout << "Enter the third runner's finish time in seconds." << endl;
    cin >> time3;
    cout << endl;

    if (time1 > 0 && time2 > 0 && time3 > 0){
            if      (time1 == time2 == time3)
                cout << "All three have tied!" << endl;

            else if (time1 <= time2 && time1 <= time3 && time2 <= time3){
                cout << "First Place:" << name1 << endl;
                cout << "Second Place:" << name2 << endl;
                cout << "Third Place:" << name3 << endl;}

            else if (time1 <= time2 && time1 <= time3 && time3 <= time2){
                cout << "First Place:" << name1 << endl;
                cout << "Second Place:" << name3 << endl;
                cout << "Third Place:" << name2 << endl;}

            else if (time2 <= time1 && time2 <= time3 && time1 <= time3){
                cout << "First Place:" << name2 << endl;
                cout << "Second Place:" << name1 << endl;
                cout << "Third Place:" << name3 << endl;}

            else if (time2 <= time1 && time2 <= time3 && time3 <= time1){
                cout << "First Place:" << name2 << endl;
                cout << "Second Place:" << name3 << endl;
                cout << "Third Place:" << name1 << endl;}

            else if (time3 <= time1 && time3 <= time2 && time1 <= time2){
                cout << "First Place:" << name3 << endl;
                cout << "Second Place:" << name1 << endl;
                cout << "Third Place:" << name2 << endl;}

            else if (time3 <= time1 && time3 <= time2 && time2 <= time1){
                cout << "First Place:" << name3 << endl;
                cout << "Second Place:" << name2 << endl;
                cout << "Third Place:" << name1 << endl;}
    }
    else
            cout << "Invalid time entry." << endl;
    return 0;
}


Ideally the cin.width should allow the program to accept the name of the first runner without skipping the second runner because the first entry was too long.
this works fine over here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main(){
    string name1;
    string name2;
	
    cout << "Enter the name of the first runner." << endl;
    cin.width(6);  // This is supposed to set a limit on the characters entered.
    cin >> name1;
    cout << "name1:" << name1 << endl;
    cin >> name2;
    cout << "name2:" << name2 << endl;
	system("pause");

	return 0;
}


Enter the name of the first runner.
pietjeklaas
name1:pietje
name2:klaas


the data is not added until you push enter. cin reads 6 bytes and every cin after that reads ALL remainder bytes from the unput buffer. you need to set the width again if you want to read 6 bytes again.
Last edited on
Is there a simple way to make the entries not overlap? So name1 gets only 6 characters, but the rest of the characters from name1 just get dropped and I can still ask the user to input name2?

Oh, never mind. Stupid question. haha.

I don't know why the book asked for something complicated out of this when I could just fix everything in the declaration.

with just:

char name[7]

and nothing else.

... Actually that doesn't work either, and I need to sleep.

Wait! I got it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    cout << "Enter the name of the first runner." << endl;
    char name1[5];
    cin.width(5);
    cin >> name1;
    cin.ignore(99, '\n');
    cout << endl;

    cout << "Enter the name of the second runner." << endl;
    char name2[5];
    cin.width(5);
    cin >> name2;
    cin.ignore(99, '\n');
    cout << endl;

    cout << "Enter the name of the third runner." << endl;
    char name3[5];
    cin.width(5);
    cin >> name3;
    cin.ignore(99, '\n');
    cout << endl;


Though my book doesn't say anything about cin.ignore at all.
Last edited on
Can't learn everything from books..
Topic archived. No new replies allowed.