dumb coding problem thread

  1. #1
    Pipster's Punk Reputation: 277
    postrook's Avatar
    Join Date
    Jun 2012
    Location
    joykill
    Posts
    2,465
    Rep Power
    9

    Default dumb coding problem thread

    because i feel more at home here than at stackoverflow.

    Code:
    int get_input(char input[])
    {
            int value=0;
     
            while((input[value]=getchar())!='\n')
            {
                    value++;
            }
            printf("value=%d",value); /*when Anything with a space at the start or more than one space elsewhere is         entered it causes the program to crash before getting here.*/
            input[value++]='\0';
            return value;
    }
    so for instance, on windows command line i'll type in

    what what
    (ie. four spaces in front of the first what, three between the two words and three after that.

    and it just freezes up and i need to ctrl+c out.

    on linux command line it at least gives me more than that. it gives me a segmentation fault.

    i assume the problem has to do with all those spaces being turned into newline characters or something, but i thought i'd try to get confirmation on that before i go ripping this up and trying to think of another solution.
    I hate TALKING. to PEOPLE. about THINGS.

  2. #2
    Pipster's Punk Reputation: 277
    postrook's Avatar
    Join Date
    Jun 2012
    Location
    joykill
    Posts
    2,465
    Rep Power
    9

    Default

    wait nevermind i figured it out. it was some code that came after that. still seems pretty weird that it wouldn't execute the printf statement before getting to that.
    I hate TALKING. to PEOPLE. about THINGS.

  3. #3
    `Doll's Dishwasher Reputation: 46

    Join Date
    Oct 2006
    Posts
    1,435
    Rep Power
    8

    Default

    generally it's problems like that where I like to leave little "print markers" so i can follow when the code breaks. Sure you can just set break points now adays on an IDE, but I prefer to read the console text to see what happened.
    Messing around.

  4. #4
    Crumbly, yet Good Reputation: 75
    Eriond's Avatar
    Join Date
    May 2006
    Location
    Montreal
    Posts
    1,798
    Rep Power
    0

    Default

    Yeah; it won't always actually printf when you say to printf. Generally IO is buffered, meaning it stays in memory before actually getting spit out to your console. The usual way to flush the buffer to the console is either to put a newline after printf (usually), so you'd do printf("value=%d\n",value) instead, or you can do fflush(stdout); right after.

    But yeah; breakpoints are key. But it's difficult to beat the simplicity of printf when using a debugger in impractical.
    Last edited by Eriond; 12-05-2013 at 08:07 PM.
    THIS MA SIGNATURE

  5. #5
    Pipster's Punk Reputation: 277
    postrook's Avatar
    Join Date
    Jun 2012
    Location
    joykill
    Posts
    2,465
    Rep Power
    9

    Default

    edit: meh, here's the entire code of the program. what's causing the problem is the function to get stuff from the files, which is named getRawData.

    http://pastebin.com/e7fzfWzE

    Code:
    /*Programmer: Nihcolas Canning
    Program: Assignment 3
    Course: IPC144A
    Instructor: Arta Kogan
    Date: December 6, 2013*/
    
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define START 1921
    #define SIZE 18
    #define DIVIDE 5
    
    //function prototypes and structures
    
    struct NameRecord
    {
    	char name[31];
      	int year;
    	int frequency;
    };
    
    void allCaps(char s[]);
    
    int getRawData(FILE* fp,struct NameRecord records[],int currSize);
    
    void setYearTotals(struct NameRecord records[], int size, int yearRangeTotal[]);
    
    void setNameYearTotals(char theName[],struct NameRecord records[], int size, int nameTotal[]);
    
    void getPerHundredThousand(int nameTotal[], int yearRangeTotal[], double perHundredThousand[]);
    
    void printData(double perHundredThousand[]);
    
    void graphPerHundredThousand(double perHundredThousand[]);
    
    int main()
    {
    	int valid=1;
    	char input=' ';
    	char theName[100]={' '};
    	FILE *fp;
    	struct NameRecord records[150000];
    	int currSize;
    	int yearRangeTotal[SIZE]={0};
    	int nameTotal[SIZE]={0};
    	double perHundredThousand[SIZE]={0};
    
    	printf("Welcome to the Name Popularity Checker\n======================================\n");
    	while(valid)
    	{
    		currSize=0;
    		printf("Please enter a name:");
    		scanf("%s",theName);
    		allCaps(theName);
    		printf("Number of Babies named %s per 100,000 births\n============================================\n", theName);
    		fp=fopen("femalebabynames.txt", "r");
    		if(fp)
    		{
    			currSize=getRawData(fp,records,currSize);
    			fclose(fp);
    			fp=fopen("malebabynames.txt", "r");
    			if(fp)
    			{
    				currSize=getRawData(fp,records,currSize);
    				fclose(fp);
    				setYearTotals(records,currSize,yearRangeTotal);
    				setNameYearTotals(theName,records,currSize,nameTotal);
    				getPerHundredThousand(nameTotal,yearRangeTotal,perHundredThousand);
    				printData(perHundredThousand);
    				graphPerHundredThousand(perHundredThousand);
    				printf("Do you wish to check another name (Y/N)?");
    				while((scanf("%c", &input))!='\n')
    				{
    					if(input=='n')
    					{
    						valid=0;
    					}
    				}
    			}
    			else
    			{
    				printf("Error opening file malebabynames.csv.\n  Program shutting Down\n");
    				valid=0;
    			}
    			
    		}
    		else
    		{
    			printf("Error opening file femalebabynames.csv.\n  Program shutting Down\n");
    			valid=0;
    		}
    	}
    	return 0;
    }
    
    //function definitions
    
    void graphPerHundredThousand(double perHundredThousand[])
    {
    	printf("\n\n                             Graph\n=====================================================\n\n");
    	int a;
    	int b=START;
    	int c;
    	int stars=0;
    	double lowest=0;
    	int remain=0;
    	for(a=0;a<SIZE;a++)
    	{
    		if(perHundredThousand[a]>=0.01)
    		{
    			if(a==0 || lowest<perHundredThousand[a])
    			{
    				lowest=perHundredThousand[a];
    			}
    		}
    	}
    	for(a=0;a<SIZE;a++)
    	{
    		remain=(int)perHundredThousand[a]%(int)lowest;
    		stars=0;
    		if(remain!=0)
    		{
    			stars=1;
    		}
    		stars+=perHundredThousand[a]/lowest;
    		printf("%d - %d |",b,b+=4);
    		for(c=0;c<stars;c++)
    		{
    			printf("*");
    		}
    		printf("\n");
    	}
    }
    
    void printData(double perHundredThousand[])
    {
    	int a;
    	int b=0;
    	for(a=START;a<2011;a++)
    	{
    		printf("%d - %d: %.2lf\n",a, a+=4, perHundredThousand[b]);
    		b++;
    	}
    }
    
    void getPerHundredThousand(int nameTotal[], int yearRangeTotal[], double perHundredThousand[])
    {
    	int a;
    	for(a=0;a<SIZE;a++)
    	{
    		perHundredThousand[a]=10000*nameTotal[a]/yearRangeTotal[a];
    	}
    }
    
    void setNameYearTotals(char theName[],struct NameRecord records[], int size, int nameTotal[])
    {
    	int a;
    	int b;
    	int error=0;
    	int check=0;
    	for(a=0;a<size;a++)
    	{
    		if(strlen(theName)==strlen(records[a].name))
    		{
    			for(b=0;b<strlen(theName);b++)
    			{
    				if(theName[b]!=records[a].name[b])
    				{
    					error=1;
    				}
    			}
    			if(error==0)
    			{
    				check=records[a].year;
    				check-=START;
    				check/=DIVIDE;
    				nameTotal[check]+=records[a].frequency;
    			}
    			error=0;
    		}
    	}
    }
    
    void setYearTotals(struct NameRecord records[], int size, int yearRangeTotal[])
    {
    	int a;
    	int check=START;
    	int check2;
    	int valid=1;
    	for(a=0;a<size;a++)
    	{
    		while(valid)
    		{
    			if(check==records[a].year)
    			{
    				check2=check;
    				check2-=START;
    				check2/=DIVIDE;
    				yearRangeTotal[check2]+=records[a].frequency;
    				valid=0;
    			}
    			else
    			{
    				check++;
    			}
    		}
    		valid=1;
    		check=START;
    	}
    }
    
    int getRawData(FILE* fp,struct NameRecord records[],int currSize)
    {
    	
    	
    		while((fscanf(fp, "%d,%[^,],%d\n", &records[currSize].year, records[currSize].name, &records[currSize].frequency))!=EOF)
    		{
    		currSize++;
    	}
    	return currSize;
    }
    
    void allCaps(char s[])
    {
    	int num=strlen(s);
    	int a;
    	for(a=0;a<num;a++)
    	{
    		s[a]=toupper(s[a]);
    	}
    }
    I hate TALKING. to PEOPLE. about THINGS.

  6. #6
    Pipster's Punk Reputation: 277
    postrook's Avatar
    Join Date
    Jun 2012
    Location
    joykill
    Posts
    2,465
    Rep Power
    9

    Default

    yay i got it outputting the problem was i was ending up with values under the minimum year i was checking for in the other functions. so those turned into infinite loops. now it's just that the values are completely wrong but that i can solve.
    I hate TALKING. to PEOPLE. about THINGS.

  7. #7
    Pipster's Punk Reputation: 277
    postrook's Avatar
    Join Date
    Jun 2012
    Location
    joykill
    Posts
    2,465
    Rep Power
    9

    Default

    got a new problem that's puzzling me.

    Code:
    bool isRegistered(const Prefix* p, const char* str, char area[], char publisher[], char title[])
    {
    ...
    char str2[30];
    ...
    for(int z=0;z<strlen(str);z++)
    {
    	str2[z]=str[z];
    }
    str2[z]='\0';
    
    //the only reason i added the above parts is because i thought using a const char* c-style string for the below might be the problem.  apparently not
    
    /*line 41*/ for(int a=1;a<13;a+=2)
    {
    	num+=(atoi(str2[a]))*3; 
    }
    ...
    }
    gives error: EAN.cpp: In function ‚bool isRegistered(const Prefix*, const char*, char*, char*, char*)‚:
    EAN.cpp:43:21: error: invalid conversion from ‚char‚ to ‚const char*‚ [-fpermissive]
    /usr/include/stdlib.h:148:12: error: initializing argument 1 of ‚int atoi(const char*)‚ [-fpermissive]
    I hate TALKING. to PEOPLE. about THINGS.

  8. #8
    Pipster's Punk Reputation: 277
    postrook's Avatar
    Join Date
    Jun 2012
    Location
    joykill
    Posts
    2,465
    Rep Power
    9

    Default

    nevermind again! also sorted it out. atoi doesn't like when you feed it individual elements as opposed to pointers to the first elements in a string, i guess.
    I hate TALKING. to PEOPLE. about THINGS.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •