Like Tree2Likes

HELP! How do I execute a function in a switch statement??

This is a discussion on HELP! How do I execute a function in a switch statement?? within the C Programming forums, part of the General Programming Boards category; Assuming i already have a readImage function, how do i prompt the user to call it by typing in "Sailing"? ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    32

    HELP! How do I execute a function in a switch statement??

    Assuming i already have a readImage function, how do i prompt the user to call it by typing in "Sailing"?

    instructions:

    1.3.1 Load a PPM Image
    This option prompts the user for the name of an image file. You don't have to implement a file reading
    function; just use the provided one, ReadImage. Once option 1 is selected, the following should be shown:
    Please input the file name to load: sailing
    After a name, for example sailing, is entered, the PhotoLab will load the file sailing.ppm. Note that, in
    this assignment please always enter file names without the extension when you load or save a file (i.e. enter
    'sailing', instead of 'sailing.ppm'). If it is read correctly, the following is shown:
    Please make your choice: 1
    Please input the file name to load: sailing
    sailing.ppm was read successfully!
    --------------------------------
    1: Load a PPM image
    2: Save an image in PPM and JPEG format
    3: Change a color image to black and white
    4: Make a negative of an image
    5: Flip an image horizontally
    2
    6: Mirror an image horizontally
    7: Flip an image vertically
    8: Mirror an image vertically
    9: Test all functions
    10: Exit
    Please make your choice:
    Then, you can select other options. If there is a reading error, for example the file name is entered incorrectly
    or the file does not exist, the following message is shown:
    Cannot open file "sailing.ppm.ppm" for reading!


    This is what i have so far i dont know how to use a switch statement to call the function readImage becuase I already have a printf statement in it. HELP

    Code:
    int main(void)
    {
    	int c;
    	char name;
    
    	do
    	{
    		printf("1: Load a PPM image\n");
    		printf("2: Save an image in PPM and JPEG format\n");
    		printf("3: Change a color image to black and white\n");
    		printf("4: Make a negative of an image\n");
    		printf("5: Flip an image horizontally\n");
    		printf("6: Mirror an image vertically\n");
    		printf("7: Flip an image vertically\n");
    		printf("8: Mirror an image vertically\n");
    		printf("9: Test all functions\n");
    		printf("10: Exit\n");
    		printf("Please make your choice: \n");
    		scanf("%d",&c);
    	
    
    		switch(c)
    		{
    		case 1: 
    			printf("Please input the file name to load: \n");
    			scanf("%s",name);
    			readImage?
    				
    			break;
    		}
    	}

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Does the readImage function take an argument such as the file name to be loaded/read? If so then you'd need to pass the file name to the function so that it can process that particular file. I'm guessing you'd want to pass the name variable to the readImage function.

    On the topic of the name variable, a single char is not enough to store a file name. You need to make name an array of sufficient size.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    I am not really sure what your question is exactly? If you want to call readImage from within the switch statement, just call it like you would call any other function. A mistake in your code is also that you are trying to read a string into a single char variable. You must change char name into either a pointer to a char, and allocate memory for the string, or change it to a char array. You should also make sure that the user doesn't enter a filename which is longer than your filename buffer can hold.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    This is my readImage function: (which is given)

    I just need to know how to call it in my switch function, so when the user types in "Sailing" it loads the Picture from the file.

    Code:
    /*Three function definitions are provided:ReadImage, ReadImageW, SaveImage*/
    int
    ReadImage(char fname[SLEN], unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT])
    {
    	FILE           *File;
    	char            Type[SLEN];
    	int             Width, Height, MaxValue;
    	int             x, y;
    	char            ftype[] = ".ppm";
            char            fname_tmp[SLEN];  /*avoid to modify on the original pointer, 11/10/10, X.Han*/
    
            strcpy(fname_tmp, fname);
    	strcat(fname_tmp, ftype);
    
    	File = fopen(fname_tmp, "r");
    	if (!File) {
    		printf("\nCannot open file \"%s\" for reading!\n", fname);
    		return 1;
    	}
    	fscanf(File, "%79s", Type);
    	if (Type[0] != 'P' || Type[1] != '6' || Type[2] != 0) {
    		printf("\nUnsupported file format!\n");
    		return 2;
    	}
    	fscanf(File, "%d", &Width);
    	if (Width != WIDTH) {
    		printf("\nUnsupported image width %d!\n", Width);
    		return 3;
    	}
    	fscanf(File, "%d", &Height);
    	if (Height != HEIGHT) {
    		printf("\nUnsupported image height %d!\n", Height);
    		return 4;
    	}
    	fscanf(File, "%d", &MaxValue);
    	if (MaxValue != 255) {
    		printf("\nUnsupported image maximum value %d!\n", MaxValue);
    		return 5;
    	}
    	if ('\n' != fgetc(File)) {
    		printf("\nCarriage return expected!\n");
    		return 6;
    	}
    	for (y = 0; y < HEIGHT; y++)
    		for (x = 0; x < WIDTH; x++) {
    			R[x][y] = fgetc(File);
    			G[x][y] = fgetc(File);
    			B[x][y] = fgetc(File);
    		}
    	if (ferror(File)) {
    		printf("\nFile error while reading from file!\n");
    		return 7;
    	}
    	printf("%s was read successfully!\n", fname_tmp);
    	fclose(File);
    	return 0;
    }
    Last edited by kkmoslehpour; 10-12-2011 at 01:05 PM.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    What do i put in the bold?

    Code:
    int main(void)
    {
        int c;
        char name;
     
        do
        {
            printf("1: Load a PPM image\n");
            printf("2: Save an image in PPM and JPEG format\n");
            printf("3: Change a color image to black and white\n");
            printf("4: Make a negative of an image\n");
            printf("5: Flip an image horizontally\n");
            printf("6: Mirror an image vertically\n");
            printf("7: Flip an image vertically\n");
            printf("8: Mirror an image vertically\n");
            printf("9: Test all functions\n");
            printf("10: Exit\n");
            printf("Please make your choice: \n");
            scanf("%d",&c);
         
     
            switch(c)
            {
            case 1:
                printf("Please input the file name to load: \n");
                scanf("%s",name);
                How do i call readImage function when Sailing is entered?? what do i put here to call it?? 
                break;
            }
        }

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by kkmoslehpour View Post
    What do i put in the bold?
    Just the function call, there is no magic to it. You are already calling a function earlier in your program, printf(), you just do the same with ReadImage(name, ...). You just need to pass the filename and the rest of the arguments to it.

    And don't forget to change name from char name; to char name[XX] where XX is the max number of letters (-1 for the null termination) in the filename.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    Quote Originally Posted by iceaway View Post
    Just the function call, there is no magic to it. You are already calling a function earlier in your program, printf(), you just do the same with ReadImage(name, ...). You just need to pass the filename and the rest of the arguments to it.

    And don't forget to change name from char name; to char name[XX] where XX is the max number of letters (-1 for the null termination) in the filename.

    Like this?
    but then Sailing becomes unidentified then...

    Code:
    switch(choice)
    		{
    		case 1: 
    			printf("Please input the file name to load: \n");
    			scanf("%s",name);
    			if(fileName==sailing)
    				ReadImage();

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by kkmoslehpour View Post
    Like this?
    but then Sailing becomes unidentified then...

    Code:
    switch(choice)
            {
            case 1: 
                printf("Please input the file name to load: \n");
                scanf("%s",name);
                if(fileName==sailing)
                    ReadImage();
    You cannot compare strings (char arrays) like that in C. You have to do:
    Code:
    if (strcmp(fileName, "sailing") == 0) {
        /* fileName is equal to sailing, do stuff */
    }
    Also, your ReadImage expects some more arguments, the filename and the R, G, B two-dimensional arrays.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    sorry i didnt give you the complete code all the file names are already defined. Here is all the complete code...

    can you check if my case 1 is correct?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*** global definitions ***/
    #define WIDTH  640		/* Image width */
    #define HEIGHT 425		/* image height */
    #define SLEN    80		/* maximum length of file names */
    /*** function declarations ***/
    
    /* print a menu */
    void            PrintMenu();
    
    /* read image from a file */
    int             ReadImage(char fname[SLEN], unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
    
    /* save a processed image */
    int             SaveImage(char fname[SLEN], unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
    
    /* change color image to black & white */
    void            BlackNWhite(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
    
    /* reverse image color */
    void            Negative(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
    
    /* flip image horizontally */
    void            HFlip(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
    
    /* mirror image horizontally */
    void            HMirror(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
    
    /* flip image vertically */
    void            VFlip(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
    
    /* mirror image vertically */
    void            VMirror(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
    
    /* Test all functions */
    void            AutoTest(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
    
    
    /*define your main function here*/ 
    
    
    int main(void)
    {
    	int choice;
    	char fileName [10];
    
    	do
    	{
    		printf("1: Load a PPM image\n");
    		printf("2: Save an image in PPM and JPEG format\n");
    		printf("3: Change a color image to black and white\n");
    		printf("4: Make a negative of an image\n");
    		printf("5: Flip an image horizontally\n");
    		printf("6: Mirror an image vertically\n");
    		printf("7: Flip an image vertically\n");
    		printf("8: Mirror an image vertically\n");
    		printf("9: Test all functions\n");
    		printf("10: Exit\n");
    		printf("Please make your choice: \n");
    		scanf("%d",&choice);
    	
    
    		switch(choice)
    		{
    		case 1: 
    			printf("Please input the file name to load: \n");
    			scanf("%s",fileName);
    			if (strcmp(fileName, "sailing") == 0) 
    				{ int returnCode = readImage(fileName, imageR, imageG, imageB);} // is that what you meant?
    
    
        /* fileName is equal to sailing, do stuff */
    }
    		case 2: 
    			printf("please enter the file name to be saved \n");
    			scanf("%s",fileName);
    		case 3:
    			printf("Black & White operation is done! \n");
    
    				
    			break;
    		}
    	}
    
    
    /*Three function definitions are provided:ReadImage, ReadImageW, SaveImage*/
    int
    ReadImage(char fname[SLEN], unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT])
    {
    	FILE           *File;
    	char            Type[SLEN];
    	int             Width, Height, MaxValue;
    	int             x, y;
    	char            ftype[] = ".ppm";
            char            fname_tmp[SLEN];  /*avoid to modify on the original pointer, 11/10/10, X.Han*/
    
            strcpy(fname_tmp, fname);
    	strcat(fname_tmp, ftype);
    
    	File = fopen(fname_tmp, "r");
    	if (!File) {
    		printf("\nCannot open file \"%s\" for reading!\n", fname);
    		return 1;
    	}
    	fscanf(File, "%79s", Type);
    	if (Type[0] != 'P' || Type[1] != '6' || Type[2] != 0) {
    		printf("\nUnsupported file format!\n");
    		return 2;
    	}
    	fscanf(File, "%d", &Width);
    	if (Width != WIDTH) {
    		printf("\nUnsupported image width %d!\n", Width);
    		return 3;
    	}
    	fscanf(File, "%d", &Height);
    	if (Height != HEIGHT) {
    		printf("\nUnsupported image height %d!\n", Height);
    		return 4;
    	}
    	fscanf(File, "%d", &MaxValue);
    	if (MaxValue != 255) {
    		printf("\nUnsupported image maximum value %d!\n", MaxValue);
    		return 5;
    	}
    	if ('\n' != fgetc(File)) {
    		printf("\nCarriage return expected!\n");
    		return 6;
    	}
    	for (y = 0; y < HEIGHT; y++)
    		for (x = 0; x < WIDTH; x++) {
    			R[x][y] = fgetc(File);
    			G[x][y] = fgetc(File);
    			B[x][y] = fgetc(File);
    		}
    	if (ferror(File)) {
    		printf("\nFile error while reading from file!\n");
    		return 7;
    	}
    	printf("%s was read successfully!\n", fname_tmp);
    	fclose(File);
    	return 0;
    }
    
    int
    SaveImage(char fname[SLEN], unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT])
    {
    	FILE           *File;
    	int             x, y;
    	char            SysCmd[SLEN * 5];
    
    	char            ftype[] = ".ppm";
    	char            fname_tmp[SLEN];  /*avoid to modify on the original pointer, 11/10/10, X.Han*/
            char            fname_tmp2[SLEN];
    
    	strcpy(fname_tmp, fname);
            strcpy(fname_tmp2, fname);
    	strcat(fname_tmp2, ftype);
    
    	File = fopen(fname_tmp2, "w");
    	if (!File) {
    		printf("\nCannot open file \"%s\" for writing!\n", fname);
    		return 1;
    	}
    	fprintf(File, "P6\n");
    	fprintf(File, "%d %d\n", WIDTH, HEIGHT);
    	fprintf(File, "255\n");
    
    	for (y = 0; y < HEIGHT; y++)
    		for (x = 0; x < WIDTH; x++) {
    			fputc(R[x][y], File);
    			fputc(G[x][y], File);
    			fputc(B[x][y], File);
    		}
    
    	if (ferror(File)) {
    		printf("\nFile error while writing to file!\n");
    		return 2;
    	}
    	fclose(File);
    	printf("%s was saved successfully. \n", fname_tmp2);
    
    	/*
    	 * rename file to image.ppm, convert it to ~/public_html/<fname>.jpg
    	 * and make it world readable
    	 */
    	sprintf(SysCmd, "/users/grad2/doemer/eecs22/bin/pnmtojpeg.tcsh %s",
    		fname_tmp2);
    	if (system(SysCmd) != 0) {
    		printf("\nError while converting to JPG:\nCommand \"%s\" failed!\n", SysCmd);
    		return 3;
    	}
    	printf("%s.jpg was stored for viewing. \n", fname_tmp);
    
    	return (0);
    }
    
    /*define all other functions here*/

  10. #10
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Looks better, but there are some warnings when compiling it, you don't get any warnings? You need to use break; in your switch-statements after each case (otherwise they will "fall through"), and you have one too many }, just at the end of case 1.

    I can't see imageR/G/B declared anywhere in your code. You have to declare those. You should probably increase the length of the filename variable a bit, most of us use filenames with more than 9 letters occasionally :-) And make sure that you don't write past the array length if the user should enter something which is too long.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    Quote Originally Posted by iceaway View Post
    Looks better, but there are some warnings when compiling it, you don't get any warnings? You need to use break; in your switch-statements after each case (otherwise they will "fall through"), and you have one too many }, just at the end of case 1.

    I can't see imageR/G/B declared anywhere in your code. You have to declare those. You should probably increase the length of the filename variable a bit, most of us use filenames with more than 9 letters occasionally :-) And make sure that you don't write past the array length if the user should enter something which is too long.
    okay im kind of getting this little by little.... thanks for being patient

    Here is the question:

    Are my imageR, imageG, imageB considered as a char? or int? because when i declare it as a char it doesn't like it...

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    never mind i think this is how i do it?

    okay so i have this in my main so far, how does it look?


    also i am not used to compiling it in little sections.. i usually code the whole thing then compile it, because i am afraid of errors and warnings....

    Code:
    int main(void)
    {
    	int choice;
    	char fileName [100];
    	unsigned char imageR[WIDTH][HEIGHT];
    	unsigned char imageG[WIDTH][HEIGHT];
    	unsigned char imageB[WIDTH][HEIGHT];
    	do
    	{
    		printf("1: Load a PPM image\n");
    		printf("2: Save an image in PPM and JPEG format\n");
    		printf("3: Change a color image to black and white\n");
    		printf("4: Make a negative of an image\n");
    		printf("5: Flip an image horizontally\n");
    		printf("6: Mirror an image vertically\n");
    		printf("7: Flip an image vertically\n");
    		printf("8: Mirror an image vertically\n");
    		printf("9: Test all functions\n");
    		printf("10: Exit\n");
    		printf("Please make your choice: \n");
    		scanf("%d",&choice);
    	
    
    		switch(choice)
    		{
    		case 1: 
    			printf("Please input the file name to load: \n");
    			scanf("%s",fileName);
    			if (strcmp(fileName, "sailing") == 0) 
    			{
    				int returnCode = ReadImage(fileName, imageR, imageG, imageB);
    			}
    			break;
    		case 2: 
    			printf("please enter the file name to be saved \n");
    			scanf("%s",fileName);
    			break;
    		case 3:
    			printf("Black & White operation is done! \n");	
    			break;
    		}
    	}

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    Im getting all these errors =[

    Error 4 error C2062: type 'int' unexpected c:\users\kmoslehp\documents\visual studio 2010\projects\asdfsfsf\asdfsfsf\hw2.cpp 97
    Error 5 error C2143: syntax error : missing ';' before '{' c:\users\kmoslehp\documents\visual studio 2010\projects\asdfsfsf\asdfsfsf\hw2.cpp 98
    Error 6 error C2065: 'fname' : undeclared identifier c:\users\kmoslehp\documents\visual studio 2010\projects\asdfsfsf\asdfsfsf\hw2.cpp 106
    Error 7 error C2065: 'fname' : undeclared identifier c:\users\kmoslehp\documents\visual studio 2010\projects\asdfsfsf\asdfsfsf\hw2.cpp 111
    Error 8 error C2065: 'R' : undeclared identifier c:\users\kmoslehp\documents\visual studio 2010\projects\asdfsfsf\asdfsfsf\hw2.cpp 140
    Error 9 error C2065: 'G' : undeclared identifier c:\users\kmoslehp\documents\visual studio 2010\projects\asdfsfsf\asdfsfsf\hw2.cpp 141
    Error 10 error C2065: 'B' : undeclared identifier c:\users\kmoslehp\documents\visual studio 2010\projects\asdfsfsf\asdfsfsf\hw2.cpp 142
    Error 11 error C2601: 'SaveImage' : local function definitions are illegal c:\users\kmoslehp\documents\visual studio 2010\projects\asdfsfsf\asdfsfsf\hw2.cpp 155
    Error 12 error C1075: end of file found before the left brace '{' at 'c:\users\kmoslehp\documents\visual studio 2010\projects\asdfsfsf\asdfsfsf\hw2.cpp(51)' was matched c:\users\kmoslehp\documents\visual studio 2010\projects\asdfsfsf\asdfsfsf\hw2.cpp 207
    13 IntelliSense: expected 'while' c:\users\kmoslehp\documents\visual studio 2010\projects\asdfsfsf\asdfsfsf\hw2.cpp 97
    14 IntelliSense: expected a '(' c:\users\kmoslehp\documents\visual studio 2010\projects\asdfsfsf\asdfsfsf\hw2.cpp 151

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kkmoslehpour View Post
    Im getting all these errors =
    Now is as good a time as any to start learning how to fix your own problems.

    "But how do I do that!? Programming is so HARRRDDDDD!!

    How about taking one at a time?

    "But what one there are so manyyyyy?!!!"

    Just like your compiler, take the top one and work your way down. Fix one, compile. Repeat until done:
    Quote Originally Posted by kkmoslehpour View Post
    Error 4 error C2062: type 'int' unexpected c:\users\kmoslehp\documents\visual studio 2010\projects\asdfsfsf\asdfsfsf\hw2.cpp 97
    Go hang out around line 97 and see what you've done wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    I know i can read it, the only problem is I dont know what to change to fix the problem, since my code is so long..... Im afraid if i change something something else would go wrong...

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch statement
    By xniinja in forum C Programming
    Replies: 1
    Last Post: 07-13-2010, 04:07 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. Switch statement help
    By GeorgeV in forum C++ Programming
    Replies: 6
    Last Post: 08-07-2007, 03:35 AM
  4. After the switch statement?
    By cerin in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2005, 07:01 PM
  5. The Switch Statement
    By gozlan in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21