1,105,281 Community Members

Switch Statment not working

Member Avatar
infantheartlyje
Light Poster
30 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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; 
}
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

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

Member Avatar
infantheartlyje
Light Poster
30 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Moschops
Nearly a Posting Virtuoso
1,363 posts since Sep 2008
Reputation Points: 414 [?]
Q&As Helped to Solve: 266 [?]
Skill Endorsements: 16 [?]
Featured
 
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;
 
}
Member Avatar
infantheartlyje
Light Poster
30 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: