program in C "the variable 'i's being used without being initialized

\* hello im new in this thing of programming and especially in C.
this program was done by me to calculate the sum of numbers i meant letters.
ex: if i write ab in console get calculation like that:
if a=1 and b=2 then sum = 1+2=3. when i type a word in console, space bar another word, enter i get an error with possibilities [abort], [retry], [ignore].





#include <stdio.h >
int main ()
{
char string1[20];


char a; char b; char g; char d; char e;
char v; char z; char T; char I;
char k; char l; char m; char n;
char o; char p; char j; char r;
char s; char t; char u; char f;
char q; char R; char y; char w;
char C; char W; char x; char zz;
char h; char c; char J; char Z;


int sum1; int sum2; int sum3; int sum4;
int sum5; int sum6; int sum7; int sum8;
int sum9; int sum10; int sum11; int sum12;
int sum13; int sum14; int sum15; int sum16;
int sum17; int sum18; int sum19; int sum20;
int sum21; int sum22; int sum23; int sum24;
int sum25; int sum26; int sum27; int sum28;
int sum29; int sum30; int sum31; int sum32;
int sum33;

int i;
int sum;

printf (" my word is:");
scanf ( "%s\n ", string1);


sum1=1; sum2=2;
sum3=3; sum4=4;
sum5=5; sum6=6;
sum7=7; sum8=8;
sum9=9; sum10=10;
sum11=100; sum12=200;
sum13=300; sum14=400;
sum15=500; sum16=600;
sum17=700; sum18=800;
sum19=900; sum20=1000;
sum21=2000; sum22=3000;
sum23=4000; sum24=5000;
sum25=6000; sum26=7000;
sum27=8000; sum28=9000;
sum29=10000; sum30=11000;
sum31=12000; sum32=13000;
sum33=14000;


sum=0;

if (string1[i]==a ) {sum= sum +sum1;}
if (string1[i]==b ) {sum= sum +sum2;}
if (string1[i]==g ) {sum= sum +sum3;}
if (string1[i]==d ) {sum= sum +sum4;}
if (string1[i]==e ) {sum= sum +sum5;}
if (string1[i]==v ) {sum= sum +sum6;}
if (string1[i]==z ) {sum= sum +sum7;}
if (string1[i]==T ) {sum= sum +sum8;}
if (string1[i]==I ) {sum= sum +sum9;}
if (string1[i]==k ) {sum= sum +sum10;}
if (string1[i]==l ) {sum= sum +sum11;}
if (string1[i]==m ) {sum= sum +sum12;}
if (string1[i]==n ) {sum= sum +sum13;}
if (string1[i]==o ) {sum= sum +sum14;}
if (string1[i]==p ) {sum= sum +sum15;}
if (string1[i]==j ) {sum= sum +sum16;}
if (string1[i]==r ) {sum= sum +sum17;}
if (string1[i]==s ) {sum= sum +sum18;}
if (string1[i]==t ) {sum= sum +sum19;}
if (string1[i]==u ) {sum= sum +sum20;}
if (string1[i]==f ) {sum= sum +sum21;}
if (string1[i]==q ) {sum= sum +sum22;}
if (string1[i]==R ) {sum= sum +sum23;}
if (string1[i]==w ) {sum= sum +sum24;}
if (string1[i]==C ) {sum= sum +sum25;}
if (string1[i]==W ) {sum= sum +sum26;}
if (string1[i]==x ) {sum= sum +sum27;}
if (string1[i]==zz ) {sum= sum +sum28;}
if (string1[i]==h ) {sum= sum +sum29;}
if (string1[i]==c ) {sum= sum +sum30;}
if (string1[i]==Z ) {sum= sum +sum31;}
if (string1[i]==J ) {sum= sum +sum32;}
if (string1[i]==y ) {sum= sum +sum33;}
if (string1[i]=='\0' ) {sum= sum +0;}
printf ("%d\n" , sum);
return 0;

}
You can use the int representations of char to add to sum. In the code below, we must subtract by 96 because the ASCII int value of 'a' is 97. When we hit NULL, we simply stop. Just in case the user entered a character that is not a letter, we print an error.

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
26
27
#include <cstdio>
#include <cctype>

int main(void)
{
	char *word=new char[25];
	char termch;
	int sum=0,counter=0;
	printf("This program adds letters.\n");
	printf("Enter your word:  ");
	scanf("%s%c",word,&termch);
	do
	{
		sum+=((tolower(word[counter]))-96);
		if(isalpha(word[counter+1]))
		{
			printf("Only letters please! Retry.");
			getchar();
			delete [] word;
			return(0);
		}
	}while(word[++counter]!=NULL);
	printf("Sum of the word is: %d",sum);
	delete [] word;
	getchar();
	return(0);
}


Also, make sure you use code tags.
Last edited on
Topic archived. No new replies allowed.