Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Lab 05 – if and switch

In previous weeks we’ve used getchar() to read in user input, this is fine is we only want a single character but what happens if we want to read in a full string or a bigger number than a single digit ?

For this we can use the function scanf, this will read in the user’s input, terminated with an enter.

For example :

#define _CRT_SECURE_NO_WARNINGS

#include 

main()

{

char name[20];

printf("What is your name ? \r\n");

scanf("%s", name);

printf("Hello %s \r\n", name);

getchar();

getchar();

}

In the example the scanf function reads in a string and applies it to the char array (name).  What about numbers ?

Example

#define _CRT_SECURE_NO_WARNINGS

#include 

main()

{

int a, b;

printf("Give me a number ? \r\n");

scanf("%d", &a);

b = a * 2;

printf("%d multiplied by 2 is %d \r\n", a,b);

getchar();

getchar();

}

There are a couple of things to notice in these examples.  First scanf works in a similar way to printf, we have to tell it what type of input it is expecting (strings, integers etc.) and the switches for scanf are the same as printf (%s for string, %d for integer, %f for float etc.).

The second thing to notice is that is the second example when scanf puts the value into a variable we tell it to use &a not a.  This is because scanf needs to assign the value to a pointer to the memory location where the variable is stored not to the variable itself.  &a is a pointer to the memory location.  What about the first example ?  Here we used name and not &name, this is because in C arrays are already pointers so name and &name are the same thing (we could use &name in the first example and it would still work, using a instead of &a in the second example will cause an error).  This will make more sense in a few weeks when we look at pointers but for now we just need to know that in order to store basic variables we have to use the & form of the variable name.

One word of warning scanf is not a safe function.  In the first example we read the user input into a 20 character array but if the user was to put in more than 20 characters C would still try to store the input into the array and end up overwriting the memory locations immediately after the array.  This is a known security exploit attack known as buffer overflow, in order to guard against this we would normally check that incoming input was smaller or the same size as the area of memory assigned to hold it and reject it otherwise.

Exercise 1.

People are said to be of a particular generation depending on when they were born, there is some debate about the exact border years but generally accepted classification are :

1901 – 1927 - Greatest Generation.

1928 - 1942 - Silent Generation or Traditionalist.

1943 – 1964 - Baby Boomers.

1965 – 1976 - Generation X

1977 – 1995 - Millennials or Generation Y.

1996+ - Centennials or Generation Z.

Write a program that asks the user their name and year of birth then outputs a message greeting them and telling them which generation they belong to.

I) Write the program using if statements.

II) Design (but don’t write) this program using a switch statement, why is an if structure a better solution for this problem ?

Exercise 2.

Write a program that asks the user to input a month of the year (1-12) and a day in the month (1-31).  The program should check whether the date is valid and return the season the date is in.

Useful information :

UK season dates are :

Spring – March 20th to June 20th

Summer – June 21st to Sept 21st

Autumn – Sep 22nd to Dec 20th

Winter – Dec 21st – March 19th.

Exercise 3.

Adapt the program from exercise 2 to also take a year and check for leap years.