Still going through some more of the C
tutorial. I built a simple guessing game alongside the tutorial and ran into an old friend / enemy called buffers. The tutorial used a while loop
to keep the user guessing for a number until they got it right. There was a simple text prompt and then the user inputs a number. The issue was that when I wrote, compiled, and ran my code there was no text prompt. With a quick search I found the likely cause for my problem. It was my old friend buffers. Once I guessed correctly and broke out of the while loop
did all the text appear. I still don’t fully understand buffers but I remember enough to somewhat understand what is going on.
TLDR;
Okay, so here are the highlights of what I did:
- Started on the “Technical Questions” section of the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. I decided to get a quick intro to the
C
andJava
programming languages before I jump into building the core data structures and algorithms that will be covered in most all technical interviews. I continued with theC
for beginners video on YouTube. Got stuck when my guessing game program failed to print the text prompt for the user to know what to do. Turns out that I needed to clear thestdout
buffer on each iteration of the loop to ensure that the text prompt actually gets displayed before each guess rather than all at once after the user finally guesses correctly. I still need to look into buffers more but for now I will have to just put a pin in it. Here is the Question from StackOverlow though, if you are interested in it.
Guessing Game Code with Buffer Clearing
#include <stdio.h>
#include <stdlib.h>
int main(){
int secretNumber = 5;
int guess;
while(guess != secretNumber){
printf("Guess a number: ");
fflush(stdout);
scanf("%i", &guess);
}
printf("You Win!!!");
return 0;
}
Conclusion
That’s all for today. This is my sixth round of the “#100daysofcode” challenge. I will be continuing my work from round five into round six. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in term derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.
I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.