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

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kkmoslehpour View Post
    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...
    Stop whining and take the task as a challenge... It's a puzzle, you can solve it.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kkmoslehpour View Post
    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...
    Fortunately for us, programming isn't a one-chance deal. If it doesn't work, change it and recompile.


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

  3. #18
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by kkmoslehpour View Post
    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...
    And what can we learn from this? Never write large chunks of code without testing it in small steps! :-) Writing several hundred lines of code and then troubleshooting it is a lot more difficult and error prone than doing it in smaller portions.

    Please repost your code as it is now, along with the compiler errors so we can see which lines of code they correspond to. But I strongly suggest you look at the error messages yourself first, and try to work each one out. Try googling for the specific error messages, that can usually give you some good ideas if you are clueless as to what it means.

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > i usually code the whole thing then compile it, because i am afraid of errors and warnings....
    Well that's an absolutely GUARANTEED way of getting page after page after page of disheartening error messages that take ages to solve.
    You're doing it wrong!

    The compiler is your friend and guide for the journey, not some harsh gatekeeper preventing you from making progress.

    A development process
    If you only write a few lines and then press compile, then the most likely outcome is it either compiles or you get just a couple of errors.
    Two errors, plus fresh code is very easy to fix.

    100+ errors, plus "WTF was I thinking when I wrote that" code is far harder to fix.

    Also, when you've finished writing a section of code (say a menu + input), you actually RUN THE CODE to make sure it works. It might not seem like it to you, but getting code to compile is actually pretty damn easy. Getting it to do exactly what you want is the real trick.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    100+ errors, plus "WTF was I thinking when I wrote that" code is far harder to fix.
    Another reason I like Pelles C... it has a cute bit of humour built in... When it hits the MAX_ERRORS wall, it stops and prints out a warning: "More than 200 errors, please improver yourself" .... The first time I saw that I was right on the floor laughing!

    Compiler as teacher... ok.

  6. #21
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    okay, thanks, i fix the do while loop problem. But why am i getting these other errors, it is saying i didn't define my variable (error message 8~17), and all the other variables when it is defined. Also how do i have a function within a function. I searched up the error messages 4 and 5 and I dont know how I have local definitions.

    Error 4 error C2601: 'ReadImage' : local function definitions are illegal c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 110 1 asfsafasf
    Error 5 error C2601: 'SaveImage' : local function definitions are illegal c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 167 1 asfsafasf
    Error 6 error C1075: end of file found before the left brace '{' at 'c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfa sfasfas.cpp(51)' was matched c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 218 1 asfsafasf
    7 IntelliSense: expected a ';' c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 110 1 asfsafasf
    8 IntelliSense: identifier "Width" is undefined c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 132 6 asfsafasf
    9 IntelliSense: identifier "File" is undefined c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 136 9 asfsafasf
    10 IntelliSense: identifier "Height" is undefined c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 136 22 asfsafasf
    11 IntelliSense: identifier "MaxValue" is undefined c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 141 22 asfsafasf
    12 IntelliSense: identifier "y" is undefined c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 150 7 asfsafasf
    13 IntelliSense: identifier "x" is undefined c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 151 8 asfsafasf
    14 IntelliSense: identifier "R" is undefined c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 152 4 asfsafasf
    15 IntelliSense: identifier "G" is undefined c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 153 4 asfsafasf
    16 IntelliSense: identifier "B" is undefined c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 154 4 asfsafasf
    17 IntelliSense: identifier "fname_tmp" is undefined c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 160 40 asfsafasf

  7. #22
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    So i am getting red underlines for my width height fname.. etc but it is defined on top... i dont get it..

    Code:
    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;

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post your latest code.
    Quote Originally Posted by kkmoslehpour View Post
    Error 4 error C2601: 'ReadImage' : local function definitions are illegal c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 110 1 asfsafasf
    Error 5 error C2601: 'SaveImage' : local function definitions are illegal c:\users\dshang1\desktop\asfsafasf\asfsafasf\asfas fasfas.cpp 167 1 asfsafasf
    Sounds like you have a function inside a function, which you can't do:
    Code:
    void foo( void )
    {
        void bar( void )  /* you can't create a function inside a function */
        {
        }
    }
    Which is typically caused by something like:
    Code:
    void foo( void )
    {
        ...stuff for foo
    ...end of stuff for foo
    /* oops, forgot my } */
    
    void bar( void )  /* your compiler thinks this is in foo, because you for */
    {
        ...stuff for bar
    }

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

  9. #24
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    Quote Originally Posted by quzah View Post
    Post your latest code.Sounds like you have a function inside a function, which you can't do:
    Code:
    void foo( void )
    {
        void bar( void )  /* you can't create a function inside a function */
        {
        }
    }
    Which is typically caused by something like:
    Code:
    void foo( void )
    {
        ...stuff for foo
    ...end of stuff for foo
    /* oops, forgot my } */
    
    void bar( void )  /* your compiler thinks this is in foo, because you for */
    {
        ...stuff for bar
    }

    Quzah.

    Thanks so much !!

  10. #25
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    Everything is solved but now im getting this... unresolved path? Does this mean it can't located the file to load?



    Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup C:\Users\dshang1\Desktop\asfsafasf\asfsafasf\MSVCR TD.lib(crtexew.obj) asfsafasf
    Error 2 error LNK1120: 1 unresolved externals C:\Users\dshang1\Desktop\asfsafasf\Debug\asfsafasf .exe 1 1 asfsafasf
    Last edited by kkmoslehpour; 10-13-2011 at 04:25 PM.

  11. #26
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you actually have a main function in your program? You have to have one, it's where your program starts.


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

  12. #27
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    I have an int main ()
    when i do void main () what should i write in the main function to start the program

  13. #28
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by your errors
    Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup C:\Users\dshang1\Desktop\asfsafasf\asfsafasf\MSVCR TD.lib(crtexew.obj) asfsafasf
    You receive an "unresolved external symbol _WinMain@16" error message when you try to build an MFC application with Unicode support in Visual C++
    You receive an "error LNK2001:unresolved external symbol "_WinMain@16" message when a Console Application project is built as an Application project in Visual C++

    A couple of seconds and a search engine should set you straight.


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

  14. #29
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    thanks again! ill be coming with more questions!

  15. #30
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    here is my save image function (case 2), it builds successfully, but i get a message in the output saying that it cannot find the path specified


    Is this right?


    My code:
    Code:
    int main(void)
    {
    	int choice;
    	int done = 0;
    	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: ");
    		scanf("%d",&choice);
    	
    
    		switch(choice)
    		{
    		case 1: 
    			printf("Please input the file name to load: ");
    			scanf("%s",fileName);
    			if (strcmp(fileName, "sailing") == 0) 
    			{
    				int returnCode = ReadImage(fileName, imageR, imageG, imageB);
    			}
    			else
    			{
    				printf("Cannot open file ''%s'' for reading!\n\n",fileName);
    			}
    			break;
    
    		case 2: 
    			printf("please enter the file name to be saved: ");
    			scanf("%s",fileName);
    			{
    				int returnCode = SaveImage(fileName, imageR, imageG, imageB);
    			}
    			printf("''%s'' was saved successfully.",fileName);
    			printf("''%s'' was stored for viewing.",fileName);
    			break;
    
    		case 3:
    			printf("Black & White operation is done! \n");	
    			break;
    
    		case 10:
    			printf("Exit \n");
    			done = 1;
    			break;
    
    		default: 
    			printf("Invalid choice!\n");
                break;
    		}
    	}
    	while(!done);
    }
    Save Image Function(given):

    Code:
    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);
    }



    Instructions:

    1.3.2 Save a PPM Image
    This option prompts the user for the name of the target image file. You don't have to implement a file saving
    function; just use the provided one, SaveImage. Once option 2 is selected, the following is shown:
    Please make your choice: 2
    Please input the file name to save: bw
    bw.ppm was saved successfully.
    bw.jpg was stored for viewing.
    --------------------------------
    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
    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:
    The saved image will be automatically converted to a JPEG image and sent to the folder public html. You
    then are able to see the image at: http://newport.eecs.uci.edu/youruserid (For o campus, the link is:
    http://newport.eecs.uci.edu/youruserid/imagename.jpg)

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, 08:01 PM
  5. The Switch Statement
    By gozlan in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:44 AM