r/C_Homework • • Mar 22 '17
Need help storing this input
Hi, I'm writing a program where the user inputs from stdin something along the lines of following example:
100001
100010
100011
So essentially it is an array of binary numbers and what I have to do is scan along each of the lines so 100001 and also along each of the columns once I have all user input so in the above example the first column would be 111 (so kind of like a 2D array).
The problem I'm having is actually storing these user inputs. In particular, I tried storing them in a 2D array and everything was working fine until the user puts in something like 0000100. Of course, because I read the user input as an int, it removed the zeroes at the front but that is quite important in my context. I tried attaching them back but nothing worked so far.
So yeah, I was just wondering if anyone have an advice about what I could try/do? Thanks in advance.
EDIT: I just slowly added the relevant pieces of code down below because my code got kind of messy towards the end.
// How I get the user input
while(scanf("%s",input) == 1){
// There are other things in the loop but I have tried so far is down below
if(input[0] = '0'){
// Make relevant square in 2D array be '0'
}
}This is essentially the jist of it. Not sure if I should include more code but I really don't want to post a lot of code cause I feel like the main issue is more with the algorithm rather than the coding up of it.
1
u/gatesplusplus Mar 23 '17
The most likely cause is that either when you check the values your checking and array index that is out of bounds, which will just be whatever happens to be in that memory location at the time. The other possibility is that your not assigning values to your array correctly, and your actually WRITING outside of your array. Which is a dangerous operation. Check to make sure your in bounds always.
1
u/gatesplusplus Mar 23 '17
You could initially store the array as a string. What do you need to do with the array when you have it?