Coding Woes: Program on the Fritz

This is a discussion on Coding Woes: Program on the Fritz within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <math.h> void main (void) { int m,k,num,x[50]; for(m=1;m<=6;m++) { printf("Enter the number of numbers to be ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    Coding Woes: Program on the Fritz

    Code:
    #include <stdio.h>
    #include <math.h>
    void main (void)
    {
    int m,k,num,x[50];
    for(m=1;m<=6;m++)
        {
    
               printf("Enter the number of numbers to be entered\n");
    
               scanf("%d",&num);
    
               printf("\nNow scan in those numbers\n");
    
        	for(k=1;k<=num;k++);
          	{
    
    	      scanf("%d",&x[k]);
    
       	}
               printf("\n");
    
                    for (k=1;k<num;k++)
         	{
    
         	 if(x[k+1]==x[k]*x[k])goto square;
    	 else goto cool;
    
          	}
               square:printf("square\n");
               cool:printf("cool\n\n");
    
         }
    }
    The object of the program is to A) scan in the total numbers you are entering. B) Enter the numbers, C) Check to see if the next number is the square of the previous.
    Ex/ Enter the amount of numbers: 3

    Enter the numbers: 3 9 81

    If they are the sequential square of the previous, then it will print the word square, and start the program over for a new set of data.

    When it is done, it will have a list of words, "Square" if the entries follow the parameters, "cool" if they do not.

    The problem I am having is, when it goes to check if it follows the parameters, the code doesnt work.

    OUTPUT:

    Code:
     Enter the number of numbers to be entered
    3
    
    Now scan in those numbers
    3 9 81
    
    cool
    
    Enter the number of numbers to be entered
    
    Now scan in those numbers
    
    cool
    
    Enter the number of numbers to be entered
    FOr some reason, it prints enter the numbers to be entered once, prints now scan in those numbers, cool, before printing "enter the number of numbers to be entered" and allowing me to scan in the next data set.

    After tracing the program, I cant make heads or tails of where I am going wrong, and why I am getting multiple instances of those print lines

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    > void main (void)
    main returns an int

    > for(k=1;k<=num;k++);
    Watch that ; at the end of the line, it makes this loop do nothing.
    The block of code which follows it gets executed just the once, with the final value of k

    > if(x[k+1]==x[k]*x[k])goto square;
    There's no reason for gotos in such a short and simple program.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    We have always been taught, when writing programs, to use:

    Code:
    #include <stdio.h>
    void main (void)
    {
    
    }

    How else would you write in the main function/

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        int m,k,num,x[50];
        
        for(m=1;m<=6;m++)
        {
               printf("Enter the number of numbers to be entered\n");
               scanf("&#37;d",&num);   
    
               printf("\nNow scan in those numbers\n");
        	   
               for(k=1;k<=num;k++)
                  scanf("%d",&x[k]);   
               printf("\n");
    
               for(k=1;k<num;k++)
               {
         	       if(x[k+1]== (x[k]*x[k]))
                      printf("square\n");
                   else 
                      printf("cool\n\n");
               }
        }
        getchar();
        return 0;
    }
    ssharish2005
    Last edited by ssharish2005; 04-26-2007 at 10:25 AM.

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by astrodude15 View Post
    We have always been taught, when writing programs, to use:

    Code:
    #include <stdio.h>
    void main (void)
    {
    
    }

    How else would you write in the main function/
    main returns an int, that's why you have to write:
    Code:
    #include <stdio.h>
    int main ()
    {
    
    }
    and if you search this forum, you'll find a thread somewhere that says why it's better not to specify void inside the parameter list.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by astrodude15 View Post
    We have always been taught, when writing programs, to use:

    Code:
    #include <stdio.h>
    void main (void)
    {
     
    }

    How else would you write in the main function/
    Understood that you have been taught by that way, but u will have get used to including int. Some teacher still follow that way which they shouldn't. Read FAQ on why the main should return an int. For brief, without return anythng from main the OS will never know wheather the program terminted successfull or failuer. by returing a value from main we specify the exit status of the program where return 0 specifies that main exited successfully return 1 specifies faulier.

    Read on int main()

    ssharish2005

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    and if you search this forum, you'll find a thread somewhere that says why it's better not to specify void inside the parameter list.
    This is C, so I believe that is fine.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > and if you search this forum, you'll find a thread somewhere that says why it's better not to specify void inside the parameter list.

    If you're talking about this thread

    i need help with sorting an array

    the conclusion was that either "int main(void)" or int main()" will work, although the C99 Standard explicitly mentions only the former. I'm guessing that this is because they considered it good practice to always be explicit in C when declaring a function taking no arguments.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    To be compliant with C99, I would say that void should be the parameter accepted in main() if you wish to ignore the parameters. In reality, most compilers will probably adapt to it receiving nothing, but I wouldn't count on it.

    I suppose it could be argued that defining main() always to receive argc and argv is probably just good practice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. coding clever program
    By rogster001 in forum Game Programming
    Replies: 1
    Last Post: 12-20-2007, 05:52 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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