Help

This is a discussion on Help within the C Programming forums, part of the General Programming Boards category; I have this code below. I am doing various things with it. I tried to comment it pretty good so ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    55

    Help

    I have this code below. I am doing various things with it. I tried to comment it pretty good so you can see what is going on. I am having a problem with the array and calculating the sum. I am tryng to get it to ignore the zero and not store it in the array, but just use it for exiting the part of the array that gets the numbers. Can someone take a look at it and tell me what I am doing wrong.

    Code:
    /*included files */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main()
    {
    	/*variable declaration*/
    	unsigned int counter, count;
    	char fname[50], lname[50];
    	char CAPfirst[1], CAPlast[50];
    	int favnum, i, j;
    	char theNums[8] [10];
    	int nums[8] = {0},sum = 0; 
    	int lengthF, lengthL;
    	float numberCount = 0, average = 0;
    
    /* Get first name from user*/
    	printf("Please enter your first name: ");
    	gets(fname);
    
    /*get last name from user*/
    	printf("\nPlease enter your last name: ");
    	gets(lname);
    
    /*get users favorite number*/
    	printf("\nPlease enter your favorite number:");
    	scanf("%d", &favnum);
    
    /*take up to 8 numbers*/
    	printf("\nPlease enter up to 8 numbers to stop entering numbers enter zero\n");
    	printf("\n");
    	for (i=0;i<8;i++)
    	{
    		printf("Enter a number:");
    		fgets(theNums[i], 8, stdin);
    		if (theNums[i][0]=='0') /*exit if zero*/
    			break;
    		else
    		nums[i]=atoi(theNums[i]);
    		numberCount ++; /*count numbers being entered*/
    	}
    	
    	printf("\n\n");
    
    /*calculate sum and average*/
    	for(j=0; j<i; j++)
    		sum +=nums[j];
    		average=(sum/numberCount);
    
    /*count the number of characters in the first and last name*/
    	lengthF = strlen(fname);
    	lengthL = strlen(lname);
    
    /*capitalize first initial of first name*/
    	for(counter=0; counter<1; counter++)
    		CAPfirst[counter] = toupper(fname[counter]);
    		CAPfirst[counter] = '\0';
    
    /*capitalize last name*/
    	for(counter=0; counter<strlen(lname); counter++)
    		CAPlast[counter] = toupper(lname[counter]);
    		CAPlast[counter] = '\0';
    
    /*print first and last name and favorite number*/
    	printf("\n%s %s your favorite number is %d", fname, lname, favnum);
    
    /*print out how many numbers were entered*/
    	printf("\nYou entered %.0f numbers.\n", numberCount);
    
    /*print out numbers entered*/
    	printf("\nHere are the numbers you entered\n");
    	for(i=0;i<numberCount;i++)
    		printf("%d ",nums[i]);
    		printf("\n");
    
    /*print sum and average*/
    	printf("\nThe total of the %.0f numbers you entered is %.0f", numberCount, sum);
    	printf("\nThe average of the %.0f numbers you entered is %.2f:", numberCount, average);
    
    /*Did the user repeat any numbers*/
    	for(i = 0; i<8; i++)
    		{
    			for(j = 0; j<8; j++)
    				{
    				if (theNums[i] == theNums[j])
    				count++;
    				}
    		if(count == 1)
            printf("There are duplicates");
    		}
    
    
    /*is their favorite number part of the array*/
    
    
    
    /*print first initial and last name in caps*/
    	printf("\nHere is your first initial and last name in all caps:\t %s %s", CAPfirst, CAPlast);
    
    /*print out which name is larger*/
    	if (lengthF < lengthL)
    		printf("\nYour last name is larger then your first name\n");
    	else
    		printf("\nYour first name is larger then your last name\n");
    
    
    	
    
    	getchar(); /*wait for keystroke to enter*/
    
    	return 0;
    
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Some problems:

    >>gets()
    Dont use it. Stick to fgets(). Its already causing you problems, if you hadn't noticed.

    >>printf("\nThe total of the %.0f numbers you entered is %.0f", numberCount, sum);
    sum is defined as an int, so use %d not %.0f
    Why is numberCount a float? Are you expecting the user to input half a number or something?

    >>char theNums[8] [10];
    >>fgets(theNums[i], 8, stdin);
    Why only input 8, when the array is 10 in length?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    55

    help counting characters

    I have made some changes. I have to use gets() for the first and last name. It is part of the assignment. I fixed the {}, I didn't realize I left them out because everything compiled okay. I have the array working and the sum and average work now. I am trying to print out part of the persons name. It is the last part of my code. Can someone look at it and tell me what I am doing wrong.

    Code:
    /*included files */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main()
    {
    	/*variable declaration*/
    	unsigned int counter, count;
    	char fname[50], lname[50];
    	char CAPfirst[1], CAPlast[50], CAPfirstFull[50];
    	int favnum, i, j;
    	char theNums[8] [8];
    	int nums[8] = {0},sum = 0; 
    	int lengthF, lengthL;
    	float numberCount = 0, average = 0;
    
    /* Get first name from user*/
    	printf("Please enter your first name: ");
    	gets(fname);
    
    /*get last name from user*/
    	printf("\nPlease enter your last name: ");
    	gets(lname);
    
    /*get users favorite number*/
    	printf("\nPlease enter your favorite number:");
    	scanf("%d", &favnum);
    
    /*take up to 8 numbers*/
    	printf("\nPlease enter up to 8 numbers to stop entering numbers enter zero\n");
    	printf("\n");
    	for (i=0;i<8;i++)
    	{
    		fgets(theNums[i], 8, stdin);
    		if (theNums[i][0]=='0') /*exit if zero*/
    			break;
    		else
    		nums[i]=atoi(theNums[i]);
    		numberCount ++; /*count numbers being entered*/
    	}
    	printf("\n\n");
    
    /*calculate sum and average*/
    	for(j=0; j<i; j++){
    		sum +=nums[j];
    		average=(sum/numberCount);
    		}
    /*count the number of characters in the first and last name*/
    	lengthF = strlen(fname);
    	lengthL = strlen(lname);
    
    /*Capitalize first name*/
    	for(counter=0; counter<1; counter++)
    		CAPfirstFull[counter] = toupper(fname[counter]);
    		CAPfirstFull[counter] = '\0';
    
    /*capitalize first initial of first name*/
    	for(counter=0; counter<1; counter++)
    		CAPfirst[counter] = toupper(fname[counter]);
    		CAPfirst[counter] = '\0';
    		
    /*capitalize last name*/
    	for(counter=0; counter<strlen(lname); counter++)
    		CAPlast[counter] = toupper(lname[counter]);
    		CAPlast[counter] = '\0';
    		
    /*print first and last name and favorite number*/
    	printf("\n%s %s your favorite number is %d", fname, lname, favnum);
    
    /*print out how many numbers were entered*/
    	printf("\nYou entered %.0f numbers.\n", numberCount);
    
    /*print out numbers entered*/
    	printf("\nHere are the numbers you entered\n");
    	for(i=0;i<numberCount;i++){
    		printf("%d",nums[i]);
    		printf("\n");
    		}
    /*print sum and average*/
    	printf("\nThe total of the %.0f numbers you entered is %d", numberCount, sum);
    	printf("\nThe average of the %.0f numbers you entered is %.2f:", numberCount, average);
    
    /*Did the user repeat any numbers*/
    	for(i = 0; i<8; i++)
    		{
    			for(j = 0; j<8; j++)
    				{
    				if (theNums[i] == theNums[j])
    				count++;
    				}
    		if(count == 1)
            printf("There are duplicates");
    		}
    
    
    /*is their favorite number part of the array*/
    
    
    
    /*print first initial and last name in caps*/
    	printf("\nHere is your first initial and last name in all caps:\t %s %s", CAPfirst, CAPlast);
    
    /*print out which name is larger*/
    	if (lengthF < lengthL)
    		printf("\nYour last name is larger then your first name\n");
    	else
    		printf("\nYour first name is larger then your last name\n");
    	
    /*print out nuumber of characters equal to smaller lengthF or lengthL*/
    	if (lengthF < lengthL)
    		for(counter=0; counter < lengthF; counter++)
    		printf("%s", CAPfirstFull);
    	else
    		for(counter=0; counter < lengthL; counter++)
    		puts(CAPlast);
    
    	
    
    	getchar(); /*wait for keystroke to enter*/
    
    	return 0;
    
    }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21