C++ std::cin is stuck at 80 lines of input - c++

here is the code that I have
int main()
{
int total = 0;
int count = 0;
std::cin >> total;
int arr[4] = {0,0,0,0};
while(count < total)
{
std::cin>>arr[0]>>arr[1]>>arr[2]>>arr[3];
count++;
std::cout<<count<<std::endl;
}
return 0;
}
so the first line of input tells how many lines I need to read after..and each line has 4 numbers separated by space. Whenever the number of lines exceed 80(e.g 100), then the while loop gets stuck. I have no idea what causes the problem and I have tried a couple things like cin.clear() but they just didnt work....
Edit: std::cin stops reading after 80 lines of input with format like 10 20 210 10
Xcode with LLVM didn't work...However g++ using terminal works.... http://melpon.org/wandbox/permlink/UXAMgM4ldn2K2NgU
here is the code that works on my terminal with g++ but not my xcode...

It's the output that's getting stuck. Unless the output of the count is being read by something and consumed, eventually the output buffer will get full and the cout line will block.

Related

Why does some code work HackerRank but not Xcode

The code below works fine on HackerRank, but not on Xcode 11. I suspect the while loops are causing the issue. Is there something I am missing?
This is not the first time code works on HackerRank, but not Xcode. I usually work on Xcode before submitting it to learn. I would like to make sure the code I write on Xcode will work on any compiler.
I would appreciate some insight.
int main(){
int n, i=0;
cin >> n;
int * A = new int[n];
while(cin >> A[i++]);
while(cout << A[--n] << ':' && n);
delete[]A;
return 0;
}
The question is:
User will need to enter the size of an array and the elements of the array on the same line separate by a space. The program will return the array in reverse order.
sample input: 4 (enter)
sample input: 1 2 3 4 (enter)
sample output: 4 3 2 1
When I try running on Xcode it seems to loop forever after I manually input the values. The cursor just blinks. I have to force quit. I do not get any errors. See image in the link below.
Xcode debug area image
while(cin >> A[i++]);
Will wait forever for the next input value until it encounters the end of the input or an invalid input value.
This will be working on hackerrank because they will be piping in a file with the input values so cin will report eof when you try to read too many values.
As you know exactly how many inputs you expect the easiest (and safest to prevent out of bounds array access) solution is to add s check for how many inputs you have read:
while(i < n && cin >> A[i++]);

Repeating code for N cases error [duplicate]

I have the following code:
std::vector<std::string> final_output;
std::string input;
int tries = 0;
std::cin >> tries;
int counter = 0;
while(counter < tries) {
std::getline(std::cin, input);
final_output.push_back(input);
++counter;
}
Given the input:
3
Here Goes
Here Goes 2
The output is:
<blank line>
Here Goes
Here Goes 2
Weirdly, it seems to enter a blank line as input for the first time it runs.
However, if I have the code as:
int tries = 3; // explicitly specifying the number of tries
int counter = 0;
while(counter < tries) {}
It works as expected. Why is the std::cin >> tries causing the code to fail?
I have tested it with VC++ 2010 and g++ 4.4.3
When you enter the number for tries, you hit the return key. After you read tries, the carriage return from hitting the return key is still sitting in the input buffer. That carriage return will normally be translated to a new-line character. Your next call to getline reads everything in the input buffer up to the next new-line. Since the first character is a new-line, it reads that as a line of zero length (i.e., zero characters before the new-line).
The newline of the first entry is still in the input buffer.
You can call std::cin.ignore(); just after reading tries from cin.
This way the newline gets discarded.
I found a good link that explains plenty of things regarding the use of I/O:
http://www.augustcouncil.com/~tgibson/tutorial/iotips.html
You have nothing to absorb the '\n' from the first line in your standalone std::cin >> tries.

Read from text file and store each value in separate arrays in c++

I am trying to read the following contents:
rima doha 44881304 20 30 10 10 20 10102 10102
andrew ny 123456 12 12 13 14 15 01020 03040
and store them in separate arrays, edit them, then store again into the same file.
Here is my attempted code:
ifstream infile;
infile.open("D:\\customers.txt");
string names[100];
string addresses[100];
int tn[100];
int numCalls[100];
int trunkCalls[100];
int numMobCalls[100];
int isdnNum[100];
int dueD[100];
int paymentD[100];
int numOfPpl = 0;
int numOfPpl = 0;
for(int i=0; i<100; i++){
infile >> names[i] >> addresses[i]>>tn[i]>>numCalls[i]>>trunkCalls[i]>> numMobCalls[i]>> isdnNum[i]>>dueD[i]>>paymentD[i];
numOfPpl++;
}
//Code where some values were edited
ofstream outfile("D:\\customers.txt");
for(int i=0; i<numOfPpl; i++)
{
outfile<<names[i] << "\t" <<addresses[i] << "\t" <<tn[i]<<"\t" <<numCalls[i]<<"\t"
<<trunkCalls[i]<<"\t"<<numMobCalls[i]<<"\t"<<numMobCalls[i]<<"\t"<<isdnNum[i]<<"\t"<<dueD[i]<<"\t"<<paymentD[i]<<endl;
}
outfile.close();
infile.close();
Issue is that the first two lines are stored correctly, but then there are random values in the file. How do I fix this?
There's a couple things wrong with your code.
First, you declare numOfPpl twice. Get rid of the 2nd count.
Secondly, you have 9 input categories (names, addresses, etc.), but your text file has 10 per line. This will throw the entire program off.
The third issue is are you going to have exactly 100 lines in the customers.txt? If not, you should utilize a command to peek to the next line to make sure if there's another line below. Using a while or do/while loop would probably be better if there aren't 100 lines in your text file. Something like
while(infile){
// retrieve your data
infile.peek();
}
or utilizing the for loop
for(int i = 0; infile; i++){
// retrieve your data
infile.peek();
}
would probably be a better loop. Again, if you're not going to have 100 lines, the for loop will give you the null output because there isn't any data being put into the array elements. I believe that should correct your issues.
Another thing you should watch out for is having an extra space at the end of a line. That'll throw your program output off as well.

cannot read the text file properly anymore

Alright so basically this is my program that reads a text file and place it inside of an array, in the end I printed out everything in that array. The program run fine and yielded the correct result few days. However, it just stopped working today. For instance the text file is
88
687
472
671
But upon the completion of the program the output is 0 1073741824 0 1073741824. I dont know what is going on and the only time I made some change to the bash was ulimit -s unlimited. Any idea?
int main(int argc, char *argv[])
{
ifstream file(argv[1]);
int placeholder;
int size = pow(2,atoi(argv[2]));
int array[size];
int index = 0;
while (file >> placeholder)
{
array[index]=placeholder;
index++;
}
for(int i = 0; i<size; i++)
{
cout<<array[i]<<endl;
}
return 0;
}
Are you sure your text-file is readable by the program? If the input file does not exist, the program will still try to print argv[2]**2 entries from array which does not contain any elements! The program ends up dumping garbage values.
I am also not sure why you do the pow call - why not get the number of elements from argv[2]?
Also, you use some c functions (atoi) when you could use C++ stringstream to do the conversion.
When I run your code with the input you provide, like this: ./a.out file.txt 2, it prints the 4 numbers as expected. When I do this instead: ./a.out does_not_exist.txt 2, it prints 4 garbage values and dumps them to the screen.

C++ program doesn't completely read huge input. Why?

I want to solve a programming contest task (C++ with XCode) which has a relatively big input (300 lines). When copying a test input into the console, it doesn't read it all.
So I have written a minimalistic test program to simply read in 300 lines:
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
ios_base::sync_with_stdio(false);
string xxx;
for(int i = 0; i < 300; i++)
cin >> xxx;
return 0;
}
If I execute it and copy 340 lines with "aaaaaaaaaa" into the console, it doesn't end. If I stop it with the debugger, it says i = 92. If I then continue, it quits.
But when I copy pieces of 50 lines into the console, it quits immeadiately as it should...
Can anyone help me with this?
PS: I inserted the 'ios_base::sync_with_stdio(false);', because I read that this would spped the input up.
I want to solve a programming contest task (C++ with XCode) which has a relatively big input (300 lines). When copying a test input into the console, it doesn't read it all
This is possible when you have more than one word per line because cin >> xx will read words as opposed to lines.
You would need to use the getline method to actually read lines.
while (getline(cin, xxx));
If I execute it and copy 340 lines with "aaaaaaaaaa" into the console, it doesn't end. If I stop it with the debugger, it says i = 92.
Yes, this is exactly the same symptom. Even if you had one word per line, you would reach only 300 lines, never 340.
Here is the whole code I would write for your reference:
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
ios_base::sync_with_stdio(false);
string xxx;
while (getline(cin, xxx))
cout << xxx << endl;
return 0;
}
cin >> string inputs words, not lines. Try:
string xxx;
while (getline(cin, xxx)) {
;
}
to input a full line at a time into xxx.
I couldn't solve the problem with X-Code but it works with Qt. So I use Qt now.