Answer to Question #311849 in C++ for honda

Question #311849

Write a program that will ask user to enter a digit character (i.e. ‘0’ or ‘1’ .... or ‘9’). If user enters a non-digit character then the program should display message to re-enter correct input. If user enters a correct character (i.e. a digit character) then your program should convert that character to a same digit but in integer type. Do this for five inputs. Finally, add all digits and display their sum. Do not use any library function or loops.


Example:

Enter a digit-character: 8 (please note that this is to be stored in char variable)

Enter a digit-character: 6

Enter a digit-character: 0

Enter a digit-character: +

Wrong character. Please enter again.

Enter a digit-character: 2

Enter a digit-character: 7

Sum of digits is 23.


1
Expert's answer
2022-03-15T16:47:58-0400
using namespace std;




int main()
{
	char c;
	int Sum=0,n=0;
	
	while(n<5)
	{
		c = 'a';
		while(c<'0' || c >'9')
		{
			cout<<"\nEnter a number (#"<<n+1<<"): "; cin>>c;	
		}
		Sum = Sum + (c-0x30);
		n++;		
	}
	cout<<"\n\tSum = "<<Sum;
	return(0);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment