Question

In: Computer Science

The following code snippet is free of errors (issues that will prevent the code from compiling)...

The following code snippet is free of errors (issues that will prevent the code from compiling) but it does contain a major bug that will cause the program to hang (not finish running). Identify the problem and create a line of code to fix it.

#include <stdio.h>

char get_input();

int main() {
    printf("The user's inputted letter is %c\n", get_input());
    return 0;
}

char get_input() {
    printf("Please enter a letter: \n");
    char input = getchar();
    
    while (!(input >= 'a' && input <= 'z') && !(input >= 'A' && input <= 'Z')) {
        printf("You did not enter a letter! Try again\n");
    }
    
    return input;
}

Solutions

Expert Solution

Answer

Here is your answer, if you have any doubt please comment, i am here to help you.

here is your fix for the above problem.

ERROR

  •   while (!(input >= 'a' && input <= 'z') && !(input >= 'A' && input <= 'Z')) {
            printf("You did not enter a letter! Try again\n");
        }
    
    /*there is no exit for the above while loop, any while loop or for loop there must be a terminate statement.
    
    Here you don't need while loop, just only enough if statement. 
    
    If your are using while just add break after printf()*/

FIX

#include <stdio.h>

char get_input();

int main() {
    printf("The user's inputted letter is %c\n", get_input());
    return 0;
}

char get_input() {
    printf("Please enter a letter: \n");
    char input = getchar();
    //just change while loop to if, then not use break
    while (!(input >= 'a' && input <= 'z') && !(input >= 'A' && input <= 'Z')) {
        printf("You did not enter a letter! Try again\n");
        //break;  //use break if while loop using, 
    }
    
    return input;
}

output

Any doubt please comment,

Thanks in advance


Related Solutions

My code does not compile, I am using vim on terminal and got several compiling errors...
My code does not compile, I am using vim on terminal and got several compiling errors this is C++ language I need help fixing my code below is the example expected run and my code. Example run (User inputs are highlighted): Enter your monthly salary: 5000 Enter number of months you worked in the past year: 10 Enter the cost of the car: 36000 Enter number of cars you’ve sold in the past year: 30 Enter number of misconducts observed...
[50%] Code Snippet Given snippet code below that you are required to complete. You are not...
[50%] Code Snippet Given snippet code below that you are required to complete. You are not allowed to make a new function or change any given code. Please complete each section that are marked with the notation “INSERT YOUR CODE HERE”. Once you complete the snippet below, your output should have the same result with the given output below. Descriptions: [15%] isValid() This function is for checking that there is no duplicated employee data in the linked list. This function...
Please fix this code I am having issues compiling it all together there is 3 codes...
Please fix this code I am having issues compiling it all together there is 3 codes here and it's giving me errors in my main method..... I feel like it might be something simple but I can't seem to find it. package assignement2; import java.util.ArrayList; import java.util.Scanner; public class reg1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of items: "); int number = input.nextInt(); input.nextLine(); for (int i = 0; i <...
What is the output of the following code snippet? (If there is some kind of syntax...
What is the output of the following code snippet? (If there is some kind of syntax error indicate this by marking "error" in your answer choice) 1.        int laps = 8;        if (laps++ > --laps)               laps += 2;        else if (--laps > (laps - 1))               laps += 4;        else if (++laps)               laps -= 3;        cout << laps << endl; 2. What is the output of the following code snippet?     int j = 47;     int i = 8;     j =...
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
• Based on the following code snippet and plan attributes, provide the output for Plan A,...
• Based on the following code snippet and plan attributes, provide the output for Plan A, Plan B and Plan C. Code Snippet • {{IF:[Copay]=Not Covered && [Coinsurance]=Not Covered, show ‘Not covered’}} • {{IF:[Copay]!=$0.00 && [Copay]!=Blank, show value from {{Copay}} copayment}} • {{IIF:[Copay]!=$0.00 && [CopayFrequency]!=Blank, show {{Copay}} copayment/{{CopayFrequency}}}} • {{IF:[CopayMax]=$0.00 OR [Copay]=$0.00, show No Charge}} • {{IF:[[CopayMax]!=$0.00 && [CopayMax] contains ‘$’ && [Coinsurance] contains ‘%’ && [CopayMaxFrequency]=Blank, show {{CopayMax}} copayment then {{Coinsurance}} coinsurance]}} Plan Copay CopayFrequency CopayMax CopayMaxFrequency Coinsurance Plan...
Explain line-by-line what the following snippet code from a .htaccess (WordPress) file means: # Restricts access...
Explain line-by-line what the following snippet code from a .htaccess (WordPress) file means: # Restricts access to PHP files from plugin and theme directories RewriteCond %{REQUEST_URI} !^/wp-content/plugins/file/to/exclude\.php RewriteCond %{REQUEST_URI} !^/wp-content/plugins/directory/to/exclude/ RewriteRule wp-content/plugins/(.*\.php)$ - [R=404,L] RewriteCond %{REQUEST_URI} !^/wp-content/themes/file/to/exclude\.php RewriteCond %{REQUEST_URI} !^/wp-content/themes/directory/to/exclude/ RewriteRule wp-content/themes/(.*\.php)$ - [R=404,L]
Explain line-by-line what the following snippet code from a .htaccess (WordPress) file means: RewriteEngine On RewriteBase...
Explain line-by-line what the following snippet code from a .htaccess (WordPress) file means: RewriteEngine On RewriteBase / RewriteRule ^wp-admin/includes/ - [F,L] RewriteRule !^wp-includes/ - [S=3] RewriteRule ^wp-includes/[^/]+\.php$ - [F,L] RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L] RewriteRule ^wp-includes/theme-compat/ - [F,L]
Write a code snippet for the following:   You need to control the maximum number of people...
Write a code snippet for the following:   You need to control the maximum number of people who can be in a   restaurant at any given time. A group cannot enter the restaurant if they   would make the number of people exceed 100 occupants. Use random numbers   between 1 and 20 to simulate groups arriving to the restaurant. After each   random number, display the size of the group trying to enter and the number   of current occupants. As soon as the...
Identify all private and shared variables in the following code snippet. What is the output of...
Identify all private and shared variables in the following code snippet. What is the output of the program (e.g. the value of res)? // ========= int A[100]; int count; void work(int index[]) { float B[10]; …… } void main(){ int res = 10; #pragma omp parallel   { int index[10], i=0, IS=0;                         work(index);                         #pragma omp for                         for(i=0;i<100;i++)                         { IS = IS + i;                         } #pragma omp critical res += IS; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT