Cant Find Error

This is a discussion on Cant Find Error within the C Programming forums, part of the General Programming Boards category; Hi, I am fairly new to C and have been teaching myself the language for now in prep. for the ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    16

    Cant Find Error

    Hi, I am fairly new to C and have been teaching myself the language for now in prep. for the class I am taking in the Fall. I have been writing a prgram for my girlfriend and have encountered a problem. What happens is that I enter the two values for the variables, but the program never enters the if statements. Can anyone please help me, I have tried many things and have looked online but cannot find the solution. Thank you!

    The problem that I believe is happening is bolded.

    Code:
    #include <stdio.h>
    
    main()
    	{
    	char Entery_One[3];
    	char Entery_Two[3];
    	float pounds;
    	float ounces;
    	float grams;
    	char x;
    	
    		
    	printf("Please enter what you want to convert from. Ie lb\n");
    	scanf("%s", Entery_One);
    	printf("Please enter what you want to convert to. Ie oz\n");
    	scanf("%s", Entery_Two);
    
    printf("\n%s\n",Entery_One); //TEST
    printf("%s",Entery_Two); //TEST
    
    	if(Entery_One == 'lb')	{
    	printf("Enter how many pounds you would like to convert\n");
    	scanf("%f", &pounds);
    
    		if(Entery_Two == 'oz')
    		{
    		pounds2ounces(pounds);
    		}
    	
    	}

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is because you are comparing a character constant with the address of an array. The compiler really should moan about this with a warning - so I suggest you enable warnings, and "listen" to what the compiler is telling you in these cases.

    To compare text input, you need to use "strcmp". Make sure you look up the strcmp function in your help, or if you use google, type in "man strcmp" to get a "manual page" from Linux to describe the standard function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. main should be
    int main(void) - read FAQ

    2. you scanf format is very dangerous - it does not provides the maximum number of characters to read

    3. 'a' is 1 character, "a" is a string literal - pay attention to the difference

    4.
    Code:
    pounds2ounces(pounds);
    you probably want to store the return value somewhere

    5. fix your indentation and add
    return 0;
    to the end of the main function
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    More information on using scanf right:
    Need help with altering info on a txt file
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  3. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21