Program that asks the user to input a month of the year

Assignment Help Other Subject
Reference no: EM132449140

LAB EXERCISE

Part 1.
In previous exercises we've used getchar() to prevent VS from closing the output window but what the function really does is read in a character from the keyboard. We can store the character retrieved by placing it in a char variable like so :

char input;
input = getchar();

The char entered by the user is now stored in the variable input.
Write a simple program that reads in 3 characters from the keyboard, and outputs to the screen on one line all three characters and the sum of their ASCII values.

LAB EXERCISE

Part 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.

LAB EXERCISE (WHILE AND FOR)
Part 3.
Ask a user to type in a short message for inclusion in a twitter message (280 Characters or less). Once the users has typed in the message the program should :
a) Repeat the message.
b) Display statistics showing :
• Total number of non-white space characters in the message.
• Total number of number in the message.
• Total number of lower case letters in the message.
• Total Number of upper case letters in the message.
• Total Number of non-alphanumeric or white space characters in the message.
The most common character in the message.

LAB EXERCISE
Lab 10 - File Input / Output (I/O)

In many circumstances our programs need to read and write to files, this tutorial will look at file handling in C.
The template for declaring a file in C looks like the following :
FILE *fp;
FILE *fopen(char *name, char *mode);

So fp is a pointer and FILE is a datatype (like short, int, float etc.). To call fopen in a program we use :
fp = fopen(name, mode);
Here name is the filename and mode refers to what we intend to do with the file. The usual modes are ("r") for read, ("w") for write and ("a") for append. We can also specify whether the file is binary or text, for our purposes we will just be using text files (the most common).
Below is an example of a simple Program to save some text to a file :
#include<stdio.h>
main()
{
FILE *fp;
char input[255];
fp = fopen("test.txt","a");
printf("Please enter some text to write to the file : \n");
scanf("%[^\n]s", input);
fprintf(fp, "%s\n", input);
fclose(fp);
printf("\n");
printf("Done.");
}
There are a few new things here so let's look at them.
FILE *fp; declares our file pointer (it doesn't have to be called fp). inputis a char array that will hold the user's input.
fp = fopen("test.txt","a"); this line opens the file where the text will go, "test.txt" is the name of the file and "a" is the mode. In this case we are using append mode, if the file already exists then the program will add new text to the end of it, if the file doesn't exist the program will create it. We could have used "w" for write, in that case the program would again create the file if it didn't exist but if it did exist it would overwrite any text in it.

We then ask the user for the text for the file. Notice the scanf line is slightly different than before. The part "%[^\n]s" is different than the usual "%s" for string the extra [^\n] tells scanf to keep accepting input until it gets to an end of line (EOL) symbol, this is when the user hits enter. If we use "%s" then scanf will stop as soon as it gets to any whitespace (spaces, tabs etc.).

The next line writes the text into the file :
fprintf(fp, "%s\n", input);
fprintf is the function for writing to the file (there are other functions that can be used such as fputs). fprintf needs three parameters : the file pointer (fp), the format of the data being written ("%s\n") here we are writing a string followed by a newline, and the data being written (input).
Finally we close the file and report back that we have finished.
We can check the contents of the file by loading it into a text editor (notepad etc.).
Reading Files.

So much for writing what about getting the data out of a file and into a program. The program below is a simple C program that opens a file and writes the contents into a char array.

#include<stdio.h>
main()
{
FILE *fp;
char input[255];
fp = fopen("test.txt","r");
fscanf(fp, "%[^\n]s", input);
fclose(fp);
printf("Contents of the file are :\n %s \n", input);
}

Not too much different here, this time we open the file using the "r" mode for reading (we can't write anything to the file in this mode). To read the data in the file we are using fscanf (again other functions can do this like fgets), fscanf is similar to fprintf in that it needs to know the file pointer, the datatype and where to store the incoming data. Again we've used "%[^\n]s" to keep scanning until we hit an end of line.

Notes.

Both of the programs here will write and read one line to and from a file. We also have to be careful that we are not trying to write or read more than 255 characters or we will have problems with array overflow.

Exercises.

1. Write a program that will read it 5 lines of text from a user and write it to a file.

2. Write a program that will read in the 5 lines of text from 1. And print them to the screen.
(Hint for 1 and 2 remember loops).

3. Write a program that asks the user for two filenames and checks whether the two files given have the same first line.

Reference no: EM132449140

Questions Cloud

Discuss areas in which health problems were identified : Based on your findings, describe at least two of the functional health pattern strengths noted in the findings. Discuss three areas in which health problems or.
Creating a New Position : Explain what will be included in the compensation and benefits package related to the job.
ITECH1100 Video and Disruption Report Assignment : ITECH 1100 Understanding the Digital Revolution - Video and Disruption Report Assignment Help and Solution, Federation University Australia
What are two public policies needed to improve quality : Public Health and Disparities and Social Determinants of Health. In 500-650 words, post in your assigned group your response to the prompt.
Program that asks the user to input a month of the year : Write a simple program that reads in characters from the keyboard, and outputs to the screen on one line all three characters and the sum of their ASCII value
Create an artifact that visualizes aspect of human identity : Why did you choose this trait? Is it a defining part of your identity, or rather an element of your identity by which you would rather not be characterized?
Discuss how digital veillance tracked on an average day : What data might be collected and to what extent can a person reject or resist digital veillance?Discuss how digital veillance tracked on an average day
Health policy and law basics : As a chief operating officer of a hospital, you have been tasked with opening a new ambulatory care center in your city.
Discuss bio-psycho-social : Identify which human behavior or social theories may guide your practice with this individual and explain how these theories inform your assessment.

Reviews

Write a Review

Other Subject Questions & Answers

  Cross-cultural opportunities and conflicts in canada

Short Paper on Cross-cultural Opportunities and Conflicts in Canada.

  Sociology theory questions

Sociology are very fundamental in nature. Role strain and role constraint speak about the duties and responsibilities of the roles of people in society or in a group. A short theory about Darwin and Moths is also answered.

  A book review on unfaithful angels

This review will help the reader understand the social work profession through different concepts giving the glimpse of why the social work profession might have drifted away from its original purpose of serving the poor.

  Disorder paper: schizophrenia

Schizophrenia does not really have just one single cause. It is a possibility that this disorder could be inherited but not all doctors are sure.

  Individual assignment: two models handout and rubric

Individual Assignment : Two Models Handout and Rubric,    This paper will allow you to understand and evaluate two vastly different organizational models and to effectively communicate their differences.

  Developing strategic intent for toyota

The following report includes the description about the organization, its strategies, industry analysis in which it operates and its position in the industry.

  Gasoline powered passenger vehicles

In this study, we examine how gasoline price volatility and income of the consumers impacts consumer's demand for gasoline.

  An aspect of poverty in canada

Economics thesis undergrad 4th year paper to write. it should be about 22 pages in length, literature review, economic analysis and then data or cost benefit analysis.

  Ngn customer satisfaction qos indicator for 3g services

The paper aims to highlight the global trends in countries and regions where 3G has already been introduced and propose an implementation plan to the telecom operators of developing countries.

  Prepare a power point presentation

Prepare the power point presentation for the case: Santa Fe Independent School District

  Information literacy is important in this environment

Information literacy is critically important in this contemporary environment

  Associative property of multiplication

Write a definition for associative property of multiplication.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd