Home > Mobile >  Can anyone help me to reprogram this in such a way that, that I don't need to use any sort of v
Can anyone help me to reprogram this in such a way that, that I don't need to use any sort of v

Time:11-15

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

CodePudding user response:

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;
    }
}

CodePudding user response:

/*I guess there are some communication barriers between you and your mentor XD 
It's easy follow these corrections
*/
#include <stdio.h>

#include <conio.h>


int processChoice() {
  int choice; //You're not required to use -1
  printf("\nMake a Choice (1, 2, 3 or 0): ");
  scanf("%d", & choice);
  printf("%d", choice);

  switch (choice) {
  case 0:
    printf("\nExiting...\n");
    break;               // Here  the code should be intended by at least one tab. It is a part of clean code..
  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; // default initializing to zero it is unecessary to use -1 here.
  }
  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() {
// Delete again initializing of this here aka int x = -1;
  do {
    showMenu();

  } while (processChoice() != 0);
  return 0;
}
  • Related