I'm taking the time from an accessor function and if it's from 0-9, so it displays correctly the hour time position, I have the following statements in my main body:

if (clock1.getHour() == 00)
    cout<<"The hour of clock1 is 00"<<endl;
    
    else if (clock1.getHour() == 01)
    cout<<"The hour of clock1 is 01"<<endl;
    
    else if (clock1.getHour() == 02)
    cout<<"The hour of clock1 is 03"<<endl;
    
  ...   ...  .... ...
    
    else if (clock1.getHour() == 08)
    cout<<"The hour of clock1 is 08"<<endl;
    
    else if(clock1.getHour() == 09)
    cout<<"The hour of clock1 is 09"<<endl;

    else 
    cout<<"The hour of clock1 is "<<clock1.getHour()<<endl;

This works great and it displays, except when I get to 8 and 9. I get two errors, invalid digit "8" & "9" in octal constant. They also don't appear in purple as the rest of the ints do in else if statements, just black.

The syntax is exactly the same as the rest of the statements. I don't get why it would all of the sudden act up now. If I comment them out, it runs.

Thanks.

Recommended Answers

All 5 Replies

I'm taking the time from an accessor function and if it's from 0-9, so it displays correctly the hour time position, I have the following statements in my main body:

if (clock1.getHour() == 00)
    cout<<"The hour of clock1 is 00"<<endl;
    
    else if (clock1.getHour() == 01)
    cout<<"The hour of clock1 is 01"<<endl;
    
    else if (clock1.getHour() == 02)
    cout<<"The hour of clock1 is 03"<<endl;
    
  ...   ...  .... ...
    
    else if (clock1.getHour() == 08)
    cout<<"The hour of clock1 is 08"<<endl;
    
    else if(clock1.getHour() == 09)
    cout<<"The hour of clock1 is 09"<<endl;

    else 
    cout<<"The hour of clock1 is "<<clock1.getHour()<<endl;

This works great and it displays, except when I get to 8 and 9. I get two errors, invalid digit "8" & "9" in octal constant. They also don't appear in purple as the rest of the ints do in else if statements, just black.

The syntax is exactly the same as the rest of the statements. I don't get why it would all of the sudden act up now. If I comment them out, it runs.

Thanks.

Well if it's an octal constant, then the only valid digits are 0 though 7, so O8 is illegal since 8 isn't an octal digit. Do you want octal constants? Is that a typo : is it supposed to be the number 0 rather than the letter O, which stands for Octal base in this context? Why not just make it:

else if (clock1.getHour() == 8)

If you want the above statement in octal, i guess it would be:

else if (clock1.getHour() == O10)

since O10 is the equivalent of 8. Do you have the letter O in there on purpose?

No, all of the 0's in the lines are all 0's, I double checked. At no time did I declare octal constants. My goal is basically to get it to print out 08, 09 for the hour parameter without converting it to a string.

Thanks though, I made 010 for 8 and 011 for 9 and is correctly displaying 08 and 09 for the hour position.

No, all of the 0's in the lines are all 0's, I double checked. At no time did I declare octal constants. My goal is basically to get it to print out 08, 09 for the hour parameter without converting it to a string.

Thanks though, I made 010 for 8 and 011 for 9 and is correctly displaying 08 and 09 for the hour position.

Hmm, I just cut and pasted your code again and you're right, they are zeroes in your code, not the letter O. Maybe I copied and pasted wrong last time or typed in the letter O without realizing it last time. I'm not sure. The error suggests that there was a letter O in there somewhere, not a zero. I don't know how you would get the error otherwise and if you go back and forth changing from the letter O o a zero and vice versa, it will compile successfully or not based on that. Not sure what the deal is, but if it works now, great.

In C/C++, octal numbers are prefixed with 0 (zero), in the same manner as hexadecimal numbers are prefixed with 0x (zero, ex).

Using the letter O (oh) does not specify an octal digit, but will be an (undefined) variable name.

I believe the answer to the OP's question is to get rid of all zeros preceeding any number, so that it is read as decimal and not octal.

In C/C++, octal numbers are prefixed with 0 (zero), in the same manner as hexadecimal numbers are prefixed with 0x (zero, ex).

Using the letter O (oh) does not specify an octal digit, but will be an (undefined) variable name.

I believe the answer to the OP's question is to get rid of all zeros preceeding any number, so that it is read as decimal and not octal.

You're right dougy. I'm embarrassed. I forgot that and wasn't really paying attention and posted and got the letter O and number 0 completely backwards! Twice!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.