Thread: If statement not working!

  1. #1
    Unregistered
    Guest

    Unhappy If statement not working!

    Can someone help me with this please? I was programming some crap today and it just wasnt working.

    Code:
    <stdlib.h>
    <iostream.h>
    
    char Countrys[10];
    char Country1[10] = "Aus";
    
    char Dogs[10];
    char Dog1[10] = "Labrodoar";
    
    int Cost;
    int People = 12;
    
    
    void main(void)
    {
    Countrys = Country1;
    Dogs = Dog1;
    
    if (Countrys == Country1 && Dogs == Dogs1) // Both of which do!
    {
    Cost = People;
    }
    
    
    cout << Cost;
    
    }

    Now this should work and there are no errors in it but when the screen shows the "Cost" it is still at 0.

    Any thoughts?

    Paul

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    you must use strcmp to compare string and strcpy to copy strings

  3. #3
    Unregistered
    Guest
    This will probably crack you up so please dont laugh too hard at me.

    Where do i write that? Up the top like <strcmp.h> or somewhere else?

    Paul

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    No - #include <string.h>

    And then, for example

    strcmp(myString, thatString);

  5. #5
    Unregistered
    Guest
    Ok ok, heres what i did thanks to your advice:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <string.h>
    
    void main()
    {
    char Fifty[20];
    char Forty[20] = "Bugs";
    strcmp(Fifty, Forty);
    cout << Fifty;
    }
    All i got was a bunch of pretty ASCII letters and pictures.
    The strcpy worked though, when i substituted that for strcmp.

    But how do I say

    if (Fifty == Forty)
    {
    do something
    }


    Paul

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    Code:
    if (strcmp(stringOne,stringTwo)==0) {
        cout << "stringOne equals stringTwo";
    }
    this line of code compares stringTwo to stringOne. if these two strings are equal, strcmp() will return 0. in this case we are testing for 0, which means they are equal, and the cout statement executes.
    hope that helps you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 12:41 PM
  3. crazy if statement
    By jamespond88 in forum C Programming
    Replies: 26
    Last Post: 03-19-2009, 04:02 PM
  4. Max/min function not working correctly
    By En-Motion in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2009, 12:28 AM
  5. Replies: 1
    Last Post: 08-31-2004, 04:07 AM