Skip to content

Fixed bug in readArray #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions programming-I/12-deleteRepeats/deleteRepeats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,25 @@ void readArray(char array[], int &size) {
cout << "\nWhat is the size: ";
cin >> size;

while (size > SIZE_LIMIT || size < 0) {
cerr << "\nInvalid size. Please enter a size between 0 and " << SIZE_LIMIT;
while (size > SIZE_LIMIT || size < 0 || !cin ) { //!cin checks for a failed state, or in other words, when a non-int type is given as 'size'.
cin.clear(); // restores cin back to normal state
cin.ignore(1024, '\n'); // this clears out the cin buffer.

cout << "\nInvalid size. Please enter a size between 0 and " << SIZE_LIMIT;

cout << "\n\nWhat is the size: ";
cin >> size;
}


cout << "Enter the array (one character at a time): " << endl;

for (int i = 0; i < size; i++)
for (int i = 0; i < size; i++){
cin >> array[i];
}
}


//*****************************************************************************************************

void deleteRepeats(char array[], int &size) {
Expand Down Expand Up @@ -196,4 +202,4 @@ Updated array: 1 2 3

Repeat? (y/n): n

*/
*/