Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > octal error

Reply
Thread Tools

octal error

 
 
notahipee
Guest
Posts: n/a
 
      05-09-2008
I have been trying to cin an number from 0 to 9 with a leading 0. For
example 00 or 07. I was using a switch case.
switch (int)
{
case 01: break;
case 02: break;.....
My problem arises at 08 and 09 because they are not octal. Is there
any way to coerce the input variable to decimal despite it having a
leading zero .
 
Reply With Quote
 
 
 
 
notahipee
Guest
Posts: n/a
 
      05-09-2008
What I was trying to do looks like this:
#include <iostream>
#include <ostream>
int main()
{
int a;

cout << "enter a number " << endl;
cin >> a;
switch (a)
{
case 00: cout << zero;
case 01: cout << zero one;
etc.
}
Since a leading zero changes the number to octal it crashes at 08 and
09.
 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      05-09-2008
notahipee wrote:

> I have been trying to cin an number from 0 to 9 with a leading 0. For
> example 00 or 07. I was using a switch case.
> switch (int)
> {
> case 01: break;
> case 02: break;.....
> My problem arises at 08 and 09 because they are not octal. Is there
> any way to coerce the input variable to decimal despite it having a
> leading zero .


Please post complete, minimal code that demonstrates the problem.




Brian
 
Reply With Quote
 
notahipee
Guest
Posts: n/a
 
      05-09-2008
This is all of it.
#include <iostream>
#include <ostream>
int main()
{
int a;

cout << "enter a number " << endl;
cin >> a;
switch (a)
{
case 00: cout << zero;
case 01: cout << zero one;
case 02: cout << zero two;
case 03: cout << zero three;
case 04: cout << zero four;
case 05: cout << zero five;
case 06: cout << zero six;
case 07: cout << zero seven;
case 08: cout << zero eight;
case 09: cout << zero ninne;
}

return 0;
}
 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      05-09-2008

"notahipee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> What I was trying to do looks like this:
> #include <iostream>
> #include <ostream>
> int main()
> {
> int a;
>
> cout << "enter a number " << endl;
> cin >> a;
> switch (a)
> {
> case 00: cout << zero;
> case 01: cout << zero one;
> etc.
> }
> Since a leading zero changes the number to octal it crashes at 08 and
> 09.


How about using 010, 011 then ...?



regards
Andy Little



 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      05-09-2008
notahipee wrote:

> What I was trying to do looks like this:
> #include <iostream>
> #include <ostream>
> int main()
> {
> int a;
>
> cout << "enter a number " << endl;
> cin >> a;
> switch (a)
> {
> case 00: cout << zero;
> case 01: cout << zero one;
> etc.
> }
> Since a leading zero changes the number to octal it crashes at 08 and
> 09.


The simple answer is, "don't do that".

Why are you putting the leading zero in the case labels? What do you
think this achieves?



Brian
 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      05-09-2008

"Default User" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> notahipee wrote:
>
>> What I was trying to do looks like this:
>> #include <iostream>
>> #include <ostream>
>> int main()
>> {
>> int a;
>>
>> cout << "enter a number " << endl;
>> cin >> a;
>> switch (a)
>> {
>> case 00: cout << zero;
>> case 01: cout << zero one;
>> etc.
>> }
>> Since a leading zero changes the number to octal it crashes at 08 and
>> 09.

>
> The simple answer is, "don't do that".
>
> Why are you putting the leading zero in the case labels? What do you
> think this achieves?


Yep... octal is a bit "retro" these days... hexadecimal on the other hand is
useful

regards
Andy Little


 
Reply With Quote
 
notahipee
Guest
Posts: n/a
 
      05-09-2008
On many occasion people are asked to enter dates like 01/01/07 for Jan
first 1807 for example. So I want the leading 0. Also I can't have the
user entering octal but I have tried to get the int a; to be coreced
to from a decimal entry to octal for use by the switchcase. but \d
doesn't work. Unfortunately I am not sure how to apply cin.get or
cin.ignore properly.

 
Reply With Quote
 
notahipee
Guest
Posts: n/a
 
      05-09-2008
On May 9, 3:12*pm, notahipee <(E-Mail Removed)> wrote:
> On many occasion people are asked to enter dates like 01/01/07 for Jan
> first 1807 for example. So I want the leading 0. Also I can't have the
> user entering octal but I have tried to get the int a; to be coreced
> to from a decimal entry to octal for use by the switchcase. but \d
> doesn't work. Unfortunately I am not sure how to apply cin.get or
> cin.ignore properly.


I just got it to work. I placed above the switch case the following:
if (b>0 && b<=9)
 
Reply With Quote
 
notahipee
Guest
Posts: n/a
 
      05-09-2008
On May 9, 3:17*pm, notahipee <(E-Mail Removed)> wrote:
> On May 9, 3:12*pm, notahipee <(E-Mail Removed)> wrote:
>
> > On many occasion people are asked to enter dates like 01/01/07 for Jan
> > first 1807 for example. So I want the leading 0. Also I can't have the
> > user entering octal but I have tried to get the int a; to be coreced
> > to from a decimal entry to octal for use by the switchcase. but \d
> > doesn't work. Unfortunately I am not sure how to apply cin.get or
> > cin.ignore properly.

>
> I just got it to work. I placed above the switch case the following:
> if (b>0 && b<=9)


I forgot to say thank for the input.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting octal strings to unicode flamingivanova@gmail.com Python 2 12-24-2004 12:11 PM
reg exp and octal notation Lucas Branca Python 5 03-05-2004 04:33 PM
Reading out data from octal buffer Chris Online C++ 1 03-02-2004 11:05 AM
converting characters to octal Hostos Java 7 10-15-2003 06:07 AM
strange replacement of . and , with octal counterpart Kermit Lowry Perl 0 09-25-2003 11:04 AM



Advertisments