Skip to main content
Raspberry Pi

Raspberry Pi Using C programming to enjoy rote revision

By December 3, 2013September 12th, 2022No Comments

In this lesson we talk of my passion for “joined up learning” where one subject touches another and education is seen as holistic. Quite simply we are learning numbers by rote but making it fun and using some useful C programming skills giving purpose to our programming and having learners help other learners. In the example I am using Romanian but of course any language will do and the same would apply to history or any other subject

The finished product

It is easiest to explain our project by starting at the finished product. This way you can gain the idea of what should happen. The program runs and the user is asked to add the year, month and day of their or another person’s birthday. The result from the program then may look like this:

As we are prompted both in English and Romanian as to what is required. As the program continues to run then we can have encouragement of successful words entered.

GPL License

This code, written by me, is licensed under the Free Software Foundation GPL V2 License. Other may freely use the code and share their development. If you have ever used the internet for ideas on how to code something, to me it would immoral, not to help others. If you own the code consider sharing it under the GPL license or other license supported or endorsed by the Free Software Foundation. My code is all distributed under the GPL v2 License.

The source code for quiz

/*
To help kids with their foreign languages, numbers 1 -10
Copyright (C) 2013 theurbanpenguin (Peterborough) Ltd Andrew Mallett
andrew.mallett@theurbanpenguin.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void eat_extra(void) {
    int ch;
    while ((ch = getchar()) != 'n') {
    }
}
main () {
 printf("Welcome you will see 10 numbers enter them in Romaniann");
 printf("O sa vezi 10 numbere...tradule sau scriele in Romanesten");
 int i;
 char one[3];
 printf("one: In Romainian is... ");
 fgets(one,4, stdin);
 for(i = 0; one[i]; i++){
  one[i] = tolower(one[i]);
 }
 printf("Your answer: %sn",one);
 if ( strcmp (one, "unu") == 0 ) {
    printf("Foarte bine!n");
    }
 eat_extra();
 char two[3];
 printf("two: In Romainian is... ");
 fgets(two,4, stdin);
 for(i = 0; two[i]; i++){
  two[i] = tolower(two[i]);
 }
 printf("Your answer: %sn",two);
 if ( strcmp (two, "doi") == 0 ) {
    printf("Excelentn");
    }
 eat_extra();
 char three[4];
 printf("three: In Romainian is... ");
 fgets(three,5, stdin);
 for(i = 0; three[i]; i++){
  three[i] = tolower(three[i]);
 }
 printf("Your answer: %sn",three);
 if ( strcmp (three, "trei") == 0 ) {
    printf("Foarte binen");
    }
 eat_extra();
 char four[5];
 printf("Four: In Romainian is... ");
 fgets(four,6, stdin);
 for(i = 0; four[i]; i++){
  four[i] = tolower(four[i]);
 }
 printf("Your answer: %sn",four);
 if ( strcmp (four, "patru") == 0 ) {
    printf("Excelentn");
    }
 eat_extra();
 char five[5];
 printf("Five: In Romainian is... ");
 fgets(five,6, stdin);
 for(i = 0; five[i]; i++){
  five[i] = tolower(five[i]);
 }
 printf("Your answer: %sn",five);
 if ( strcmp (five, "cinci") == 0 ) {
    printf("Foarte binen");
    }
 eat_extra();
 char six[4];
 printf("Six: In Romainian is... ");
 fgets(six,5, stdin);
 for(i = 0; six[i]; i++){
  six[i] = tolower(six[i]);
 }
 printf("Your answer: %sn",six);
 if ( strcmp (six, "sase") == 0 ) {
    printf("Excelentn");
    }
 eat_extra();
 char seven[5];
 printf("Seven: In Romainian is... ");
 fgets(seven,6, stdin);
 for(i = 0; seven[i]; i++){
  seven[i] = tolower(seven[i]);
 }
 printf("Your answer: %sn",seven);
 if ( strcmp (seven, "sapte") == 0 ) {
    printf("Excelentn");
    }
 eat_extra();
 char eight[3];
 printf("Eight: In Romainian is... ");
 fgets(eight,4, stdin);
 for(i = 0; eight[i]; i++){
  eight[i] = tolower(eight[i]);
 }
 printf("Your answer: %sn",eight);
 if ( strcmp (eight, "opt") == 0 ) {
    printf("Foarte binen");
    }
 eat_extra();
 char nine[4];
 printf("Nine: In Romainian is... ");
 fgets(nine,5, stdin);
 for(i = 0; nine[i]; i++){
  nine[i] = tolower(nine[i]);
 }
 printf("Your answer: %sn",nine);
 if ( strcmp (nine, "noua") == 0 ) {
    printf("Excelentn");
    }
 eat_extra();
 char ten[4];
 printf("Ten: In Romainian is... ");
 fgets(ten,5, stdin);
 for(i = 0; ten[i]; i++){
  ten[i] = tolower(ten[i]);
 }
 printf("Your answer: %sn",ten);
 if ( strcmp (ten, "zece") == 0 ) {
    printf("Foarte binen");
    }
 printf("Am terminat!n");
}

 

eat_extra Function

The eat_extra function is use to remove additional characters the user may type. The answers all go into what is known as standard input, when we read an answer we only read enough characters that are required for the answer. If we do not clean up the additional characters they will be part of the next answer and so on.

void eat_extra() {
int ch;
while ((ch = getchar()) != 'n') {
  }
}

 

This implements a simple while loop to remove all but the new line character and is called from our main function after each answer has been read into its own variable for assessment.

Function main

If we take one question from the main method we can gain the idea of what is happening:

main () {
 printf("Welcome you will see 10 numbers enter them in Romaniann");
 printf("O sa vezi 10 numbere...tradule sau scriele in Romanesten");
 int i;
 char one[3];
 printf("one: In Romainian is... ");
 fgets(one,4, stdin);
 for(i = 0; one[i]; i++){
  one[i] = tolower(one[i]);
 }
 printf("Your answer: %sn",one);
 if ( strcmp (one, "unu") == 0 ) {
    printf("Foarte bine!n");
    }
 eat_extra();
 char two[3];

 

 

Question one starts with the definition of an array:

char one[3];

The array will have 4 elements:

  • one[0]
  • one[1]
  • one[2]
  • one[3]

The answer should be unu needing three spaces to hold each character but a character string like this needs a space for a system character. We need to set up a char array like this for each question.

We then print the question to the screen:

printf(“one: In Romainian is… “);

 

Now we have to read in the answer. We use fgets to do this as we can specify how many characters to read in so we do not overfill the array. In this question we read in 4 characters to the array identified as one. The input is read from the file stdin, the keyboard.

fgets(one,4, stdin);

 

The for loop iterates though each element of the array to convert the user input to lower case for ease of testing.

for(i = 0; one[i]; i++){ one[i] = tolower(one[i]); }

 

We then display the answer with printf and test to see if it is correct. we cannot compare an array directly with the answer, so we use strcmp from the string.h header.

printf("Your answer: %sn",one); if ( strcmp (one, "unu") == 0 ) {  printf("Foarte bine!n")  }

 

Then we use the eat_extra function to remove additional input the user may have entered and we are ready to ask the next question.

Watch the video tutorial on YouTube: