Hello, I need to make program that asks user to input some symbols as a string. Then I have to check for all the numbers and find their average arithmetic and output that. If there are none numbers in that string then program should output "False"
This is what I have got for now:
int x = 0;
int count = 0;
int main(void)
{
int ch;
for (;;)
{
printf("Input symbols :\n");
ch = getchar();
if (ch == '.') break;
if (isdigit(ch))
{
printf("%c This symbol is a number!!\n", ch);
x += (ch - 48); //x will increase with each number typed
count += 1; //Keep track of how many numbers we've encountered!
}
else
printf("False\n");
printf("\n");
}
system("PAUSE");
return 0;
}
And know that this will ONLY work on single digit numbers. and it's (ch-48) because the number values on the ascii table starts at 48 for 0.
The variable "count" will keep track of how many numbers you've found, and you can use it to divide and find the average.
However, I suspect what you may really want is to be able to use more than single digit numbers for this. For that case, your code is going in the wrong direction since "char" can only hold a single character at a time.
Also, it seems that there's no reason for you to be using a for loop, should replace it with a while loop.
Indentation. It helps. It is hard to see what is inside a loop or if in your code.
Some indentation, etc:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <cctype>
#include <cstdio>
int main()
{
printf("Input symbols :\n");
for (;;) {
char ch = getchar();
if ( ch == '.' ) break;
if ( isdigit(ch) ) {
printf("%c This symbol is a number!!\n", ch);
} else {
printf("False\n");
}
printf("\n");
}
}
You cannot print "False" before you have seen all characters. Only after the '.' -- after the loop is over -- you can tell whether none was a number.
That relates to the count. You should have a counter.
It should be 0 before the loop, because at that point you have processed 0 characters and 0 numbers.
Whenever you do get a number in the loop, the counter should increment.
After the loop the counter thus holds the count of numbers that you saw in the loop.
A question: Does "42" contain one number, or two? In other words, is each digit a separate number? Your current loop treats digits as separate numbers.
Really big thanks for your fast responses. This program is ment to work only with single digit numbers. And if, for example, "42" is entered, program should treat them as separate numbers.
Although program is almost done, I am having some difficulties with that loop. I am not sure if I understand correctly how it works. Why my last printf line does not even show up. Is it because of that loop or some silly beginner mistake?
int x = 0;
float y;
int count = 0;
int main(void)
{
int ch;
for (;;)
{
printf("Input symbols :\n");
ch = getchar();
if (ch == '.') break;
if (isdigit(ch))
{
printf("%c This symbol is a number!!\n", ch);
x += (ch - 48);
count += 1;
}
else
printf("False\n");
}
y=x/count;
printf("Average aritmethic from all numbers is %2.1f ",y);
system("PAUSE");
}
Thank you, finally solved all my problems and got program working exactly as I wanted to.
I changed the end of program a bit:
if
(count>0)
{
y=(x/count);
printf("Videjais aritmetiskais no visiem ievaditajiem skaitliem ir %2.1f ",y);
printf("\n");
}
else
printf("False\n");
Now, if input has no numbers it goes straight to False.
I did not understand "static_cast<float>(x) / count" this, but I changed most of my variables to float and it all worked out.
I am just starting out and I really appreciate all the help!
Thank you so much!