Help with a Lab C++

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...
here what you are looking for:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <math.h>
using namespace std;
int length(char e[]){
	int s=0;
	for (int q=0;q<=100;q++)
	{if (e[q]==0)
	{s=q;break;}}
	return s;}
void main(){
	char x[32],m[32];int u,w,i;int y;
	cout<<"enter digits :";
	cin>>m;i=0;
	while (m!=0){u=0;y=0;
	w=length(m);
	for (i=0;i<w;i++)
	{if (m[i]=='.') continue;
	x[u]=m[i];u++;}
	for (i=0;i<u;i++)
	{y=y+((x[i]-48)*(pow(10,(u-1-i))));}
	cout<<"multiplied by 2 : "<<y*2;cout<<"\nenter digits: ";cin>>m;}}
Last edited on
Thanks, I actually had to use a switch statement, forgot to mention that, but here is my final code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
using namespace 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;
}
Topic archived. No new replies allowed.