Thread: void and menus

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    5

    void and menus

    ok.
    so im writing a program in C which contains a menu (list of choices) and when you type in 1, 2, 3, etc. you are asked and are able to do certain things.

    in 3 of the 4 choices however, something isnt just printed. something is actually calculated.

    so i wrote this program, but i keep getting a few "void value not ignored as it ought to be" errors, while compiling.

    or even crazier ones such as...
    /usr/ccs/bin/ld: Unsatisfied symbols:
    scan (first referenced in /var/tmp/ccsjiyBd.o) (code)
    changelength (first referenced in /var/tmp/ccsjiyBd.o) (code)
    volsphere (first referenced in /var/tmp/ccsjiyBd.o) (code)
    volcube (first referenced in /var/tmp/ccsjiyBd.o) (code)
    menu (first referenced in /var/tmp/ccsjiyBd.o) (code) .

    i *believe* this has something to do with the (void), and which cannot be used with calculations? if so, how do i fix it?

    if im totally off, anyone have any idea whats going on?

    thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It means you're doing something like this:
    Code:
    void foo( void )
    {
    }
    
    int main( void )
    {
        int x;
        x = foo( );
    
        return 0;
    }
    Here, you're trying to use the return value of foo, when in fact there is no return value. (That's what the void is. void functions don't return anything, so stop trying to assign it to something.)


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

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    5
    ok, thats what i thought. so i cant use void.

    but then what can i use? because the 'variable' (if you can call it that) for each menu subset has a calculation based on a previously asked value, or asks to change that original value...

    i cant use float or int right?

    so what *can* i use for something like this:

    Code:
    (...)
    float fvolsphere;
    ????? volumesphere();
    (...)
    int choice;
     while ((choice = menu()) != 4) {
     switch (choice) {
            (...)
            case 2: volsphere(); break;
            (...)
            }
     }
            return 0;
    
     (...)
    
    ????? volumesphere()
    { 
            fvolsphere = (4.0/3.0)*(M_PI)*(pow(flength,3));
            printf("The volume of the sphere is %f \n", fvolsphere);
    }
    what can go in the "??????"s ?

    thanks for all the help.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    by the look of it 'void' should be fine because i cant even see anywhere in this program that you are calling volumesphere();
    unless you mean this call:
    Code:
    case 2: volsphere(); break;
    but still in that case the return value is not checked or needed.
    Can you please post the rest of your code?

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    5
    alright alright, here's the code...
    it probably has its fair share of stupid mistakes, but i cant even get this thing to compile... (i changed it a lil from the previously and partially listed code)...
    maybe i have some stupid problem with the "{ }"s?
    thanks once again...

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int menu(void);
    float flength;
    float fvolsphere;
    float fvolcube;
    void changelength(void);
    void sphere(void);
    void cube(void);
    
    int main(void)
    {
    printf("Enter the length in meters: /n");
    scanf ("%f", &flength);
    
    if (flength < 0.0) {
    printf ("Hey! There's no such thing as a negative length!! /n");
    }
    
    int choice;
     while ((choice = menu()) != 4) {
     switch (choice) {
            case 1: changelength(); break;
            case 2: sphere(); break;
            case 3: cube(); break;
            default: printf("Invalid choice.\n");
            }
            return 0;
    }
    
    int menu(void)
    {
            int choice;
            printf("\n\nMenu\n");
            printf("1 - Change length value.\n");
            printf("2 - Compute and display volume of sphere of this diameter.\n");
            printf("3 - Compute and display volume of cube of this side length.\n>");
            printf("4 - Quit Program.\n>");
    
            scanf("%d",&choice);
            return choice;
    }
    
    void changelength(void)
    {
            printf("Enter the new length, in meters: /n");
            scan ("%f", &flength);
    
    void sphere(void)
    {
            fvolsphere = (4.0/3.0)*(M_PI)*(pow(flength,3));
            printf("The volume of the sphere is %f \n", fvolsphere);
    }
    
    void cube(void)
    {
            fvolcube = pow(flength,3);
            printf("The volume of the cube is %f \n", fvolcube);
    }
    exit (0);
    }

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    A few things:
    1) "while ((choice = menu()) != 4)" is missing its '}'
    2) '/n' is not a new line character - try '\n'
    3) changelength() is missing its '}'
    4) get rid of the "exit (0)" and the following '}' they are not in any function at the moment
    5) "int choice" should be declared at the begining of the function unless you are specifically using a C99 compiler - always safest to assume not.
    6) M_PI is not necessarily included in 'math.h' (again, maybe it is in C99...I can't remember)

    After I fixed these things it compiled. Try to use a consistent indentation style - you will pick up many of these problems far more easily. A couple more things:

    7) There is no reason for 'fvolsphere' or 'fvolcube' to be global.
    8) You could restructure the program to make 'flength' non-global as well. Global variables tend to be a symptom of sloppy code and should be avoided whenever possible - there is danger of unforseen side effects as your programs get more complex.
    Last edited by DavT; 10-19-2004 at 10:47 AM.
    DavT
    -----------------------------------------------

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    You need to indent stuff properly. If you do it you will see your mistakes. Especially with in larger project, indenting is a requirement, not an option.
    For now, here you go: (edit: meaning pointing out the mistakes, not indenting it for you )

    Code:
       #include <stdio.h>
       #include <math.h>
       // you need this for exit() (though shouldn't actually need exit())
        #include <stdlib.h>
        
       int menu(void);
       float flength;
       float fvolsphere;
       float fvolcube;
       void changelength(void);
       void sphere(void);
       void cube(void);
       
       int main(void)
       {
       	printf("Enter the length in meters: /n"); // find our what's wrong here on your own :)
       	scanf ("%f", &flength);
       
       	if (flength < 0.0) { // 1
       		printf ("Hey! There's no such thing as a negative length!! /n"); // same here
        	} // 1
       
       	// from the end of your code:
        	exit(0);
       }
       
     int menu(void)
       {
       		int choice;
       		printf("\n\nMenu\n");
       		printf("1 - Change length value.\n");
       		printf("2 - Compute and display volume of sphere of this diameter.\n");
       		printf("3 - Compute and display volume of cube of this side length.\n>");
       		printf("4 - Quit Program.\n>");
       
       		scanf("%d",&choice);
       		return choice;
       }
       
       int choice() { // this was supposed to be a function, right?
       	int choiceresult; // you cannot reuse a variable name as a function name.
     				 // especially not in the function itself
         while ((choiceresult = menu()) != 4) { // 2.
       	switch (choiceresult) {
       	  case 1: changelength(); break;
       	  case 2: sphere(); break;
       	  case 3: cube(); break;
       	  default: printf("Invalid choice.\n");
       	}
       
       	} // good morning, 2
         return 0; // assuming you don't want this in the loop as your intenting implies
       }
       
       void changelength(void)
       {
       		printf("Enter the new length, in meters: /n");
       		scanf ("%f", &flength); // it is scanf, not scan
       } // 2 and a half
       
       void sphere(void)
       {
       		fvolsphere = (4.0/3.0)*(M_PI)*(pow(flength,3));
       		printf("The volume of the sphere is %f \n", fvolsphere);
       }
       
       void cube(void)
       {
       		fvolcube = pow(flength,3);
       		printf("The volume of the cube is %f \n", fvolcube);
       }
       
       // now I see, you were trying to nest those functions in main(). Thats not possible.
        // moving this up
      
        //exit (0); 
        //}
    Last edited by Nyda; 10-19-2004 at 11:49 PM.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Nyda
    Code:
      #include <stdio.h>
      #include <math.h>
      // you need this for exit() (though shouldn't actually need exit())
       #include <stdlib.h>
       
      int menu(void);
      float flength;
      float fvolsphere;
      float fvolcube;
      void changelength(void);
      void sphere(void);
      void cube(void);
      
      int main(void)
      {
      	printf("Enter the length in meters: /n"); // find our what's wrong here on your own :)
      	scanf ("%f", &flength);
      
      	if (flength < 0.0) { // 1
      		printf ("Hey! There's no such thing as a negative length!! /n"); // same here
       	} // 1
        	// from the end of your code:
       	exit(0);
      }
      
      // moving this above choice() as you cannot use a function before you either
       // "forward" declare or actually introduce it
    Actually, you're wrong there. See the line I colored? That's called a function prototype. Since the function is prototyped before it is used, the function itself can be any place. It doesn't matter, because the compiler already knows about it, because we've prototyped it.

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

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    void changelength(void)
      {
      		printf("Enter the new length, in meters: /n");
      		scanf ("%f", &flength); // it is scanf, not scan
      }
    you should make your variables local

    Code:
    case 1: flength = changelength(); break;
    
    float changelength(void)
      {
          float flength;
          printf("Enter the new length, in meters: /n");
          scanf ("%f", &flength);
          return flength;
      }

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by quzah
    Actually, you're wrong there. See the line I colored?
    Thanks Quzah. You're of course right. I didn't see the prototype inbetween those globals (note to the OP: See where that style leads to? ;-) ). I also got a compiler error about menu() being unknown, but that was probably due to other bugs in the source.

    Quote Originally Posted by quzah
    That's called a function prototype.
    That is what I called a forward declaration. I recon that might not mean anything to C programmer, so I should have said prototype. I've corrected the post.

    If I may, let me blame the 80x25 terminal :-)
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to build a flexible Game Menu..
    By ThLstN in forum Game Programming
    Replies: 1
    Last Post: 12-13-2008, 10:53 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  4. Help with my c-program
    By duty11694 in forum C Programming
    Replies: 1
    Last Post: 11-15-2002, 02:13 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM