Thread: build error in If statement -- total newbie

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    North Carolina
    Posts
    2

    Question build error in If statement -- total newbie

    Hey all, new to the board and new to Cpp.

    I'm having a build error that has me stumped.
    What I am trying to do is write a header function that requests a date in MM/DD/YYYY format and returns three ints to the caller. But I get an error than I can't figure out in these lines:

    Code:
            iii=0; for (i=3; i<5; i++) {ii=i-1;
                if (date_c[ii] !(<"0" || >"9")) {dd_c[iii]=date_c[ii]; iii++;} else {error=1;}}
    
            ii=i; if (date_c[ii] !("/" || "-")) {error=1;}
    The error is "Expected ` ) ' before ' ! ' token". Note the " ` ".

    All of my braces, brackets, and parantheses seem to be in order.

    Date_c and dd_c are declared as char *date_c, etc. I tried including <string> and declaring them as strings, but that caused problems.

    Can anyone see what I cannot?
    TIA

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    First of all: i, ii, and iii as variables is very confusing. The convention is to use i, j, and k, if you need multiple indexing variables.

    Second, ! is not a binairy operator. you can't do "date_c[ii] !(...)". All ! does is make a true value evaluate to false, and a false one to true. It does not mean "is not". If you want to compare date_c[ii] to some constants then you have to write an expression like (data_c<constant1 || data_c >constant2 )

    Third, you cannot compare string literals to characters like that. Use character literals. These use single quotes.

    Fourth, you really should use std::strings. Figure out what the problems are and fix them. It will be easier then debugging problems with char *.

    Fifth, you should avoid long lines of code. Rarely is it a good idea to have multiple semicolons (excluding for loop first line) in the same line.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    Your if statements, to my eyes, are incorrect.

    Code:
    if (date_c[ii] !(<"0" || >"9"))
    What does this statement mean? Nothing to me. Perhaps you meant something like this:

    Code:
    if ( date_c[ii] < "0" || date_c[ii] > "9" ) {
        error_stuff_here();
    } else {
        not_error_stuff_here()
    }
    Same thing goes for your second if statement.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    North Carolina
    Posts
    2
    Thanks King and Zach!

    Your input helped me quite a bit.

    I changed back to std::string, reformatted the lines for easier reading, and took the advice from both of you. Compiles with no errors, now. Here's the change:

    Old code:
    Code:
            iii=0; for (i=3; i<5; i++) {ii=i-1;
                if (date_c[ii] !(<"0" || >"9")) {dd_c[iii]=date_c[ii]; iii++;} else {error=1;}}
    
            ii=i; if (date_c[ii] !("/" || "-")) {error=1;}





    New code:
    Code:
    for (i=3; i<5; i++)
            {j=i-1;
                if (date_s[j] >='0' || date_s[j] <='9')
                {dd_s[k]=date_s[j]; k++;}
                else {error=1;}
                }
    
            j=i;
            if (date_s[j] !='/' && date_s[j] !='-') {error=1;}

    Runs as a stand alone, but still won't output what I want. But that is something I want to try and solve on my own. Best way for me to learn, eh?

    Thanks again for your help.

    C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  2. Either my loop is bad or my function is messed up
    By crazychile in forum C Programming
    Replies: 25
    Last Post: 11-02-2008, 02:04 PM
  3. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  4. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM