Here is my code . Switch statement doesn't working in this. why?

int callprocess(FILE *File)
{
	int Rec = 0; // record number
    	
    	char tcode[3];
    	//char buffer[30];
    	struct timeval b,e;
    	long int tms,tme;
	do
      
    	{
	 	printf("\nMenu :Init, Dummy, Add or Quit ( I, U, A or Q) : ");
        	switch(toupper(getchar()))
          	{ 
			case 'I' :
        	      		printf("Creating dummy file of 10 entries\n");
        	      		InitFile(File);
       		      		break;
	 		case 'U' :
             		      	printf("Creating dummy file of 1000 entries\n");
          	      		adddummyfile(File);
              	      		break;
            		case 'A' :
              			Rec = AddRecord(File);
         			printf("Record #%d Added\n\n", Rec);
         			break;              
           		
            		case 'Q' :
              			fclose(File);
				//if(c=='1')
				//	deletefile(FNAME);
				//else
					//deletefile(TEMPF);
              			return 0; 
		}while(getchar() != '\n'); 
    	}while (1); 
}
int main (void)
{ 
	FILE *File;	
    	char c;	
    	srand(time(NULL));
	printf("Random Access File Demonstration\n\n");
	
	printf("Which File Do You want to read ? 1. abc.cli 2. tmp.cli ( 1 , 2) : ");
	scanf("%c",&c);
	if(c=='1')
		File = FileOpen(FNAME);
	else if (c == '2')
		File = FileOpen(TEMPF);
    	if (!File)
      	{ 
		printf("Curses foiled again!\n\n");
        	exit(-1); 
	}

        callprocess(File);
    	
    	return 0; 
}

Mind giving us useful information? Like what it does wrong?

Mind giving us useful information? Like what it does wrong?

Okie...if i enter 'U'. The statements under 'U' doesn't working. For all case.

Nothing inherently wrong with using the switch statement like that. The following works (excuse the C++).

#include <cstdlib>
#include <iostream>
#include <cctype>
 
int main()
{
  switch(toupper(getchar()))
 {
  case 'U':
    std::cout << "It's you!";
    break;
  default:
    std::cout << "Not";
    break;
}
  return 0;
 
}

Nothing inherently wrong with using the switch statement like that. The following works (excuse the C++).

#include <cstdlib>
#include <iostream>
#include <cctype>
 
int main()
{
  switch(toupper(getchar()))
 {
  case 'U':
    std::cout << "It's you!";
    break;
  default:
    std::cout << "Not";
    break;
}
  return 0;
 
}

Use like this before switch statment

printf("Which File Do You want to read ? 1. abc.cli 2. tmp.cli ( 1 , 2) : ");
scanf("%c",&c);
if(c=='1')
File = FileOpen(FNAME);
else if (c == '2')
File = FileOpen(TEMPF);

And compile

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.