lessthen or equal to

This is a discussion on lessthen or equal to within the C Programming forums, part of the General Programming Boards category; im almost finished with my home work. i just need to know is there is a way for me to ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    43

    lessthen or equal to

    im almost finished with my home work. i just need to know is there is a way for me to do less then or equal to function for an 'if' statement.

    you lot probably understand what i mean but here's an example anyway...

    Code:
     if (Votes_Needed<=State_Two_Votes)
        {     
            printf("If %s can win %s then %s will win the election\n",Candidate, State_Two, Candidate);
            system("pause");
            return 0;
        }
    i was hoping that the '<= 'sign would work...it didn't =(
    and my help book on my programs sucks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Well, that would work, if Votes_Needed and State_Two_Votes were say, int variables.
    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

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If one or both are functions, you need to called the function with parentheses: ().

    Post the exact error you're getting -- it really helps people tell what is happening.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    ok im sorry i assumed that it was the sign that made the program crash. my program did compile but after the inputs it crashed. (windows error "program needs to close")

    here is the code for the hole thing...i though it was good =/

    Code:
    //John Torres
    //COP 3223-0002
    //Assignment #1.3
    //September, 4, 2008
    #include <stdio.h>
    int main(void)
    {
        char State_One[21];
        char State_Two[21];
        char Candidate[21];
        int State_One_Votes;
        int State_Two_Votes;
        int Both_States;
        int Votes_Won;
        int Votes_Needed;
    //input
        printf("Who is your candiate?\n(Obama or McCain for 2008)\n");
        scanf("%s",Candidate);
        printf("What is the name of the first state in question?\n");
        scanf("%s",&State_One);
        printf("What is the name of the second state?\n");
        scanf("%s",&State_Two);
        printf("How many votes are in %s?\n",State_One);
        scanf("%d",&State_One_Votes);
        printf("How many votes are in %s?\n",State_Two);
        scanf("%d",&State_Two_Votes);
        printf("How many votes does %s already have?\n",Candidate);
        scanf("%d",Votes_Won);
    //math
        Votes_Needed=270-Votes_Won;
        Both_States=State_One_Votes+State_Two_Votes;
    //outputs    
        if (Votes_Needed<1)
        {
            printf("%s has already won! Go party!\n",Candidate);
            system("pause");
            return 0;
        }
        if (Votes_Needed<=State_One_Votes)
        {
            if (Votes_Needed>State_Two_Votes)
            {
                printf("If %s can win %s then %s will win the election\n",Candidate, State_One, Candidate);
                system("pause");
                return 0;
            }   
            if (Votes_Needed<=State_Two_Votes)
            {
                printf("If %s can win %s or %s then %s will win the election\n",Candidate, State_One, State_Two, Candidate);        
                system("pause");
                return 0;
            }
        }
        if (Votes_Needed<=State_Two_Votes)
        {     
            printf("If %s can win %s then %s will win the election\n",Candidate, State_Two, Candidate);
            system("pause");
            return 0;
        }
        if (Votes_Needed<=Both_States)
        {
            printf("If %s can win %s and %s then %s will win the election\n",Candidate, State_One, State_Two, Candidate);
            system("pause");
            return 0;
        }
        if (Votes_Needed>Both_States)
        {
            printf("%s cannot win.\n",Candidate);
            system("pause");
            return 0;
        }
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    scanf needs you to use & if the thing you are reading into is NOT a pointer. Since State_One, State_Two, and Candidate are given to scanf as pointers (pointing the front of the array), then you do NOT need & here. Since Votes_Won is NOT a pointer, you need &.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    You forgot something here:
    Code:
    printf("How many votes does %s already have?\n",Candidate);
    scanf("%d",&Votes_Won);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    ...
    i cant believe i missed that. sorry i wasted your time. =/

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    and thanx!

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    and i was so happy when it compiled my first try too.
    anyway it works and does what i want now.
    Thanx alot!

  10. #10
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    as an aside, if you had used an incorrect symbol you would have received a compiler error.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    ok thanx

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    You have another problem: Need help with altering info on a txt file
    And use getchar() instead of system("pause") is you can. The latter is unportable.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Equal compare
    By George2 in forum C Programming
    Replies: 1
    Last Post: 11-10-2007, 04:26 AM
  2. Check that no numbers are equal
    By IdioticCreation in forum C++ Programming
    Replies: 4
    Last Post: 11-25-2006, 11:34 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. set string array equal variable
    By WaterNut in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2004, 05:02 PM
  5. how can 0.00665 not equal 0.00665??!!
    By Susan in forum C++ Programming
    Replies: 10
    Last Post: 02-10-2002, 01:33 AM

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