C Programming help

Discussion in 'OT Technology' started by Smitty, Feb 14, 2006.

  1. Smitty

    Smitty New Member

    Joined:
    May 9, 2002
    Messages:
    56,538
    Likes Received:
    0
    Location:
    Charleston, SC
    I have tried a bunch of different things but I keep getting different errors when I try to build it. Basically its a coin flip program:
    Code:
    #include <iostream.h>
    
    #include <stdlib.h>
    
    //#include <math>
    
    //#include <string>
    
    int flip()
    	{
    		return Rand() % 2;
    	}
    
    int main()
    {
    	int count_H = 0;
    	int count_T = 0;
    
    	for (int i = 0; i < 100; i++)
    	
    	{
    		if (flip() == 0)
    			count_H++;
    		else (flip() == 1)
    			count_T++;
    	}
    	cout << "Heads flipped " << count_H << endl;
    	cout << "Tails flipped " << count_T << endl;
    	return 0;
    }
    Im still really shady on this stuff so forgive my noobness :hs:
     
    Last edited: Feb 14, 2006
  2. Smitty

    Smitty New Member

    Joined:
    May 9, 2002
    Messages:
    56,538
    Likes Received:
    0
    Location:
    Charleston, SC
    Here are the errors Im getting:

    warning C4518: 'int ' : storage-class or type specifier(s) unexpected here; ignored
    error C2146: syntax error : missing ';' before identifier 'flip'
    fatal error C1004: unexpected end of file found
     
  3. antiyou

    antiyou OT Supporter

    Joined:
    Jul 13, 2005
    Messages:
    25,295
    Likes Received:
    0
    Location:
    in ur base
    1. you can't declare functions within a function
    2. use code tags
    3. move int main() down 6 lines
     
  4. Scooby

    Scooby Growing up too fast...

    Joined:
    Jul 31, 2000
    Messages:
    39,062
    Likes Received:
    0
    Location:
    virginia
    ok i'm here
     
  5. Scooby

    Scooby Growing up too fast...

    Joined:
    Jul 31, 2000
    Messages:
    39,062
    Likes Received:
    0
    Location:
    virginia
    whats the errors now?
     
  6. Smitty

    Smitty New Member

    Joined:
    May 9, 2002
    Messages:
    56,538
    Likes Received:
    0
    Location:
    Charleston, SC
    what? :o


    code tags? I dont think we have done those yet...


    and moving int main down gave me 7 more errors
     
  7. antiyou

    antiyou OT Supporter

    Joined:
    Jul 13, 2005
    Messages:
    25,295
    Likes Received:
    0
    Location:
    in ur base
    when you enter code use [ code ] [ /code ](without the spaces)

    It gave you 7 more errors because it wasn't compiling down that far before. It was getting to the part where you declare a function within a function then stopping.

    you're missing a ; after return 0;
     
  8. Smitty

    Smitty New Member

    Joined:
    May 9, 2002
    Messages:
    56,538
    Likes Received:
    0
    Location:
    Charleston, SC
    fixed the ; but still have the same errors :hs:

    cts\CIS175\coinflip.cpp(11) : warning C4518: 'int ' : storage-class or type specifier(s) unexpected here; ignored
    C:\Program Files\Microsoft Visual Studio\MyProjects\CIS175\coinflip.cpp(11) : error C2146: syntax error : missing ';' before identifier 'flip'
    C:\Program Files\Microsoft Visual Studio\MyProjects\CIS175\coinflip.cpp(11) : fatal error C1004: unexpected end of file found
     
  9. Scooby

    Scooby Growing up too fast...

    Joined:
    Jul 31, 2000
    Messages:
    39,062
    Likes Received:
    0
    Location:
    virginia
    can you post the updated code?
     
  10. Smitty

    Smitty New Member

    Joined:
    May 9, 2002
    Messages:
    56,538
    Likes Received:
    0
    Location:
    Charleston, SC
    I really have never used that code stuff on anything except OT :o I dont know if we are supposed to do that.... we havent gone over that in class though.
     
  11. Smitty

    Smitty New Member

    Joined:
    May 9, 2002
    Messages:
    56,538
    Likes Received:
    0
    Location:
    Charleston, SC
    #include <iostream.h>

    #include <stdlib.h>

    //#include <math>

    //#include <string>

    int main()

    int flip()
    {
    return Rand() % 2;
    }



    {
    int count_H = 0;
    int count_T = 0;

    for (int i = 0; i < 100; i++)

    {
    if (flip() == 0)
    count_H++;
    else (flip() == 1)
    count_T++;
    }
    cout << "Heads flipped " << count_H << endl;
    cout << "Tails flipped " << count_T << endl;
    return 0;
    }
     
  12. Scooby

    Scooby Growing up too fast...

    Joined:
    Jul 31, 2000
    Messages:
    39,062
    Likes Received:
    0
    Location:
    virginia
    you didn't move the function outside the main?
     
  13. Smitty

    Smitty New Member

    Joined:
    May 9, 2002
    Messages:
    56,538
    Likes Received:
    0
    Location:
    Charleston, SC
    C:\Program Files\Microsoft Visual Studio\MyProjects\CIS175\coinflip.cpp(11) : error C2065: 'Rand' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\CIS175\coinflip.cpp(26) : error C2146: syntax error : missing ';' before identifier 'count_T'

    did it again and got that

    EDIT: Updating first post with each change :hs:
     
  14. antiyou

    antiyou OT Supporter

    Joined:
    Jul 13, 2005
    Messages:
    25,295
    Likes Received:
    0
    Location:
    in ur base
    1. FOLLOW THE DIRECTIONS when you enter code on OT use code tags not in your actual code.

    2. I said more int main down 6 lines just over top of the { after the flip function.

    3. You cannot declare a function within a function if you don't understand that statement. Change majors now.
     
  15. Scooby

    Scooby Growing up too fast...

    Joined:
    Jul 31, 2000
    Messages:
    39,062
    Likes Received:
    0
    Location:
    virginia
    crap.. hold on

    this is what your for loop should look like, try that

    Code:
    for (int i = 0; i < 100; i++)
    {
    if (flip() == 0)
          count_H++;
    else {
            if (flip() == 1)
               count_T++;
           }
    }
    
     
  16. Smitty

    Smitty New Member

    Joined:
    May 9, 2002
    Messages:
    56,538
    Likes Received:
    0
    Location:
    Charleston, SC
    :o finally got what you meant about the code thing, my bad man.

    and quit being mean :mamoru:
     
  17. Scooby

    Scooby Growing up too fast...

    Joined:
    Jul 31, 2000
    Messages:
    39,062
    Likes Received:
    0
    Location:
    virginia
    it should be Rand()%2

    no spaces
     
  18. Scooby

    Scooby Growing up too fast...

    Joined:
    Jul 31, 2000
    Messages:
    39,062
    Likes Received:
    0
    Location:
    virginia
    also its rand()%2

    lower case R

    not sure if that matters
     
  19. Smitty

    Smitty New Member

    Joined:
    May 9, 2002
    Messages:
    56,538
    Likes Received:
    0
    Location:
    Charleston, SC
    C:\Program Files\Microsoft Visual Studio\MyProjects\CIS175\coinflip.cpp(11) : error C2065: 'Rand' : undeclared identifier
    Error executing cl.exe.
     
  20. Smitty

    Smitty New Member

    Joined:
    May 9, 2002
    Messages:
    56,538
    Likes Received:
    0
    Location:
    Charleston, SC
    works :bowdown:
     
  21. Scooby

    Scooby Growing up too fast...

    Joined:
    Jul 31, 2000
    Messages:
    39,062
    Likes Received:
    0
    Location:
    virginia
    loop at my post above
     
  22. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    else (flip() == 1)

    should be else if (flip() == 1)

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    //#include <math>
    //#include <string>
    
    int flip()
    {
    	return rand() % 2;
    }
    
    int main()
    {
    	int count_H = 0;
    	int count_T = 0;
    
    	for (int i = 0; i < 100; i++)
    	{
    		if (flip() == 0)
    			count_H++;
    		else if (flip() == 0)
                                        count_T++;
    	}
    
    	cout << "Heads flipped " << count_H << endl;
    	cout << "Tails flipped " << count_T << endl;
    	
    	return 0;
    }
    
     
  23. Scooby

    Scooby Growing up too fast...

    Joined:
    Jul 31, 2000
    Messages:
    39,062
    Likes Received:
    0
    Location:
    virginia
    :bigthumb:
     
  24. Smitty

    Smitty New Member

    Joined:
    May 9, 2002
    Messages:
    56,538
    Likes Received:
    0
    Location:
    Charleston, SC
    Code:
    #include <iostream.h>
    
    #include <stdlib.h>
    
    #include <math.h>
    
    #include <string.h>
    
    int flip()
    	{
    		return rand()%2;
    	}
    
    
    int main()
    {
    	int count_H = 0;
    	int count_T = 0;
    
    	for (int i = 0; i < 100; i++)
    {
    if (flip() == 0)
          count_H++;
    else {
            if (flip() == 1)
               count_T++;
           }
    }
    	cout << "Heads flipped " << count_H << endl;
    	cout << "Tails flipped " << count_T << endl;
    	return 0;
    }
    
    
    
    final product that worked :cool:

    Appreciate both of you guys helping me :h5:
     
  25. Scooby

    Scooby Growing up too fast...

    Joined:
    Jul 31, 2000
    Messages:
    39,062
    Likes Received:
    0
    Location:
    virginia
    anytime man :)
     

Share This Page