Question

int main() char in, Cls; int r-0; printf(A: Circle nB: Square\nC: Triangle\n); printf(Enter Which Shape to use: \n); scanf(%c, &in); switch(in) [ case A printf(Enter P for solve for perimeter and A for solve for Area\n); printf(Choice: ); scanf(%c, &cis); / /cis is choice printf(Enter circle radius: ); scanf(%lf, &r); printf(Circle perimeterr is:, perimeter(A, 2, 0, e)); return 0;

Why when i enter A, and the second scanf founction is not work, there are no value for cis.

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

The scanf does not work because in the first scanf i.e.

scanf("%c", &in);

Here you type a character and press enter.

The scanf will assign the character you typed to 'in' variable but the enter i.e. the newline character is left behind.

So when you try to scan another character 'cis' - scanf("%c", &cis); - the newline character from the previous scanf will be consumed automatically, which gives rise to the problem you are facing.

There are two ways to rectify the error

1) Use a getchar() after the first scanf() to consume the newline character.

#include <stdio.h>

int main()

{

    // Variable declaration

    char in, cis;

    int r = 0;

   

    // Display menu

    printf("A: Circle\nB: Square\nC: Triangle\n");

    printf("Enter which shape to use:\n");

    scanf("%c", &in);

    // Consume the newline character from the above scanf

    getchar();

   

    switch(in) {

        case 'A':

        printf("Enter P for solve for perimeter and A for solve for Area\n");

        printf("Choice: ");

        scanf("%c", &cis); // cis is Choice

        // scanf(" %c", &cis); // cis is Choice

        if(cis == 'P') {

            printf("Enter circle radius: ");

            scanf("%d", &r);

           

            // Rest of the code

            // ...

        }

    }

    return 0;

}

SAMPLE OUTPUT:

2) Use a <space> in the second scanf statement - scanf(" %c", &cis);

#include <stdio.h>

int main()

{

    // Variable declaration

    char in, cis;

    int r = 0;

   

    // Display menu

    printf("A: Circle\nB: Square\nC: Triangle\n");

    printf("Enter which shape to use:\n");

    scanf("%c", &in);

    // Consume the newline character from the above scanf

    // getchar();

   

    switch(in) {

        case 'A':

        printf("Enter P for solve for perimeter and A for solve for Area\n");

        printf("Choice: ");

        // scanf("%c", &cis); // cis is Choice

        scanf(" %c", &cis); // cis is Choice

        if(cis == 'P') {

            printf("Enter circle radius: ");

            scanf("%d", &r);

           

            // Rest of the code

            // ...

        }

    }

    return 0;

}

SAMPLE OUTPUT:

A: Circle B: Square C: Triangle Enter which shape to use: Enter P for solve for perimeter and A for solve for Area Choice: P

Add a comment
Know the answer?
Add Answer to:
Why when i enter A, and the second scanf founction is not work, there are no...
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
  • I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the...

    I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the program until they enter "q" to quit. However, my program runs through one time, the prompt appears again, but then it terminates before the user is allowed to respond to the prompt again. I'm not able to debug why this...

  • So I have a question in regards to my program. I'm learning to program in C...

    So I have a question in regards to my program. I'm learning to program in C and I was curious about the use of functions. We don't get into them in this class but I wanted to see how they work. Here is my program and I would like to create a function for each of my menu options. I created a function to display and read the menu and the option that is entered, but I would like to...

  • NEED CODE HELPS WITH C PROGRAMMING. ISSUES EXPLAINED IN BOLD. COMPARING TWO LETTERS AND CALLING A...

    NEED CODE HELPS WITH C PROGRAMMING. ISSUES EXPLAINED IN BOLD. COMPARING TWO LETTERS AND CALLING A FUNCTION.   Problem are explained in bold #include <stdio.h> #include <string.h> #include <stdlib.h> #define pi 3.1415 void Area (double n); void VolSphere (double n); void Circumference (double n); void AllCalc (); // i dont know if the declaration of the function is correct AllCalc = AllCalculations //function main begins program execution int main (void) { puts("-Calculating Circle Circumference, Circle Area or Sphere Volume-\n"); void (*f[4])...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • /* I want to fix void make_flight(int counter, flight_t flights[]) Enter flight code> Enter departure info...

    /* I want to fix void make_flight(int counter, flight_t flights[]) Enter flight code> Enter departure info for the flight leaving SYD. Enter month, date, hour and minute separated by spaces> Enter arrival city code> Enter arrival info. Enter month, date, hour and minute separated by spaces> It should do all of this: 1-Flight - left aligned, MAX_FLIGHTCODE_LEN (i.e. 6) chars at most. 2-City - left aligned, MAX_CITYCODE_LEN 3 chars at most . For example( VA1 or LAX ) 3- Month,...

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

  • Create another program that will prompt a user for input file. The user will choose from...

    Create another program that will prompt a user for input file. The user will choose from 4 choices: 1. Display all positive balance accounts. 2. Display all negative balance accounts. 3. Display all zero balance accounts. 4. End program. C PROGRAMMING my code so far #include<stdlib.h> #include<stdio.h> int main(void) { int request; int account; FILE *rptr; char name[20]; double balance; FILE *wptr;    int accountNumber;    if((wptr = fopen("clients.dat","w")) == NULL) { puts("File could not be opened"); } else {...

  • create case 4 do Method 1:copy item by item and skip the item you want to...

    create case 4 do Method 1:copy item by item and skip the item you want to delete after you are done, delete the old file and rename the new one to the old name. #include int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1:...

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