Thread: advice on why this wont run... pointers.. any advice appreciated! :)

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    9

    advice on why this wont run... pointers.. any advice appreciated! :)

    Hi there. This program is to count the number of digits in each string. I'm really not sure if I am using pointers correctly, and not sure if i can compare them to ints like that in the if condition. Currently it wont run and gives errors:C2220: warning treated as error - no 'object' file generated, warning C4702: unreachable code

    Any tips appreciated!
    Code:
    #include <stdio.h>
    int count_digits(char* text);
    
    
    int main(void)
    {
    	char* test[] =
    	{
    	"H3ll0", // 2 digits
    	"Qu3sti0n 3...", // 3 digits
    	"0ld Pr4ctic4l T3st", // 4 digits
    	"is", // 0 digits
    	"e4sy!" // 1 digit
    	};
    	for (int k = 0; k < 5; ++k)
    	{
    		printf("%s : ", test[k]);
    		printf("%d digits\n", count_digits(test[k]));
    	}
    	return 0;
    }
    
    
    int count_digits(char* text)
    {
    	int count = 0;
    	for (int i = 0; i < 20; i++)
    	{
    		if (text[i] > 47 && text[i] < 58)
    		{
    			count++;
    		}
    	}
    	return count;
    	// TODO: Insert your code here...
    	// TODO: Temporary stub return...
    	
    	
    	
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,855
    Delete the return 0; in count_digits
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,121
    > for (int i = 0; i < 20; i++)
    You should stop when you reach the end of the string, not keep roaming through memory.

    > if (text[i] > 47 && text[i] < 58)
    Saying this would be more readable.
    if (text[i] >= '0' && text[i] <= '9' )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    409
    Strings are terminated with nulls in C. So to step through a string character by character, you don't stop at a set length. The most efficient way to do it is

    Code:
    int stringfunction(char *text)
    {
        int i;
    
        for ( i = 0; text[i] != 0; i++)
        {
             /* do something with text[i] */
        }
    }
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    697
    Code:
    int count_digits( char *s )
    {
      int counter = 0;
    
      for (; *s; s++ )
        if ( isdigit( *s ) )
          counter++;
    
      return counter;
    }

  6. #6
    Registered User
    Join Date
    Jun 2020
    Posts
    9
    Thanks all, was the help i needed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some advice
    By Adam22 in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2011, 02:34 AM
  2. Need some advice
    By Cdrwolfe in forum C++ Programming
    Replies: 10
    Last Post: 08-13-2006, 09:13 AM
  3. Need Advice 2
    By cavecrazy in forum C++ Programming
    Replies: 12
    Last Post: 06-07-2003, 10:13 PM
  4. need advice 3
    By lokail in forum C Programming
    Replies: 2
    Last Post: 04-16-2002, 05:33 PM
  5. Advice&help
    By REvOLvER in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2002, 04:23 PM

Tags for this Thread