Question

Write a C program as follows: Single source code file Uses an infinite loop Requests the...

Write a C program as follows:

  • Single source code file
  • Uses an infinite loop
  • Requests the user to press a key
  • Uses a nested switch statement
  • Either prints the key pressed, or terminates using the break statement and prints Goodbye! if the user presses either the e or E key

Please include lots of comments please, this is how I learn how to code. I need LOTS of comments!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you find this helpful, then please leave a like. The code is explained in the comments.

CODE:

#include <stdio.h>
#include <stdlib.h>

int main()
{
char ch; //Character variable to store user's input character
int exit_flag=0; //Flag used to exit from the infinite loop. The loop is exited when it's value is 1.
while(1) //Infinite flag. Any value other than 0 is evaluated to true. Hence loop with while(0) is never executed
{
printf("Enter a character:"); //Prompting the user to press key.
scanf(" %c",&ch); //Taking the user input and storing it in variable ch. Here we are passing the address of ch (&ch) so that scanf can change the actual value of ch.
switch(ch) //Switch block
{
case 'e': //When ch=='e', the program control comes here and then continues through the body of switch statement till a break statement is found. Since no break statement is found here, it moves to the next code i.e, code of case 'E'.
case 'E': //When ch=='E'
printf("Goodbye!"); //Printing the output
exit_flag=1; //Setting the flag to 1
break; //Breaks the program flow out of the switch block
default: //When ch!='e' && ch!='E'
printf("Entered character:%c\n",ch); //Outputting the entered key
}
if(exit_flag==1) //If the flag is set to 1, i.e, exit the loop
break; //Breaking out of the infinite loop
}
return 0;
}

OUTPUT:


Add a comment
Know the answer?
Add Answer to:
Write a C program as follows: Single source code file Uses an infinite loop Requests the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program as follows: Single source code file Requests the user to input two...

    Write a C program as follows: Single source code file Requests the user to input two integer numbers Requests the user to make a choice between 0 (add), 1 (subtract), or 2 (multiply) Declares three separate functions Uses a pointer to these three functions to perform the requested action Outputs the result to the screen Submit your program source code file to this assignment. Sample Output Enter first integer number: 15 Enter second integer number: 10 Enter Choice: 0 for...

  • Write a C program as follows: Single source code file Calls a function with an arbitrary...

    Write a C program as follows: Single source code file Calls a function with an arbitrary name (i.e. you name it) that accepts two arrays of the same size The function should add each element in the arrays together and place the values in a third array Each array element, each array address, and the sum are printed to the screen in tabulated format with headersI also would like to request that LOTS of comments be included, as I need...

  • CSC 130 Lab Assignment 8 – Program Menu Create a C source code file named lab8.c...

    CSC 130 Lab Assignment 8 – Program Menu Create a C source code file named lab8.c that implements the following features. Implement this program in stages using stepwise refinement to ensure that it will compile and run as you go. This makes it much easier to debug and understand. This program presents the user a menu of operations that it can perform. The choices are listed and a prompt waits for the user to select a choice by entering a...

  • Please post following code with criteria below... please leave as many comments as possible Exam Four:...

    Please post following code with criteria below... please leave as many comments as possible Exam Four: the election Outcome: Student will demonstrate all core competencies from this class: Program Design (design tools) Variables Decision Error Checking Looping Functions Arrays Program Specifications: Let’s pretend that we are tracking votes for the next presidential election. There will be two candidates: Ivanka Trump and Michele Obama. You can assume that there are fifty states casting votes. You will do not need to deal...

  • please code in basic c++ program Write a program that uses the following formula: n (ax...

    please code in basic c++ program Write a program that uses the following formula: n (ax - ib) i=1 Your program will prompt the user for the number of iterations for the calculation. input values for n, x, a, b, and c. n must be a positive integer, but x, a, b, and c can be any real numbers. Then it will perform the calculation and output the result. Then, it will ask the user if they would like to...

  • IN C++ Help to fix code Library management program. Error is when i type a letter...

    IN C++ Help to fix code Library management program. Error is when i type a letter into options menu the code breaks. after your edits, the code should still run as normal. CODE: #include<iostream> using namespace std; //structure to store library data class BookDetails{ public: struct library { int code; char title[20]; int status; }; library book_details[100]; }; //structure to store user data class UserDetails:public BookDetails{ public: struct users { int id; char name[20]; int booksNumber; }; users user_details[100]; };...

  • In C++ Write a program that will calculate the cost of a phone call as follows:...

    In C++ Write a program that will calculate the cost of a phone call as follows: The first 10 minutes are charged at a flat rate of $0.99 for the entire 10 minutes (Not 99 cents a minute - but 99 cents for the first 10 minutes total). Every minute after the initial 10 minutes will be charged at $0.10 per minute. Input the number of minutes talked as an integer. Using an IF/ELSE structure, check for an entry of...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

  • This is for a Unix class. Please help me out. I am attaching a skeletal code of the program below, it just needs ti be filled in. Below is a skeletal code of the program. Fork a child process...

    This is for a Unix class. Please help me out. I am attaching a skeletal code of the program below, it just needs ti be filled in. Below is a skeletal code of the program. Fork a child process and then use the parent for reading and the child for writing.  This is just a way of sending and receiving messages asynchronously. /* ************************************************************* * Utility functions * ************************************************************** */ static void usageError(const char * progName, const char *msg) {...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT