Thread: My First C Program - Checking for compliance

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    My First C Program - Checking for compliance

    Hello,

    I've mostly programmed/scripted in VB and I want to learn better/proper techniques. I've written a very simple program and wanted to make sure it's standards compliant. I'm using VS2008 C++ for a compiler.

    Code:
    #include <stdio.h>
    
    int pause(), add(int one, int two);
    
    main(){
    	int a;
    	int b;
    	int result;
    	
    	printf("Please enter a number: ");
    	scanf("%d", &a);
    	fflush(stdin);
    
    	printf("Please enter another number: ");
    	scanf("%d", &b);
    	fflush(stdin);
    
    	result = add(a, b);
    	printf("The number you entered is: %d\n",result);
    	pause();
    	return(0);
    }
    
    int pause(){
    	char move_on;
    	printf("Thanks for playing, press enter...");
    	//Won't move on until enter is pressed, doesn't work with just any key
    	move_on=getchar();
    	fflush(stdin);
    	return(0);
    }
    
    int add(int one, int two){
    	int sum;
    	sum = one + two;
    	return(sum);
    }
    Please let me know if I seem to be on the write track, of if I'm doing something that would be considered bad technique or out of standards.

    Thanks,
    Kyle

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can always turn up the warnings to high, which will note the lack of "int" before "main" and maybe the use of fancy // comments in a C file (depending on which standard).

    Things that are not "standards compliant" but that you probably won't get warnings for are the use of fflush(stdin), which the standard does not give a meaning to but MS does, and your assigning the result of getchar (which is an int) into a char variable.

    Things to note is that while your comment is more-or-less correct, it's not because of anything you do. That is, the reason you have to type enter is not some magic of getchar, it's just that that's the way input works -- you never see input until the enter key is pressed. (Also the fact that I can talk about "the comment" is worrisome as well.)

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    I've made main:
    Code:
    int main(void)
    I'm trying to figure out the FAQ for flushing right now and will turn up my warnings. Thank you for the feedback!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The board has examples of flushing the input buffer too, if you would search.
    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.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    Code:
    #include <stdio.h>
    
    int pause(), add(int one, int two), convert2int(char string[]);
    
    int main(){
    	char a[10];
    	char b[10];
    	char *p;
    	int uno;
    	int dos;
    	int result;
    	
    	printf("Please enter a number: ");
    	if (fgets(a, sizeof(a), stdin) != NULL)
    	{
    		if ((p = strchr(a, '\n')) != NULL)
    			*p = '\0';
    	}
    
    	printf("Please enter another number: ");
    	if (fgets(b, sizeof(b), stdin) != NULL)
    	{
    		if ((p = strchr(b, '\n')) != NULL)
    			*p = '\0';
    	}
    
    	uno = convert2int(a);
    	dos = convert2int(b);
    
    	result = add(uno, dos);
    	printf("The number you entered is: %d\n",result);
    	pause();
    	return(0);
    }
    
    int pause(){
    	int move_on;
    	printf("Thanks for playing, press enter...");
    	//Get input until enter is pressed
    	move_on=getchar();
    	return(0);
    }
    
    int	add(int one, int two){
    	int sum;
    	sum = one + two;
    	return(sum);
    }
    
    int convert2int(char string[]){
    	int converted;
    	converted = atoi(string);
    	return(converted);
    }
    Is this better? Or what problems have I created now? Thanks again for your feedback.

  7. #7
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Just a few things (and not exactly against standards).
    return is not a function, no () needed. For example

    Code:
    int convert2int(char string[]){
    	int converted;
    	converted = atoi(string);
    	return converted;
    }
    would be just fine. Except....

    I am not a big fan of atoi(). With it you cannot make difference between user who inputs 0, and user who inputs jfdahöfhsfhdsöhgfshösdfahf.
    (Both will return 0)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Add 4 accounts to this program
    By kellymart87 in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2007, 04:31 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM