Thread: Problems with a simple coin flipping program

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    15

    Problems with a simple coin flipping program

    Hi,

    Although I have had a few years of experience with C programming, I still consider myself a fairly new user. This is because my debugging skills are primitive and I still can't seem to get around simple errors.

    I was writing a code to:
    - flip a coin a 1000 times
    - display "heads" or "tails" as each flip comes up
    - count the result of each flip, and display percentages of heads and tails when its done

    Although I initially wrote another code which worked fine, I decided to simplify the code. However, I get a few errors, which I cannot seem to get around.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    #define FLIPS 1000
    
    
    int main()
    {
    	int flips, result;
    	int head_count = 0;
    	float hpercent, tpercent;
    
    	srand((unsigned)time(NULL));
    
    	for(flips=0; flips<FLIPS; flips++)
    	{
    		result = rand() % 2;
    
    		if(result)
    		{
    			head_count += 1;
    			printf("Heads ");
    		}
    		else
    			printf("Tails ");
    		}
    	}
    	
    	hpercent = ((float)head_count / FLIPS) * 100;   /* The errors start here */
    	tpercent = 100.0 - hpercent;
    
    	printf("Heads: %.2f percent \n", hpercent);
    	printf("Tails: %.2f percent \n", tpercent);          /* And end here */
    
    	return(0);
    }
    These are the error messages:
    coinflip2.c:32: 'head_count' undeclared here <not in a function>
    coinflip2.c:32: initializer element is not a constant
    coinflip2.c:32: warning: data definition has no type or storage class
    coinflip2.c:33: initializer element is not a constant
    coinflip2.c:33: warning: data definition has no type or storage class
    coinflip2.c:35: parse error before string constant
    coinflip2.c:35: warning: data definition has no type or storage class
    coinflip2.c:36: parse error before string constant
    coinflip2.c:36: warning: data definition has no type or string constant

    If anybody has any suggestions, please feel free to help me out.

    Thanks,

    Thileepan

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look out for a missing opening brace:
    Code:
    		if(result)
    		{
    			head_count += 1;
    			printf("Heads ");
    		}
    		else
    			printf("Tails ");
    		}
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    omg...i can't believe i missed that!!!

    i was looking over this code for like the last hr or more....lol

    thanks a lot

    what an embarassment

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You're missing an opening brace in your "else" clause.

    Looks like you're using gcc. Always use -Wall when building. More warnings will be issued, and that's a good thing. -Wall in this case might have led you to the problem, although the pertinent warning isn't completely obvious. In any case, use -Wall. I can't stress this enough.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Hey sorry to be a pain once again,

    But I'm not quite too familiar with the -Wall concept. Can you please elaborate or refer me to a site or tutorial that provides an overview of this.

    And yes, I'm using gcc. Dev-C++ to be exact. But I was unable to figure out how the debugging works for this program, as I have tried performing breakpoint debugging, but was unable to activate it in the editor.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Hey vart,

    I was actually looking over that link you posted. I still don't know how to use the -Wall command when compiling, and what the purpose of it is.

    On the link, it just states that -Wall is a combination of such and such. Sorry, still a newbie.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    gcc -Wall coinflip2.c -o coinflip2

    I tried that...not working...am I close?

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    oh wait...it works...now I just gotta put some errors in there and see what it does

    thanks

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It bumps up the warning level. The compiler will issue for warnings for more things.
    Remember that good code is code with no warnings. Broken code is code with errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    "-W -Wall" gives you even more warnings, and "-W -Wall -ansi -pedantic" is even better, though the latter sometimes gives you warnings in GCC's own header files.

    Also, +=1 is the same as ++.
    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.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Thanks

    I've been trying to compile using Quincy 2005 (all these days, I was just using notepad and DOS with the Dev-C++ compiler) and I get the following error:

    Error: ISO C90 does not support 'long long'

    Has anyone experienced that and know a possible solution?

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Actually...the Compiling is fine....it's BUILD that is giving me the problem

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Error: ISO C90 does not support 'long long'
    There are two (three, if you count K&R) major revisions of the C language, commonly referred to as C90 and C99--there are also C89 and C95 but that's not especially relevant here. One of the changes that C99 made was the "long long" type, which C90 does not support. If you're using the -ansi flag with gcc, that instructs it to conform to C90 and the -pedantic flag causes gcc to be, well, pedantic.

    While it doesn't fully support C99, you can enable gcc's work-in-progress C99 support by replacing -ansi with -std=c99. This will allow "long long" to work. Different people will have different opinions on what standard to target, so it's really up to you. C90 has wider support, C99 has some nifty features.

    There's also GNU C, which is what you get when you don't specify a standard to use. GNU C is standard C plus some GNU extensions. Confusing enough yet?

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    lol

    Actually, I was going to say that I worked my way around the problem by changing one of the options in the Quincy compiler

    It specified for strict ISO compliance, so I just unchecked that box, and everything works fine

    But yea, the information you provided was very helpful in my understanding though. I'll definitely keep the -std=c99 command in mind for possible need in the near future.

    Thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-17-2006, 08:03 AM
  2. Mystery bug in simple program? - I am beginner
    By archie123 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2005, 07:23 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. Help with small simple program
    By Sonor in forum C Programming
    Replies: 5
    Last Post: 04-02-2005, 07:40 AM
  5. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM