Thread: Will not run.

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    Will not run.

    Hi, I am fairly new to the c language and I am working on a project for my class. When I run this program in cmd prompt, and enter my menu selection, the program errors out on me.

    Here is my code:
    Code:
    #include <stdio.h>
    
    
    void DisplayMenu();
    void Count10();
    void Countby2();
    void CountDown();
    int main()
    {
    	void DisplayMenu();
    	int menu;
    	do
    	{
    		DisplayMenu();
    printf ("Enter the number of the operatrion you would like to perform: ");
    		scanf ("%d", menu);
    		
    		switch (menu)
    		{
    		case 1: Count10(); break;
    		case 2:	Countby2(); break;
    		case 3:	CountDown(); break;
    		case 4:	printf ("Quitting the Program"); break;
    		default : printf ("Invalid Input\n\n"); break;
    		}
    		
    	}while (menu != 4);
    
    	
    	return 0;
    }
    //**************************************
    //**************************************
    void DisplayMenu()
    {
    	printf ("1. Count to 10\n");
    	printf ("2. Count to 100 by 2\n");
    	printf ("3. Countdown from 10\n");
    	printf ("4. Exit\n");
    }
    
    //**************************************
    //**************************************
    void Count10()
    {
    int i;
    	for(i=1;i<=10;i++)
    	{
    		printf ("%d", i);
    	}
    }
    
    //*************************************
    //*************************************
    void Countby2()
    {
    	int i=0;
    	while (i<=100)
    	{
    		printf ("%d\n", i);
    		i+=2;
    	}
    }
    //***********************************
    //***********************************
    void CountDown()
    {
    int f;
    	for(f=10;f>0;f--)
    	{
    		printf ("%d\n", f);
    	}
    
    }

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Code:
    int main()
    {
    	void DisplayMenu();
    Why are you declaring a function inside a function, given that you've already declared it before? I don't think that's needed.
    Code:
    		scanf ("%d", menu);
    WE HAVE A WINNNNNAAAARRR!!!!!
    scanf requires the address of variables to stick stuff into, so you want &menu, not menu. Plus, for bonus points, using scanf can be hazardous to your health, as if you were trying to read a string into menu it may be too long (scanf wouldn't know how long your string buffer is). So, in future, may I suggest:-
    Code:
    char temp[BUFSIZ];
    
    fgets(temp, BUFSIZ, stdin);
    sscanf(temp, "%d", &menu);
    That will save you many tears in the future, when you want more than just a number.

    EDIT: fgets does solve one or two other problems as well, such as flushing the input buffer, but that may be a bit beyond at the mo.
    Last edited by SMurf; 03-16-2006 at 04:29 PM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    well the thing is

    we havent learned most of that stuff u just said, we have been told to use scanf. Also, i stated that i was a noob so u dont need to be dick. if your going to be a dick, dont reply.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Vectorl33t
    if your going to be a dick, dont reply.
    You've got that slightly off. It's more like: If you're going to be a dick, at least make sure you're not wrong.

    Quote Originally Posted by SMurf
    Code:
    int main()
    {
    	void DisplayMenu();
    Why are you declaring a function inside a function, given that you've already declared it before? I don't think that's needed.
    It's not declaring a function within a function. (That would be a nested function.) It's a function prototype. You're allowed to prototype functions nearly anywhere you please, and it's still both legal, and allowed.


    Quzah.
    Last edited by quzah; 03-16-2006 at 05:16 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Quzah: The same thing also appears four lines above that snippet.

    Vectorl33t: I'd love to know in what way I've offended. I neither poked fun nor swore at you, and I wrote the words "in future" for a reason, i.e. that you don't need/know fgets at the moment, but you will.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Quote Originally Posted by SMurf
    WE HAVE A WINNNNNAAAARRR!!!!!
    Thats what he is being a dick about. If he was not, then sorry for me saying that as i must have read it wrong.
    Last edited by Vectorl33t; 03-16-2006 at 05:36 PM.

  7. #7
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Allow me to explain: the need for the address of menu to be passed as an argument to scanf was the correct answer to the implied question "Why won't this run?". Hence, as many a game show host would say, "We have a winner", although they usually shout it in the manner I tried to convey.

    I guess light-heartedness doesn't work here.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    lol, tis hard to detect sarcasm over typing

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    Anywho...

    ya, i got it running, now... i wanna run case 1 2 and 3 but this time with and inputting number from the user. So, what i did was add 3 new case statements. I cannot figure out how to run a loop with user inputted data, being i suck at programming. Here is what i added for one of my loops.
    Code:
     
    //*************************************
    //*************************************
    void Inputcountup()
    {
    int p;
    int g;
    	Printf ("Enter a number to count up to: ");
    	scanf ("%d", &p);
    		for(g=1;g<=p;g++)
    {
    			printf ("%d\n", g);
    }
    But when i run the program, it errors out again and says syntax error at the end of input. And i do not no what it means by this. and yes, before u ask i did include it at the top such as void inputcountup(); and it is also in my case statement and included in my menu as an option.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    Here is all my code so far

    Here is all my code so far. And it tells me syntax error before ')' token on line 87.
    Code:
    #include <stdio.h>
    //***************************
    //***************************
    void DisplayMenu();
    void Count10();
    void Countby2();
    void CountDown();
    void Inputcountup();
    int main()
    {
    	void DisplayMenu();
    	int menu;
    	do
    	{
    		DisplayMenu();
    		printf ("Enter the number of the operatrion you would like to perform: ");
    		scanf ("%d", &menu);		
    		switch (menu)
    		{
    		case 1 : Count10(); break;
    		case 2 : Countby2(); break;
    		case 3 : CountDown(); break;
    		case 4 : Inputcountup(); break;	
    		case 5 : printf ("Quitting the Program"); break;
    		default : printf ("Invalid Input\n\n"); break;
    		}
    		
    	}
    		while (menu != 4);	
    	return 0;
    }
    //**************************************
    //**************************************
    void DisplayMenu()
    {
    	printf ("1. Count to 10\n");
    	printf ("2. Count to 100 by 2\n");
    	printf ("3. Countdown from 10\n");
    	printf ("4. Input Count up\n");
    	printf ("5. Exit\n");
    }
    
    //**************************************
    //**************************************
    void Count10()
    {
    int i;
    	for(i=1;i<=10;i++)
    	{
    		printf ("%d\n", i);
    	}
    }
    //*************************************
    //*************************************
    void Countby2()
    {
    	int i=0;
    	while (i<=100)
    	{
    		printf ("%d\n", i);
    		i+=2;
    	}
    }
    //***********************************
    //***********************************
    void CountDown()
    {
    int f;
    	for(f=10;f>0;f--)
    	{
    		printf ("%d\n", f);
    	}
    	}
    //***********************************
    //***********************************
    void Inputcountup()
    {
    int p;
    int g;
    	Printf ("Enter a number to count up to: ");
    	scanf ("%d", &p);
    		for(g=1;g<=p;g++)
    {
    			printf ("%d\n", g);
    }
    }
    }

  11. #11
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Remove a bracket at the end. If you had formatted correctly that would be readily apparent.

    You will also need to decapitalize Printf in function Inputcountup

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by SMurf
    Quzah: The same thing also appears four lines above that snippet.
    What's your point? You can prototype a function as many times as you like, provided all prototypes are the same. It doesn't matter that it was four lines up. It's still valid to prototype a function within another funciton. Even if you prototype it outside that function.


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

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What's your point?
    I think the point was that while it's valid to prototype a function multiple times, it's unnessesary.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    But that wasn't the original point. The original point was that they were nesting functions. They weren't. They were prototyping, which as used, wasn't incorrect. Like I said, if you're pointing out "errors", at least make sure you're correct.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 05:32 PM
  2. how to run an exe command in c++ and get back the results?
    By mitilkhatoon in forum C++ Programming
    Replies: 5
    Last Post: 09-21-2006, 06:00 PM
  3. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 03:04 AM
  4. How I can Run exe file in C++
    By palang in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2006, 11:55 AM
  5. Replies: 2
    Last Post: 10-29-2002, 04:56 PM