I have to do a lab for my intro to programming class and I was hoping you guys could help me get started?
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).
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...
#include <iostream>
usingnamespace std;
int main()
{
char i;
char d[10];
char n = 10;
int value = 0;
cout<<"Enter digits:"<<endl;
for (i=0; i<n; i++)
{
cin>>d[i];
if(d[i] == ';')
break;
}
for (i=0; i<n; i++)
switch (d[i])
{
case'0':
value *= 10;
break;
case'1':
value += 1;
value = value * 10;
break;
case'2':
value += 2;
value = value * 10;
break;
case'3':
value += 3;
value = value * 10;
break;
case'4':
value += 4;
value = value * 10;
break;
case'5':
value += 5;
value = value * 10;
break;
case'6':
value += 6;
value = value * 10;
break;
case'7':
value += 7;
value = value * 10;
break;
case'8':
value += 8;
value = value * 10;
break;
case'9':
value += 9;
value = value * 10;
break;
}
value /= 10;
cout << "number: " << value << endl;
cout << "Multiplied by two: "<< value * 2 << endl;
return 0;
}