I wrote this program and am trying to simplifying by creating a character array. How can this be done please?

I found some help here
http://www.java2s.com/Code/C/Data-Type/Forloopachararrayusingpointer.htm

but couldn't really implement that in my program.

//inputs five strings representing integers, converts the strings to
//integers, sums the values and prints out the total

#include<stdio.h>
#include<stdlib.h>

int main ()
{
	int i,a,b,c,d,e;
	char word1[256],word2[256],word3[256],word4[256],word5[256];
	printf("enter something \n");
	fgets(word1,256,stdin);
	printf("enter something \n");
	fgets(word2,256,stdin);
	printf("enter something \n");
	fgets(word3,256,stdin);
	printf("enter something \n");
	fgets(word4,256,stdin);
	printf("enter something \n");
	fgets(word5,256,stdin);
	a=atoi(word1);
	b=atoi(word2);
	c=atoi(word3);
	d=atoi(word4);
	e=atoi(word5);
	i=a+b+c+d+e;
	printf("Total value entered is %d\n\n",i);
	return 0; 
}

I take it you want to make a multi-dimensional array so you can store all the words into one array and then use a for() loop to populate/sum the values.

You can merge the two for() loops into one but if you were to join them then why take the values in as a char array and not as an integer rather than converting.

//inputs five strings representing integers, converts the strings to
//integers, sums the values and prints out the total

#include<stdio.h>
#include<stdlib.h>

int main ()
{
	int i, sum = 0;
	int numWords = 5, sizeofWord = 256;

	char words[numWords][sizeofWord];

	for( i = 0; i < numWords; i++ )
	{
		printf("\nEnter something\n");
		fgets(words[i], sizeofWord, stdin);
	}

	for( i = 0; i < numWords; i++ )
		sum += atoi(words[i]);

	printf("\nTotal value entered is %d\n\n", sum);
	return 0;
}
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.