fixed :)

This is a discussion on fixed :) within the C Programming forums, part of the General Programming Boards category; Well after a few hours with it this morning I have fixed all the bugs and answered my own questions, ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38

    fixed :)

    Well after a few hours with it this morning I have fixed all the bugs and answered my own questions, here is the corrected code if anyone wants to see it.
    Code:
    #include <stdio.h>
    main()
    {
     int a;
     printf("1.FOR loop\n2.WHILE loop\n3.DO-WHILE loop\nSelect a loop to preform:");
     scanf("%d",&a);
     switch(a){
      case 1:
       FORloop();
       break;
      case 2:
       WHILEloop();
       break;
      case 3:
       DOWHILEloop();
       break;
      default:
       printf("Bad input, quitting.\n");
       break;
     }
     getchar();
    }
    FORloop()
    {
     int b;
     for(b=0;b<11;b++){
      printf("%d\n",b);
     }
    }
    WHILEloop()
    {
     int c=0;
     while(c<11){
     printf("%d\n",c);
     c++;
     }
    }
    DOWHILEloop()
    {
     int input,d=0;
     printf("1.Good loop\n2.Bad loop\nI'll explain later ;) pick one:");
     scanf("%d",&input);
     if(input==1){
      dwloop();
     }
     else if(input==2){
      do{
       printf("The explanation is in the source :)\n");
      }while(d!=0);  /*notice how it runs the "do" once even though the conditional statement is false*/
     } 
     else{
      printf("Bad input, quitting.\n");
     }
    }
    dwloop()
    {
     int e=0;
     do{
      printf("%d\n",e);
      e++;
     }while(e!=11);
    }

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38
    lol that was supposed to be the reply to what I thought was the thread I had posted this morning which was actually just the preview of the thread I was intending on posting.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38
    made it a little more effiecent
    Code:
    #include <stdio.h>
    main()
    {
     int a;
     printf("1.FOR loop\n2.WHILE loop\n3.DO-WHILE loop\nSelect a loop to preform:");
     scanf("%d",&a);
     switch(a){
      case 1:
       FORloop();
       break;
      case 2:
       WHILEloop();
       break;
      case 3:
       DOWHILEloop();
       break;
      default:
       printf("Bad input, quitting.\n");
       break;
     }
     getchar();
    }
    FORloop()
    {
     int b;
     for(b=0;b<11;b++){
      printf("%d\n",b);
     }
    }
    WHILEloop()
    {
     int c=0;
     while(c<11){
     printf("%d\n",c);
     c++;
     }
    }
    DOWHILEloop()
    {
     int input,d=0;
     printf("1.Good loop\n2.Bad loop\nI'll explain later ;) pick one:");
     scanf("%d",&input);
     if(input==1){
      do{
       printf("%d\n",d);
       d++;
      }while(d!=11);
     }
     else if(input==2){
      do{
       printf("The explanation is in the source ;)\n");
      }while(d!=0);  /*notice how it runs the "do" once even though the conditional statement is false*/
     } 
     else{
      printf("Bad input, quitting.\n");
     }
    }
    Last edited by elsheepo; 01-31-2009 at 10:25 AM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    As it is it's only working because a lack of return type is defaulting to int, and a missing function prototype is defaulting to a function taking no args and returning int. Functions should have return types and their prototype at least must appear before first use.

    At best this code will throw out a lot of warnings, and will probably often fail to compile, depending on the compiler and it's settings.

    Well done learning about loops though. Next, try learning to pass parameters to functions.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38
    Do you mean I should have
    Code:
    return 0;
    in each function as good practice?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. reading fixed length
    By Cpro in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2008, 03:12 PM
  3. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  4. Fixed point MOD (for sin LUT)
    By Pea in forum C Programming
    Replies: 0
    Last Post: 04-10-2005, 06:03 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

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