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.
Related
I have the following snippet which I'm trying to log into the terminal for debug purposes:
void DebugVector(vector<string> word_list) {
size_t word_count = word_list.size();
for (int i = 0; i < word_count; i++) {
for (int j = 0; j < word_count; j++) {
if (i == 156 && j == 156) {
fflush(stdout);
printf("We're supposed to find the word with value lares; ");
fflush(stdout);
const char *wordi = word_list[i].c_str();
const char *wordj = word_list[j].c_str();
printf("Actual values are %s and %s", wordi, wordj);
fflush(stdout);
}
}
}
}
I'm using Windows with the Windows Subsystem for Linux feature turned on and when I'm building the program with the following command
cl.exe /Zi /EHsc /nologo /Fe: C:\Users\user\Documents\VSCode_projects\test_program\main.exe C:\Users\user\Documents\VSCode_projects\test_program\main.cpp
and after I run it, the output is
We're supposed to find the word with value lares; Actual values are lares and lares
However, when I'm running it from the bash terminal in Windows, after building it with the following command:
$ g++ main.cpp -o main-linux.exe && ./main-linux.exe
The output is
and laresosed to find the word with value lares; Actual values are lares
It looks like the cursor is moved to the beginning of the line right after printing the first string. From what I understand c_str only outputs null terminated values, so I don't understand why this could happen.
I've tried creating a concatenated string, using cout instead, and I'm getting pretty much the same result. Also, the word_list has about 10,000 words in it, so I believe I'm not accessing any out of range values.
Is there anything that I can do here to make the program output the correct value?
The issue was from the difference getline() makes when compiled with g++ and cl.exe. The vector was generated via this code:
if (list_file.is_open()) {
while (getline(list_file, line)) {
word_list.push_back(line);
}
}
getline() deals with new line in windows differently which is not very surprising. g++ will read the word including the carriage return (\r) value while cl.exe won't. This results in the print issue.
you can access vector elements with subscripts like array
because its object and it has address storage values
YOU CAN ALSO use .at() method of vector
https://www.geeksforgeeks.org/vector-in-cpp-stl/
here in the element access method
and c_str() here works fine! no issue with that...
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.
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.
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.
In the following program, I am reading a 6-length string dd and storing its ascii code in an integer array ipc of size 3x2. The problem is that the values stored in ipc are wrong and they change themselves later when I reprint them. I am surprised how can there be such a clear problem with such a simple code. (I am using Code::Blocks 10.05 on Win7 x64)
#include<iostream>
using namespace std;
int main()
{ char dd[5];
int ipc[2][1];
cin.get(dd,6);
for(int i=0;i<3;i++)
{ for(int j=0;j<2;j++)
{ ipc[i][j]=int(dd[j+2*i]);
cout<<ipc[i][j]<<endl;
}
}
cout<<"------"<<endl;
for(int i=0;i<3;i++)
{ for(int j=0;j<2;j++)
{ cout<<ipc[i][j]<<endl; }
}
}
If input given is 123456, the output is:
49
50
51
52
53
2
------
49
51
51
53
53
2
Any sort of help will be very much appreciated. Thank you.
The array declaration is incorrect and the code is going out-of-bounds on the array causing undefined behaviour. Declaration should be changed from:
int ipc[2][1];
to:
int ipc[3][2];
Additionally, cin.get() will read count - 1 characters, so:
cin.get(dd, 6);
will only read 5 characters, not 6. If the user enters 123456 only 12345 will be read. The cin.get() will also append a null character, (as commented by tinman). To correct increase the size of dd and the number of characters to be read:
char buf[7];
cin.get(buf, 7);