Firstly, this is my assignment:
Parsing is the act of converting a sequence of characters into a regular program data value. In this exercise you will write a program that parses a sequence of characters representing an integer, into an int value. For example, your program will convert the characters
1 2 9
into the int value 129.
A strict parser requires that the syntax for data items be followed precisely. A permissive parser, on the other hand, ignores minor errors or idiosyncracies in the input.
Your parser should be permissive; it should ignore commas and periods in the input. This is because, in North America, people often write large integers with commas. 3,102 means the same integer as 3102. In Europe people use periods between digits; 5.201.301 means the same thing as 5201301 and also 5,201,301.
We will use the semicolon character (';') to indicate the end of an integer value.
Your program should:
Read the individual characters into a partially-filled array of char elements.
Stop reading characters when a ; is encountered.
Convert valid base-10 digits (i.e. 0 through 9) into the corresponding number. Ignore any comma (',') or period ('.') characters that may be present.
Print out the number.
Print out the number multiplied by 2. (This checks that you really did convert the number to an int and aren't just printing the digits back).
Note that you will have to enter spaces or newlines in between the individual characters.
Before you start programming, take a few minutes plan your approach and sketch out a structure chart.
Example input/output
Enter digits: 1 2 3 4 ;
Number: 1234, multiplied by two: 2468
Enter digits: 4 , 1 0 9 ;
Number: 4109, multiplied by two: 8218
Enter digits: 1 . 2 3 1 ;
Number: 1231, multiplied by two: 2462
Enter digits: 0 ;
Press any key to continue...
Hint: converting digits to numbers
The following algorithm converts a sequence of base-10 digits into a single int value. It assumes that the digits are supplied in left-to-right order; in other words, the most significant digit comes first.
value = 0
for each digit d (d is a character '0', '1', ... '9'):
value = value * 10
if d is '9' then increase value by 9
if d is '8' then increase value by 8
...
if d is '1' then increase value by 1
if d is '0' then do nothing
Hint: this would be a good place to use a switch statement.
So far I am just trying to make the switch statement and need to know if this will work?
int value=0;
switch(digit)
{
case '9': value=value+9;
case '8': value=value+8;
case '7': value=value+8;
case '6': value=value+6;
case '5': value=value+5;
case '4': value=value+4;
case '3': value=value+3;
case '2': value=value+2;
case '1': value=value+1;
case '0': value=value;
case ';': value=value;
break;
default:
break;
}