Thread: Holy moly!!!

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    Holy moly!!!

    Hey folks,

    I am completely new to C programming(just started taking it in a class at school).. I have a question.

    I am making an if/else statement.

    Code:
    if (column_number<5&&column_number>0)
    		{
    			printf("The average of column %i is: %.3le", column_number,average);
    		}
    	else
    		{
    			printf("Invalid column number");
    		}
    So basically, if the number isnt 1 through 4, then it prints the "invalid column". But if I type in 2.4 or 3.5 it doesnt go to the invalid column statement. What signs can I use to make only 1,2,3 and 4 work ????

    Thanks.. I have a feeling ill be on this forum a lot...

    P.S.. i know my format (neatness) needs work

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Cast the float/double to an int?

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    If you declared column_number as a int and then you are reading the user input with something like scanf("&#37;d", &column_number); what will happen basically if the user enter something like 2.4, is that scanf will read and store the value 2 in column_number. This is how works scanf for integer, it reads the keyboard buffer for numeric character until it read a non-numeric character (approximately).

    There would be different way to do so if the user enter something like 2.4. Like reading the user input in a buffer with fgets, than reading the buffer for an int with sscanf and reading again the buffer with sscanf for a float, then casting the int value to the float and checking if the comparaison is true; in that case, you could assume that the user enter a "real" integer.

    I think that would work (but i have not tested).

    (Edit: i just tested it and it does work. Here's my test program heh)
    Code:
    #include <stdio.h>
    #define TAILLE 50
    
    int main()
    {
    	char chaine[TAILLE];
    	int i;
    	int entier;
    	float flottant;
    
    	printf("Enter a number: ");
    	fgets(chaine, TAILLE, stdin);
    
    	i = sscanf(chaine, "%d", &entier);
    	if (i != 0)
    	{
    		sscanf(chaine, "%f", &flottant);
    		if ((float) entier == flottant)
    		{
    			printf("Integer number.\n");
    		}
    
    		else
    		{
    			printf("Decimal number.\n");
    		}
    	}
    	
    	else
    	{
    		printf("That's not even a number, moron.\n");
    	}
    
    	getchar();
    
    	return 0;
    }
    Last edited by foxman; 07-22-2007 at 01:59 PM.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    cant find "fgets" in my textbook... what is that?? I will show you all the code that I have written...

    Code:
    #include "utils.h"
    	#include  <stdio.h>
    
    
    	#define filename "data.txt"
    
    	int main ()
    {
    
    		int column_number;
    		double average;
    
    
    		FILE *f;
    		f=fopen("data.txt", "r");
    
    		printf("Enter the column number: ");
    		scanf("&#37;i", &column_number);
    
    
    		average = column_average(f,column_number);
    
    	if (column_number<5&&column_number>0)
    		{
    			printf("The average of column %i is: %.3le", column_number,average);
    		}
    	else
    		{
    			printf("Invalid column number");
    		}
    
    
    
    		return 0;
    
    
    }

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    ahhh.. got it.. thanks folks... I'll be back.. haha. guarenteed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implementing POSIX Timers on AIX - Holy Moly
    By dedham_ma_man in forum C Programming
    Replies: 2
    Last Post: 10-03-2007, 02:36 AM
  2. Holy crap, its forty not fourty
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 02-14-2005, 05:23 PM
  3. Holy crap
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 12-08-2004, 11:14 AM
  4. the holy grail for newbie programmers...
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2003, 07:19 PM
  5. Looking for the Holy Grail...
    By Sebastiani in forum Windows Programming
    Replies: 2
    Last Post: 01-07-2003, 01:37 AM