C program wont compile

This is a discussion on C program wont compile within the C Programming forums, part of the General Programming Boards category; I am writing a program that analyzes a string. Each part must be written as a function and each part ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    76

    C program wont compile

    I am writing a program that analyzes a string. Each part must be written as a function and each part will analyze the same string. So far, I'm just getting the string length. I am getting compiling errors. Whats wrong?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int charnum(char text);
    
    void main(void) {
    	
    	int size;
    	char text[81];
    
    	printf("Please type a string or hit 'Enter' to quit:\n");
    	fgets(text, 81, stdin);
    	while (text[1]!='\0') {
    		size=charnum(text);
    		printf("count=", size);
    	}
    }
    
    
    int charnum(char text) {
    
    	int size;
    	size=0;
    	size=strlen(text);
    	return size;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    the strlen() function works on a pointer to a char, not a char. Change the function to:

    Code:
    int charnum(char *text) {
    	if(text)
    		return strlen(text);
    	return -1;
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Also your printf() statement should look like:
    Code:
    printf("count = %d\n", size);
    The %d in the printf() statement is where the passed integer gets inserted into the string.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    ok thanks

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > while (text[1]!='\0')
    Are you sure you want a constant expression here (nothing inside the loop will change it)
    It will either not happen at all, or it will loop forever.

    I think you probably wanted an if() statement

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Compile Public Domain Pocket PC C Program
    By m1l in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 07-20-2007, 04:02 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. new to C--can't get program to compile
    By samerune in forum C Programming
    Replies: 12
    Last Post: 04-02-2007, 09:44 AM
  5. how do i compile a program that deals w/classes?
    By Shy_girl_311 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2001, 01:32 AM

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