Thread: What's wrong with my success statement??

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

    What's wrong with my success statement??

    Just a simple program to return an average value from a separate file. I can only type in the number 1 through 4 for the column_number. Any ideas?

    Code:
    #include <stdio.h>
    #include "utils.h"
    #define FILENAME "data.txt"
    
    
    int main () {
    
    	int column_number;
    	double average;
    	char success = 1;
    
    	FILE *f;
    	f=fopen("data.txt", "r");   //open file
    
    	printf("Enter the source column: ");
    	scanf("%i", &column_number);
    
    
    switch (column_number) {
    
    		case '1':
    
    			average = column_average(f,column_number);
    
    			break;
    
    		case '2':
    
    			average = column_average(f,column_number);
    
    			break;
    
    		case '3':
    
    			average = column_average(f,column_number);
    
    			break;
    
    		case '4':
    
    			average = column_average(f,column_number);
    
    			break;
    
    
    		default:
    
    		success = 0;
    
    
    
    break;
    
    }
    
    if (success)
    
    
    	printf("The average of column %i is %.3e", column_number, average);
    
    	else
    
    	printf("Invalid column number");
    
    
    
    
    return 0;
    
    
    }
    Thanks!!

    Ty

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > case '1':
    You're typing in numeric 1, not character '1'
    So drop the single quotes.
    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.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by cool_dude07 View Post
    Just a simple program to return an average value from a separate file. I can only type in the number 1 through 4 for the column_number. Any ideas?

    Code:
    #include <stdio.h>
    #include "utils.h"
    #define FILENAME "data.txt" // This is questionable
    
    	f=fopen("data.txt", "r");   //open file
           // at this point, your actually calling fopen(""data.txt"", "r") 2 double quotes ??
          //  a better way is: char *filename = "data.txt";
    
    	printf("Enter the source column: ");
    	scanf("&#37;i", &column_number);
    
    
    switch (column_number) {
    
    		case '1':
    
    			average = column_average(f,column_number);
    
    			break;
    
    		case '2':
    
    			average = column_average(f,column_number);
    
    			break;
    
    		case '3':
    
    			average = column_average(f,column_number);
    
    			break;
    
    		case '4':
    
    			average = column_average(f,column_number);
    
    			break;
    
    
    		default:
    
    		success = 0;
    
    
    
    break;
    
    }
    
    if (success)
    
    
    	printf("The average of column %i is %.3e", column_number, average);
    
    	else
    
    	printf("Invalid column number");
    
    
    
    
    return 0;
    
    
    }
    Thanks!!

    Ty
    Check commets above, eh nvm I see your not using FILENAME as an argument but still it's questionable practice.
    if you want to define a string literal use a char * or char[]
    Last edited by MacNilly; 07-21-2007 at 12:54 AM.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    2
    thanks for the tips...but still no success (no pun intended) in filtering the input

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by cool_dude07 View Post
    thanks for the tips...but still no success (no pun intended) in filtering the input
    Post complete code. I see no "column_number" defined anywhere.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by MacNilly View Post
    Post complete code. I see no "column_number" defined anywhere.
    Code:
    int main () {
    
        int column_number;

  7. #7
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    You really don't need a switch statement. You just need
    Code:
    if (column_number < 1 || column_number > 4)
    {
        printf("Invalid column number");
        return 0;
    }
    
    average = column_average(f,column_number);
    printf("The average of column %i is %.3e", column_number, average);
    
    return 0;
    Don't quote me on that... ...seriously

  8. #8
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by robwhit View Post
    Code:
    int main () {
    
        int column_number;
    Eh forget it

    I think at least one of your problems was declaring an integer variable and then comparing it to character literals.

    I'm not sure if that's guaranteed to work on all systems. If you want to compare an int to and int do that.
    Last edited by MacNilly; 07-21-2007 at 11:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with my if statement
    By joker_tony in forum C Programming
    Replies: 6
    Last Post: 05-10-2008, 02:11 AM
  2. what is wrong with my if statement
    By joker_tony in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 12:26 AM
  3. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Something wrong with this if statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-30-2002, 05:19 PM