Thread: lessthen or equal 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
    28,413
    Well, that would work, if Votes_Needed and State_Two_Votes were say, int variables.
    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
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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,336
    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,817
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You have another problem: http://cboard.cprogramming.com/showp...37&postcount=9
    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, 05:26 AM
  2. Check that no numbers are equal
    By IdioticCreation in forum C++ Programming
    Replies: 4
    Last Post: 11-25-2006, 12:34 PM
  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, 02:33 AM