Thread: Help needed for c programming only

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    30

    Exclamation Help needed for c programming only

    Please help me to check my coding where the mistake is and correct it for me. Thank you for the help. I need c programming coding only.

    Question:
    Write down a function getint() , which would receive a numeric string from the keyboard, convert it to an integer number and return the integer to the calling function. A sample usage of getint() is shown below:
    Code:
    #include <stdio.h>
    int main ()
    (
    int a;
    a=getint();
    printf(“you entered %d\n”, a);
    return 0;
    }
    My answer:
    Code:
    #include <stdio.h>
    #include<string.h>
    #define num 50
    void getint()
    {
    	int i,j;
    	char cha1[50];
    	char cha2[num][50];
    	printf ("Please enter the number:(Enter 'endtext' when finish.)\n");
    	scanf ("%s",&cha1);
    	printf ("1");
    	while (strcmp(cha1, "endtext")!=0)
    	{
    		cha2[i]=cha1;
    		i++;
    		scanf ("%s",&cha1);
    	}
    	for (j=0;j<=i;j++)
    	{
    		if (cha2[j]==0)
    		{
    			printf ("%s ",cha2[j]);
    		}	
    		else if (cha2[j]==0)
    		{
    			printf ("Please do not insert character.");
    		}
    		else
    		{
    			printf ("Please do not insert special symbol.");
    		}
    	}	
    }
    int main ()
    {
    	int a;
    	a=getint();
    	printf("you entered %d\n", a);
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    fgets() will get the char string for you, much easier than what you have - 1 line.

    C has standard functions for converting alpha char's to numbers, why not use them? One line, and you're done. Check out atoi() (short for "alpha to integer"), and related functions in the C language.

    It seems like you're fighting the language here, rather than using it. It's fine to work out your own way of doing this, but 1) it doesn't work, and 2) it's just not good code, imo.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Thanks Adak. Because I just learn the C programming this two months. So I only learn so basic like string, array and so on. So I still do not know got the code of atoi(). I will look at it and try my code. Thanks for the help.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    atoi() is part of stdlib.h, so include that header file

    I haven't run this code, but I believe it's OK:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
      int number;
      char *string = {"12345"};
      
      n = atoi(string);
      printf(" My string is: %s, and my number is: %d\n", string, number);
    
    
      return 0;
    }
    
    //Watch your overflow with atoi(). If that happens, the result is undefined.
    Be sure any string you use with atoi(), (like with all string functions), is a REAL string, and has been given (or you have assigned to it), an ending char of '\0' - the end of string character.

    Any bunch of letters without that end of string char, is just a bunch of letters, **NOT** a string, in C.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Oh. The code is use like that. Ok. Thanks for the help. Then how the getint () code function??? As I not sure how it works.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    int i,j;
    	char cha1[50];
    	char cha2[num][50];
    	printf ("Please enter the number:(Enter 'endtext' when finish.)\n");
    	scanf ("%s",&cha1);
    	printf ("1");
    	while (strcmp(cha1, "endtext")!=0)
    	{
    		cha2[i]=cha1;
    		i++;
    		scanf ("%s",&cha1);
    	}
    When I saw i being incremented, without having ever been assigned a starting value, I just stopped reading the code -

    such code is just not up to the standard I set.

    I can't stand to read it.

    If you want to use this function, let me know - I'll disappear from your thread.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Don't disappear from my thread. I need to learn to be better programmer so that i can program such a good code as u did. Thanks for the help anyway.

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Adak, can u recommend me to study which programming book to improve my coding standard????? Thanks.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Adak, do PLEASE avoid bad practices when showing code. Use const char* with string literals.
    And it's better to use strtol rather than atoi. How do you use them? That is what documentation is for. For example, just googling strtol should yield lots and lots of results. Have you tried that already?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    I will try it Elysia. Thanks for the suggestion also. I can learn more by trying more coding. And thanks for the Tool that suggest me the book. Thank you very much.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Elysia, you use const* for string literals, because on your system, they ARE sting literals. As you know, on my compiler, they are NOT.
    So naturally, I don't use const* for those string literals. I only do that on compilers where they ARE constant.

    You can call it a "bad practice" until the cows come home - but I'm WAY too pragmatic to call something something it is not, or change something that works perfectly.

    atoi() worked perfectly. I can't say "don't use atoi() because it might fail", when it's working perfectly. Any C function can fail if it's not used correctly.

    Z man, here's the book for you: (cheaper and better for you):

    Author: Ivor Horton, Title: "Beginning C"

    It is for beginners, but it will take you a very long way, with lots of excellent examples of how to solve a real life puzzle or problem, with your programming.

    For a beginner, it's way better than K&R's white book - that one's for coder's with a bit of experience.
    Last edited by Adak; 12-13-2009 at 09:51 PM.

  13. #13
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875

    I had something like this on a job interview once...

    Only it was to take an integer such as 32768 and reverse it using NO string methods...so the output would be 86723....only question that I had to scratch my head over....good test of thinking but not the kind of stuff I have to do day-in and day-out (CellBE system software)....

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    Elysia, you use const* for string literals, because on your system, they ARE sting literals. As you know, on my compiler, they are NOT.
    Nah, they are string literals either way. Rather, on your system and compiler toolchain, the undefined behaviour that results from modifying a string literal happens to be implemented in a sensible way and is documented as a usable feature.

    Quote Originally Posted by Adak
    So naturally, I don't use const* for those string literals. I only do that on compilers where they ARE constant.
    It may be the case that it is the case for zhengkoon8's compiler.

    Quote Originally Posted by Adak
    You can call it a "bad practice" until the cows come home - but I'm WAY too pragmatic to call something something it is not, or change something that works perfectly.
    It is not pragmatic to rely on undefined behaviour when it is rather easy to get away without undefined behaviour... unless you know for certain that the given compiler where the undefined behaviour is documented as expected will always be used, but this is certainly not the case for an example in a forum dedicated to C programming in general.
    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

  15. #15
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Thanks Adak for the suggestion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DIfference between , and ;
    By rocketman03 in forum C++ Programming
    Replies: 6
    Last Post: 10-21-2009, 09:46 AM
  2. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  3. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM

Tags for this Thread