C++ Inputs and ASCII

Kuberr

Daemon Poster
Messages
610
I am currently taking an Intro to C++ course at my local community college.

I have been coding a program, in which I have the following:

cout<<"Please enter in a three-digit number: ";
cin<<Number;

Later on, I have a section that says:

cout<<Number;

Most values work, except values such as 008 don't. The output states that the value of Number is 0. I asked my instructor, and he said that he chose that test data on purpose. He says it has something to do with ASCII. It seems like the C++ is reading the input in with some different method. So how can I fix this?
 
Yah. That was kind of by accident. It's a bit too late now, but I'd like to know anyways.

// Dennis Ai
// CSC133-GW
// Lab03.cpp
// This program will determine if the digits of a 3-digit number are odd or even.

#include <iostream.h>
#include <iomanip.h>

int main()
{
//local declarations
int Number, HDCalc, TDCalc, UDCalc, TDQuotient, HDQuotient;
int OddCounter, EvenCounter, ZeroCounter;

OddCounter = 0;
EvenCounter = 0;
ZeroCounter = 0;

cout<<"______________________________________________________________________"<<endl;
cout<<"Dennis Ai CSC133-GW Lab03ex.cpp 7/11/06"<<endl;
cout<<"______________________________________________________________________"<<endl;

cout<<"This program will calculate if the digits of a three-digit number are"<<endl;
cout<<"odd or even."<<endl;
//Imput digits
cout<<"Please enter your three-digit number: ";
cin>>Number;
//Determine unit digit's odd/even attribute
UDCalc = Number % 2;
if (UDCalc==1)
OddCounter++;
else if (UDCalc==0)
{
UDCalc = Number % 10;
if (UDCalc==0)
ZeroCounter++;
else EvenCounter++;
}
//Determine ten digit's odd/even attribute
TDCalc = Number / 10;
TDQuotient = TDCalc % 2;
if (TDQuotient==1)
OddCounter++;
else if (TDQuotient==0)
{
TDCalc = TDCalc % 10;
if (TDCalc==0)
ZeroCounter++;
else EvenCounter++;
}
//Determine hundred digit's odd/even attribute
HDCalc = Number / 100;
HDQuotient = HDCalc % 2;
if (HDQuotient==1)
OddCounter++;
else if (HDQuotient==0)
{
HDCalc = HDCalc % 10;
if (HDCalc==0)
ZeroCounter++;
else EvenCounter++;
}
//Rewrite three-digit number
cout<<"The three-digit number you entered was: "<<Number<<endl;
//Print out even/odd attribute
if (OddCounter==3)
cout<<"The number contains all odd digits.";
else if (EvenCounter==3)
cout<<"The number contains all even digits.";
else if (ZeroCounter==3)
cout<<"The number contains all zeroes.";
else if ((OddCounter==1) && (EvenCounter==1) && (ZeroCounter==1))
cout<<"The number contains odd, even, and zero digits.";
else if ((OddCounter>=1) && (ZeroCounter>=1))
cout<<"The number contains odd and zero digits.";
else if ((EvenCounter>=1) && (ZeroCounter>=1))
cout<<"The number contains even and zero digits.";
else if ((OddCounter>=1) && (EvenCounter>=1))
cout<<"The number contains both odd and even digits.";
cout<<OddCounter<<EvenCounter<<ZeroCounter;
//End main
return 0;
}
 
i cannot reproduce your problem...

the whole enter 3 digits 00x and get it to equal zero... i always get the value of the number i enter.

maybe you can explain to us how to get the error?
 
well i've tried 008 and 009 and other numbers with two zeros preceding it, still can't get the error.

i tried with the code you've supplied and just a simple cin >> var; cout << var; program, always comes out the correct value.
 
Back
Top Bottom