Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can anyone help me to reprogram this in such a way that, that I don't need to use any sort of variable?

//This my code

#include <stdio.h>
#include <conio.h>


int processChoice()
{
    int choice = -1; //I need to execute this code without using any variable?  
    printf("\nMake a Choice (1, 2, 3 or 0): ");
    scanf("%d",&choice);
    printf("%d",choice);
    
    switch(choice)
    {
        case 0:
        printf("\nExiting...\n");
        break;
        case 1:
        printf("\nDrawing rectangle...\n");
        break;
        case 2:
        printf("\nDrawing Right triangle...\n");
        break;
        case 3:
        printf("\nDrawing isosceles triangle...\n");
        break;
        
        default:
        printf("\n** Invalid Choice! **\n");
        choice = -1;
    }
    return choice;
}

void showMenu()
{
    printf("\nMenu:");
    printf("\n1. Draw Rectangle");
    printf("\n2. Draw Right triangle");
    printf("\n3. Draw isosceles triangle");
    printf("\n0. Exit program\n");
}


int main()
{
    int x = -1;
    do
    {
        showMenu();
      
    }while(processChoice() != 0);
    return 0;
}

/* That’s my code here I used a variable "int Choice = -1;" I’m supposed to execute the same code without using any variable as per guidelines of my mentor. Please help me with this
*/

I’m expecting the same code to be executed without using any variable.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Maybe your mentor means that this declaration in main

int x = -1;

is not used and should be removed.

As for the function processChoice then in any case you need to enter a value from the user. I see the only possibility to write the function without using a variable the following way with using function getchar

int processChoice( void )
{
    printf("\nMake a Choice (1, 2, 3 or 0): ");
    
    switch( getchar() )
    {
        case '0':
        printf("\nExiting...\n");
        while ( getchar() != '\n' );
        return 0;

        case '1':
        printf("\nDrawing rectangle...\n");
        while ( getchar() != '\n' );
        return 1;

        case '2':
        printf("\nDrawing Right triangle...\n");
        while ( getchar() != '\n' );
        return 2;

        case '3':
        printf("\nDrawing isosceles triangle...\n");
        while ( getchar() != '\n' );
        return 3;
        
        default:
        printf("\n** Invalid Choice! **\n");
        while ( getchar() != '\n' );
        return -1;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading