Can't get buffer to clear

I cannot get the buffer to clear when I am trying to run 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

using namespace std;
using std::ifstream;
using std::ofstream;
using std::ios;

void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);
void flush(istream & is);

const char FileName[] = "c:/TestAddress.txt";
string database;
ifstream myAddress(FileName);

    string name = " ";
    string address = " ";
    string street= " ";
    string city = " ";
    string state = " ";
    string zipCode = " ";
    int record = 0;

int main () 
{
        
    menu();
    return 0;
} //end main

void menu(void) 
{
    //allow user to choose to append records, display records or exit the program
    char userChoice = ' ';

    //Display Menu
    cout << "\n\n(A)ppend record, (S)how Record, (E)xit:"; 
        cin >> userChoice; 
        //Users Choice

    switch (userChoice)
    {
        case 'a':
        case 'A'://Append Record
            myAddress.open ("TestAddress.txt", ios::app);
            if (myAddress.is_open()) 
                {writeData(); }
        break;

        case 's':
        case 'S'://Show record
            readData();
            
        break;

        case 'e':
        case 'E'://Exit
            myAddress.close();
        break;

        default:
            cout<<"Invalid choice"<<endl;
            cout << endl << endl << endl;
            break;
            
    }

    return;
    
}//end menu

void flush(istream & is) 
{ // clear the keyboard buffer 
    is.clear(); char nextChar; 
    while( (nextChar = is.get()) != '\n' && nextChar != EOF)
    { }is.clear(); 
}

void writeData(void) //Write the Address Info to a file
{    
    
    char answer = ' ';

    
    cout<< " Enter another record? (Y or N)"<<endl;
    cin>>answer;
	myAddress>>database;
    

    
    //entering loop
    while (answer == 'Y' || answer == 'y')
	{
		 

        cout << "Enter name\n";
        getline(cin, name);
        cout<< "Enter Street\n";
        getline(cin, street);
        cout<< "Enter City\n";
        getline(cin,city);
        cout<< "Enter State\n";
        getline(cin, state);
        cout<< "Enter Zip Code\n";
        getline(cin, zipCode);
        cout<<name<<", "<< street<<" ,"<< city<<", "<< state<<" ,"<< zipCode<<endl;
        record++;
        cout<< " Enter another record? (Y/N)"<<endl;
        cin>>answer;}
        
		while (answer == 'n' || answer == 'N')
        {return menu();}

    
    
}//end write data

void readData(void)
{     
    cout<<"Reading a File press y"<<endl;

    //read data from a file
    getline (myAddress, database, '\n');
    myAddress>>database;
    //use the split function to break a deliminated line of text into fields

    cout<<"Record #"<<record<<endl;
    string *theField = split(database, ',');
    cout<<"Name"<<"....."<<name<<theField[0]<<endl;
    cout<<"Street"<<"..."<<street<<theField[1]<<endl;
    cout<<"City"<<"....."<<city<<theField[2]<<endl;
    cout<<"State"<<"...."<<state<<theField[3]<<endl;
    cout<<"Zip Code"<<"."<<zipCode<<theField[4]<<endl;

    
}//end read data

string * split(string theLine, char theDeliminator){
    //Break theline into fields and save the fields to an array.
    //Each field will occupy one element in a character array.
    //theLine is a string with fields separated with theDeliminator character.
    //Assumes the last field in the string is terminated with a newline.
    //Useage: string *theFields = split(lineBuffer, ',');

    //determine how many splits there will be so we can size our array
    int splitCount = 0;
    for(int i = 0; i < theLine.size(); i++){
    if (theLine[i] == theDeliminator)
    splitCount++;
    
}

splitCount++; //add one more to the count because there is not an ending comma 
    //create an array to hold the fields
    string* theFieldArray;
    theFieldArray = new string[splitCount];
    //split the string into seperate fields
    string theField = "";
    int commaCount = 0;

    for(int i = 0; i < theLine.size(); i++){ //read each character and look for the deliminator
    if (theLine[i] != theDeliminator) {
    theField += theLine[i]; //build the field
    }
    else { //the deliminator was hit so save to the field to the array
    theFieldArray[commaCount] = theField; //save the field to the array
    theField = "";
    commaCount++;
    }
    }
    theFieldArray[commaCount] = theField; //the last field is not marked with a comma...

    return theFieldArray;
} //end split 

The problem is that when I enter the character for make another record in the Append function it takes the Y character and uses it as the name when the user is prompted to enter a name and then goes straight to the Enter street field. How do I clear the cin buffer of the junk character? Thanks for any help
Last edited on
You can use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); which will take the \n character from the stream, and any characters that come before the limit of the input length. You'll need to #include <limits>...
Thanks chrisname that took care of the problem. I am only a beginner at C++ and probably would have never figured that out.
Now when I try to enter other records it is doing the same thing. How would I correc this?
Put chrisname's solution after line 114 but before the curly brace that shouldn't be on that line
Ok that worked but now it wont let me stop entering records when I choose N. Any suggestions.
Post the current code; please :)

By the way, I'm a complete beginner too. Welcome to the club :)
We don't have cookies, by the way. There was a syntax error during cook-time and we didn't have time to fix it.

I usually use std::cin.ignore(); without the whole numeric_limits thing; but it's a better (although somewhat lengthier) way of doing it. So that's how I do it.
I worked it out chrisname so no need to post a solution that someone can copy and past after all my hard work but thanks for the help
Topic archived. No new replies allowed.