Need help asap

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <ctype.h>
#include <stdio.h>
#include <windows.h>
int x;
main(void)
{
char ch;
printf("Input symbols :\n");
for(;;) {
ch = getchar();
if(ch == '.') break;
if(isdigit(ch)) 

printf("%c This symbol is a number!!\n", ch);
else
printf("False\n");
printf("\n");
}
system("PAUSE");
return 0;
}

I am not even sure if I am on right track
Is there a way to add all my numbers together and divide by their own count?
I am lost...
You're on the right track if you want to do it this way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.
Last edited on
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.
Last edited on
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?

#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>

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");
}

That last line should show up, unless output is buffered and buffer does not flush.

What does happen, if input has no numbers, but you calculate average anyway?

Think about:
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
#include <cctype>
#include <cstdio>

int main()
{
  int sum = 0;
  int count = 0;
  printf("Input symbols :\n");
  for (;;) {
    char ch = getchar();
    if ( ch == '.' ) break;

    if ( isdigit(ch) ) {
      printf("%c This symbol is a number!!\n", ch);
      ++count;
      sum += (ch - '0');
    }
  }

  if ( count ) {
    printf( "Sum %d, count %d\n", sum, count );
  } else {
    printf("False\n");
  }
}


One last thing: int/int produces an int. That has no remainder, no fraction.

A float/int would implicitly convert the int to float, and float/float produces float.
How to get float/int? By explicit cast.

In C: (float)x / count
In C++: static_cast<float>(x) / count
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!
Topic archived. No new replies allowed.