Hello, simple program here i can't seem to get to work. In a nutshell the program reads characters into an array using getchar and then prints them in a loop, although the numbers printed are not the ones i entered! thanks in advance

#include <stdio.h>

#define MAXSIZE 100 /*max array length*/

int arrlen(int []);

int main()
{
	int c, i = 0;
	int s[MAXSIZE];
	
	while((c=getchar()) != EOF && c != '\n'){ /*while the chars read havn't reached a new line or EOF */
		s[i++] = c; /*Read them into an array*/
		if(c == '\n'){
			s[i++] = c;
			}
		}
	s[i] = '\0'; /* terminate the array */
	
	for(i=0;i<arrlen(s);++i){
		printf("%d\n", s[i]);
		}
	return 0;
}

int arrlen(int array[])
{
	int i;
	
	for(i=0;array[i] != '\0';++i) 
	;
	return i; /*return legnth of the array*/
}

Recommended Answers

All 5 Replies

You only enter a string, not a number.

Typing in 123
will likely get you
49 50 51
as output.

If you were expecting 123 to be printed, you need more code.
At the moment, you just print the decimal value of ASCII character codes.

Thanks for the fast reply, yeah was expecting 123 to be printed. Still don't fully understand why it dosn't work. Could you give me an example of the code i would need or point me in the right direction, cheers.

Thanks for the fast reply, yeah was expecting 123 to be printed. Still don't fully understand why it dosn't work. Could you give me an example of the code i would need or point me in the right direction, cheers.

Example of what you are doing:

#include <stdio.h>

int main(void) {
  printf("%d\n", (int)getchar());

  return 0;
}

Example of what you want to do:

#include <stdio.h>

int main(void) {
  printf("%c\n", getchar());

  return 0;
}

So instead of an int array, use char, and instead of %d in your printf statement, use %c.
Good luck.

Hello, simple program here i can't seem to get to work. In a nutshell the program reads characters into an array using getchar and then prints them in a loop, although the numbers printed are not the ones i entered! thanks in advance

#include <stdio.h>

#define MAXSIZE 100 /*max array length*/

int arrlen(int []);

int main()
{
	int c, i = 0;
	int s[MAXSIZE];
	
	while((c=getchar()) != EOF && c != '\n'){ /*while the chars read havn't reached a new line or EOF */
		s[i++] = c; /*Read them into an array*/
		if(c == '\n'){
			s[i++] = c;
			}
		}
	s[i] = '\0'; /* terminate the array */
	
	for(i=0;i<arrlen(s);++i){
		printf("%d\n", s[i]);
		}
	return 0;
}

int arrlen(int array[])
{
	int i;
	
	for(i=0;array[i] != '\0';++i) 
	;
	return i; /*return legnth of the array*/
}

There is a library function strlen() in string.h which returns the length of the string. The only thing you need to do is just change the %d to %c in printf coz you are entering the characters and want teh output of those characters not their ASCII values.

Thanks alot, knew it was something simple!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.