Std :: cin works for a small number of lines, but not larger

I am writing a piece of program that takes a list of user-supplied filenames and stores them in a vector. So far, testing with a small number of filenames (I did before 11) works fine and the program continues, but trying to import 70 or more files results in the program not continuing.

Here is my code:

//******************************* Get List of Filenames ********************************
cin.ignore(1000, '\n');
cout << "Please paste all of the Scans files here: ";
vector<string> filenameList;
string filenameString;

//can change the quit value to anything you want
while (cin >> filenameString && filenameString != "b") 
{
    filenameList.push_back(filenameString);
}

cout << "The filenames entered are:\n";
for (int i = 0; i < filenameList.size(); i++)
{
    cout << filenameList[i] << endl;
}
cout << endl << filenameList.size();

      

The error is that the program hangs and looks like it is still waiting for input, but does nothing, even after entering the exit code "b".

We copy and paste the filenames directly into the console and then use to cin

place them into a vector.

Below is an example of a list of filenames we are using:

MSS279_S10_B112_F8_p001.jpg
MSS279_S10_B112_F8_p002.jpg
MSS279_S10_B112_F8_p003.jpg
MSS279_S10_B112_F8_p004.jpg
MSS279_S10_B112_F8_p005.jpg
MSS279_S10_B112_F8_p006.jpg
MSS279_S10_B112_F8_p007.jpg
MSS279_S10_B112_F8_p008.jpg
MSS279_S10_B112_F8_p009.jpg
MSS279_S10_B112_F8_p010.jpg
MSS279_S10_B112_F8_p011.jpg
MSS279_S10_B112_F8_p012.jpg
MSS279_S10_B112_F8_p013.jpg
MSS279_S10_B112_F8_p014.jpg
MSS279_S10_B112_F8_p015.jpg
MSS279_S10_B112_F8_p016.jpg
MSS279_S10_B112_F8_p017.jpg
MSS279_S10_B112_F8_p018.jpg
MSS279_S10_B112_F8_p019.jpg
MSS279_S10_B112_F8_p020.jpg
MSS279_S10_B112_F8_p021.jpg
MSS279_S10_B112_F8_p022.jpg
MSS279_S10_B112_F8_p023.jpg
MSS279_S10_B112_F8_p024.jpg
MSS279_S10_B112_F8_p025.jpg
MSS279_S10_B112_F8_p026.jpg
MSS279_S10_B112_F8_p027.jpg
MSS279_S10_B112_F8_p028.jpg
MSS279_S10_B112_F8_p029.jpg
MSS279_S10_B112_F8_p030.jpg
MSS279_S10_B112_F8_p031.jpg
MSS279_S10_B112_F8_p032.jpg
MSS279_S10_B112_F8_p033.jpg
MSS279_S10_B112_F8_p034.jpg
MSS279_S10_B112_F8_p035.jpg
MSS279_S10_B112_F8_p036.jpg
MSS279_S10_B112_F8_p037.jpg
MSS279_S10_B112_F8_p038.jpg
MSS279_S10_B112_F8_p039.jpg
MSS279_S10_B112_F8_p040.jpg
MSS279_S10_B112_F8_p041.jpg
MSS279_S10_B112_F8_p042.jpg
MSS279_S10_B112_F8_p043.jpg
MSS279_S10_B112_F8_p044.jpg
MSS279_S10_B112_F8_p045.jpg
MSS279_S10_B112_F8_p046.jpg
MSS279_S10_B112_F8_p047.jpg
MSS279_S10_B112_F8_p048.jpg
MSS279_S10_B112_F8_p049.jpg
MSS279_S10_B112_F8_p050.jpg
MSS279_S10_B112_F8_p051.jpg
MSS279_S10_B112_F8_p052.jpg
MSS279_S10_B112_F8_p053.jpg
MSS279_S10_B112_F8_p054.jpg
MSS279_S10_B112_F8_p055.jpg
MSS279_S10_B112_F8_p056.jpg
MSS279_S10_B112_F8_p057.jpg
MSS279_S10_B112_F8_p058.jpg
MSS279_S10_B112_F8_p059.jpg
MSS279_S10_B112_F8_p060.jpg
MSS279_S10_B112_F8_p061.jpg
MSS279_S10_B112_F8_p062.jpg
MSS279_S10_B112_F8_p063.jpg
MSS279_S10_B112_F8_p064.jpg
MSS279_S10_B112_F8_p065.jpg
MSS279_S10_B112_F8_p066.jpg
MSS279_S10_B112_F8_p067.jpg
MSS279_S10_B112_F8_p068.jpg
MSS279_S10_B112_F8_p069.jpg
MSS279_S10_B112_F8_p070.jpg
MSS279_S10_B112_F8_p071.jpg
MSS279_S10_B112_F8_p072.jpg
MSS279_S10_B112_F8_p073.jpg
MSS279_S10_B112_F8_p074.jpg
MSS279_S10_B112_F8_p075.jpg
MSS279_S10_B112_F8_p076.jpg

      

+3


source to share


2 answers


I've never figured out why, but I think there is a limit on the number of characters that Xcode can import for whatever reason (somewhere around 1000 characters).



I switched to Visual Studio on PC and everything worked out great ...

0


source


Try to split cin >> filenameString && filenameString != "b"

into two lines of code. Maybe put the second part inside a loop in an expression if

like this: if (filenameString != "b") break;

I have a suspicion that assigning filenameString

and then comparing it on the same line may not always behave the way you think.



0


source







All Articles