Output spaced and non negative number

Ok i am having trouble getting my code to not output a - number. such as inputting -1234 and it will output 1 2 3 4 with no negative. I have to to where it will space them out but can't figure out where or how to make it not output the -.

here is the code i have for spacing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  using namespace std;

int main()
{
    int in;
    char ch[4];

    cout << "Please enter a 4 digit number: ";

    for(in=0;in<4;in++)
    {
        cin.get(ch[in]);
    }
    cout << "The single digit numbers are: " << ch[0] << " " << ch[1] << " " << ch[2] << " "
            << ch[3]  << endl;

    return 0;

}
Ok i know this is very ugly but i might be getting somewhere. but it will not recognize numbers only symbols. it will add them up but its adding the symbol values not the digits.

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
int main()
{

   int num, sum;
   char ch[4];

   cout << "Please enter a number: " << endl;
   cin >> num;

if (num<0,num<4,num++)
{


    cout << "The single digit numbers are: " << ch[0] << " " << ch[1] << " " << ch[2] << " "
            << ch[3]  << endl;


    sum = ch[0]+ch[1]+ch[2]+ch[3];

    cout << "The sum of the numbers is:" << sum <<  endl;
}

else if( num != 0)
{
    cout<< "Invalid"<< endl;
}


   return 0;
}


im trying to get it to separate the number given and also add them up
Last edited on
@bcav311

The only solution I could come up with, using your first program problem, was making the array to be ch[5]. As so..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
	int in;
	char ch[5];

	cout << "Please enter a 4 digit number: ";
	for (in = 0; in < 5; in++)
	{
		cin.get(ch[in]);
	}
	cout << "The single digit numbers are: ";
	if (ch[0] == '-')
		cout << ch[1] << " " << ch[2] << " " << ch[3] << " " << ch[4] << endl;
	else
		cout << ch[0] << " " << ch[1] << " " << ch[2] << " " << ch[3] << endl;
	return 0;
}
@whitenite1

ok great looks good, now if i wanted to make that also add them up after it has split them up. i would make just another if statement after the return right.

or could i use this right after the cout on the if statment

1
2
3
4
sum = ch[0]+ch[1]+ch[2]+ch[3];

    cout << "The sum of the numbers is:" << sum <<  endl;
} 
Last edited on
Anything after the return value will not be executed. Also, your sum will still be wrong. Not sure if it's the proper way to do this, but you can use

sum = ch[0]+ch[1]+ch[2]+ch[3] - (4 * static_cast<int>('0'))
@bcav311

Here's how..

The ASCII value of '0', is 48, '1' is 49, etc. So, to get the sum, which is an int, to get the value of the number, would be to subtract the ASCII value of it.

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
#include <iostream>
using namespace std;

int main()
{
	int in, sum=0;
	char ch[5];

	cout << "Please enter a 4 digit number: ";
	for (in = 0; in < 5; in++)
	{
		cin.get(ch[in]);
	}
	cout << "The single digit numbers are: ";
	if (ch[0] == '-')
	{
		cout << ch[1] << " " << ch[2] << " " << ch[3] << " " << ch[4] << endl;
		sum += (ch[1] - 48) + (ch[2] - 48) + (ch[3] - 48) + (ch[4] - 48);
	}
	else
	{
		cout << ch[0] << " " << ch[1] << " " << ch[2] << " " << ch[3] << endl;
		sum += (ch[0] - 48) + (ch[1] - 48) + (ch[2] - 48) + (ch[3] - 48);
	}
	cout << "The sum of the numbers is : " << sum <<  endl;
	return 0;
}


or, you could just 192 all at once, instead of doing each number separately, which is, of course, 4 * 48.

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
#include <iostream>
using namespace std;

int main()
{
	int in, sum=0;
	char ch[5];

	cout << "Please enter a 4 digit number: ";
	for (in = 0; in < 5; in++)
	{
		cin.get(ch[in]);
	}
	cout << "The single digit numbers are: ";
	if (ch[0] == '-')
	{
		cout << ch[1] << " " << ch[2] << " " << ch[3] << " " << ch[4] << endl;
		sum = (ch[1] + ch[2]+ ch[3]  + ch[4])-192;
	}
	else
	{
		cout << ch[0] << " " << ch[1] << " " << ch[2] << " " << ch[3] << endl;
		sum = (ch[0] + ch[1] + ch[2] + ch[3]) - 192;
	}
	cout << "The sum of the numbers is : " << sum << endl;
	return 0;
}
Last edited on
ok got it, so if i wanted to add more numbers i could just up that 192 and then subtract it at the end with each number added. Makes sense thanks for explaining that to me and not just showing. it helps alot to have both, thanks so much i'll keep playing and adding to this

@whitenite1 thanks again
@bcav311

It would probably be easier if you just subtract the 48 from each char number entered. As an aside, I also forgot to add the word 'subtract' in
or, you could just 192 all at once, ...
. It should have read
or, you could just subtract 192 all at once, ...


And you're very welcome.
Topic archived. No new replies allowed.