sync() and flush(), usages?

This is part of a bigger program, but essentially, what's happening is that "stringB" isn't being allowed to be written properly. After I type in 10 characters it tells me that the length isn't right. If I hit enter 9 times, the prompt to retry comes up, and it will accept the 10 characters. This is after a lot of cout/cin back and forth. I started using cin.sync() and it seemed to alleviate some issues, but I'm not really sure if buffers are the root cause, or if I'm just maybe losing my mind. Code follows:

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
#include <iostream>

int main() {

char stringA[12], stringB[12];
bool filevalid;

filevalid == false;
int interval=0;
do {
   cout << "Enter string (length of 10): ";
   cin.read(stringA, 10);
   cin.sync();
   cout << "Testing string length..." << endl;
   for(i=0; i<10; i++) {
      if(stringA[i] == '\0' || stringA[i] == '\n') {
         cout << "Length not correct...\nPress ENTER to retry";
         cin.ignore();
         cin.sync();
         filevalid = false;
         interval++;
         break;
      }
      else {
         filevalid = true;
      }
   }
} while(filevalid==false && interval <3);
if(interval == 3) {
	cout << "Failed to provide proper key\n";
	cout << "Press ENTER to exit...";
	cin.ignore();
   cin.sync();
   return 0;
}
filevalid=false;
interval=0;
do {
   cout << "Enter second string (length of 10): ";
   cin.read(stringB, 10);
   cin.sync();
   cout << "Testing string length..." << endl;
   for(i=0; i<10; i++) {
      if(stringB[i] == '\0' || stringB[i] == '\n') {
         cout << "Length not correct...\nPress ENTER to retry";
         cin.ignore();
         cin.sync();
         filevalid = false;
         interval++;
         break;
      }
      else {
         filevalid = true;
      }
   }
} while(filevalid==false && interval <3);
if(interval == 3) {
    cout << "Failed to provide proper string\n";
    cout << "Press ENTER to exit...";
    cin.ignore();
    cin.sync();
    return 0;
}

cout << "Strings are good...\n";
cout << "Press ENTER to exit...";
cin.ignore();
cin.sync();
return 0;
}
If you're relying on the behavior of cin.sync(), you're relying on implementation defined behavior. In some environments cin.sync() does nothing at all.

cin.read is something you don't see very often. read and write is primarily meant to be used with binary files, not text streams.

Here is an alternate way to do what you're doing:

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
#include <iostream>
#include <string>
#include <limits>
using namespace std ;

string getStringOfExactLength( unsigned length, unsigned max_attempts)
{
    string input ;
    unsigned attempts = 0 ;
 
    bool inputNotSuccessful = true ;

    while ( attempts++ < max_attempts && inputNotSuccessful )
    {
        cout << "Enter " << length<< " characters (exactly): " ;
        getline(cin, input) ;

        if ( input.length() != length )
            cout << "ERROR:  You must enter exactly " << length << " characters.\n" ;
        else
            inputNotSuccessful = false ;
    }

    return inputNotSuccessful ? string() : input ;
}

int main() 
{    
    const unsigned stringLength = 10 ;
    const unsigned maxTries = 3 ;

    const char* errorNoKey =
        "Failed to provide proper input.\nEnter to exit..." ;

    cout << "Enter first string!\n" ;
    string A = getStringOfExactLength(stringLength, maxTries) ;

    if ( !A.length() )
    {
        cout << errorNoKey ;
        cin.ignore(numeric_limits<streamsize>::max(), '\n') ;
        return 0  ;
    }

    cout << "\nEnter second string!\n" ;
    string B = getStringOfExactLength(stringLength, maxTries) ;

    if ( !B.length() )
    {
        cout << errorNoKey ;
        cin.ignore(numeric_limits<streamsize>::max(), '\n') ;
        return 0 ;
    }

    cout << "\nEntered: \"" << A << "\" and \"" << B << "\"\n" ;
    cout << "Strings are good!\nEnter to exit..." ;
    cin.ignore(numeric_limits<streamsize>::max(), '\n') ;
}
Last edited on
Very interesting! Thank you for your informative response! If cin.read() is for binary, how would that be implemented? Unless cin is actually reading the binary value of the keys typed. I hadn't thought of incrementing the 'tries' in the while condition and I need to read a bit more on strings. Thank You Again!
Last edited on
Topic archived. No new replies allowed.