Thread: error

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    21

    error

    I seem to never be able to figure out the little things that are screwing up my programs... It is always something tiny... anyone have a clue?
    Code:
    #include <stdio.h>
    #include <iostream>
    
    main ()
    {
         int num, redctr, bluectr, brownctr, yellowctr, greenctr, purplectr;
         redctr = 0, bluectr = 0, brownctr = 0, yellowctr  = 0, greenctr = 0, purplectr = 0;
         
         printf("(Press 9 to stop voting.)\n\n");
         printf("The Many Colors of M&M's Candy\n");
         printf("1. red\n");
         printf("2. blue\n");     
         printf("3. brown\n");    
         printf("4. yellow\n");     
         printf("5. green\n");     
         printf("6. purple\n");     
    
         printf("\nVote on your favoriate color by pressing the number before it.\n");
         
    
         input: 
         printf("Enter a vote:\n\n");
         scanf("%i", &num);
    
         while (num >= 1 && num <= 6)
         {
              switch (num)
              {
              case 1:
                    printf ("\nYou voted for red.\n");
                    redctr = redctr + 1;
                    break;
              case 2:
                    printf ("\nYou voted for blue.\n");
                    bluectr = bluectr + 1;
                    break;
              case 3:
                    printf ("\nYou voted for brown.\n");
                    brownctr = brownctr + 1;
                    break;
              case 4:
                    printf ("\nYou voted for yellow.\n");
                    yellowctr = yellowctr + 1;
                    break;     
              case 5:
                    printf ("\nYou voted for green.\n");
                    greenctr = greenctr + 1;
                    break;
              case 6:
                    printf ("\nYou voted for purple\n");
                    purplectr = purplectr + 1;
                    break;
              }
              goto input;
         }
         
         if (num == 9)
         {
              goto finish;
         }
             
         printf ("\nYou have entered and invalid vote.  Please try again.\n");
         
         goto input;
         
         finish:
                
         printf("\nYou have completed voting.  The totals are:\n\n", num);
         printf("1. red          %i\n", redctr);
         printf("2. blue         %i\n", bluectr);     
         printf("3. brown        %i\n", brownctr);    
         printf("4. yellow       %i\n", yellowctr);     
         printf("5. green        %i\n", greenctr);     
         printf("6. purple       %i\n", purplectr);     
         printf("\n\n");
    
         system("pause");
    
         return 0;
         
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Whats your problem? Seemed to work fine for me after I removed this line:
    Code:
    #include <iostream>

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    There is absolutely no need for goto in this program. Use of goto usually results in hard-to-read code. Of course, I'd also replace your variables with an array of structs. Here's an example alternative (not tested):
    Code:
    #include <stdio.h>
    
    main ()
    {
         struct
         {
            char *name;
            int votes;
         } colors[] = { { "red", 0 }, { "blue", 0 }, { "brown", 0 }, { "yellow", 0 },
                        { "green", 0 }, { "purple", 0 } };
         int i;
    
         for(;;)
         {
           printf("(Press 9 to stop voting.)\n\n");
           printf("The Many Colors of M&M's Candy\n");
           for(i = 0;i < 6;++i)
             printf("&#37;d. %s\n", i + 1, colors[i].name);
    
           printf("\nVote on your favoriate color by pressing the number before it.\n");
         
    
           printf("Enter a vote:\n\n");
           scanf("%d", &num);
    
           if(num == 9)
             break;
    
           if(num >= 1 && num <= 6)
           {
             printf ("\nYou voted for %s.\n", colors[num - 1].name);
             colors[num - 1].votes++;
           }
           else
             printf ("\nYou have entered and invalid vote.  Please try again.\n");
         }
    
         printf("\nYou have completed voting.  The totals are:\n\n", num);
         for(i = 0;i < 6;++i)
           printf("%d. %13s %d\n", i + 1, colors[i].name, colors[i].votes);
         printf("\n\n");
    
         system("pause");
    
         return 0;
    }
    Last edited by itsme86; 07-16-2007 at 01:12 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    21

    i like the struct idea...

    Thanks for the pointer, i think i can make that work, and also the iostream thing is removed and it works peachy..

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by itsme86 View Post
    Code:
    main ()
    {
         //Some code
         return 0;
    }
    It should be int main() since it returned 0, right?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, always being explicit about your return types is a good thing.
    All of the old 'assume int' things have been removed in C99.
    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.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Moony View Post
    It should be int main() since it returned 0, right?
    Yeah, I just didn't change that from the OP's code.
    If you understand what you're doing, you're not learning anything.

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. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM