Thread: Little help please

  1. #1
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179

    Little help please

    I am assuming that this is a very stupid question but I am learning this stuff and anyways I was just playing around trying to make a program that uses constants, it only adds like 2 numbers to the users number but anyways when I compile it I get this warning:

    warning C4244: '=' : conversion from 'double' to 'int', possible loss of data

    Now I don't know how to fix it though I have an idea of what the problem is, like you can't have data in one type of a variable/constant and then move it to another type but as of yet.
    There is one other thing, this error only appears sometimes, I will open it, compile it will say the compiler needs to create these files(blah blah blah) then it will be fine, I then took out a "\n" to make in neater and it gave me the warning but then I executed it and it screwed up as usual but when I recompiled the warning was gone so I don't get it.

    I don't know how to fix it, can someone help please?


    Code:
    #include <stdio.h>
    
    /*This program is designed to test how literal constants and other types of constants work.*/
    
    #define DECIMAL .2354698
    const int whole = 10;
    int userVariable;
    int userTotal;
    
    void userNumber(void)
    {
    	printf("This program will take whatever number you enter and add .234698 to it, and then");
    	printf("add 10 to it as well, it is a test to see how constants work\n\n");
    	printf("Please enter your number and the program will add to it: ");
    	scanf("%i", &userVariable);
    	userTotal = DECIMAL + whole + userVariable;
    }
    
    main(void)
    {
    	userNumber();
    	printf("\nThe result of adding %f and %i to %f is: %f\n", DECIMAL, whole, userVariable, userTotal);
    	
    	return 0;
    }
    Thanks a lot
    Last edited by CAP; 06-26-2002 at 12:25 AM.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    Problem:

    your converting form a float point value to an int, that means
    all your float specific data will be lost.

    Solution:

    your dealing with a floating point value.
    use all doubles or floats.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    Wrong specifiers

    I think that the programm final printf wont show what you expect it to show since userVariable, userTotal are ints and you are using the float specifier %f when you should be using the int specifier %d to show the data.

    Isnt that ryt?? If am wrong somebody fix it, I'm new although I thinks that's part of the problem.
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    [email protected]

  4. #4
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Ok, I tried to do enforce the changes that you suggested and it almost worked. The only problem is that it does display an answer. For example lets say I entered the number 2 it says that the answer is 12(so I am not just getting a 0 this time) but it doesn't add on the decimal constant, I tried to change it to a float to see what would happen and it gives me a number like -235467 or something along that line, what should I do?

    P.S.

    Here is the line of code that the 'warning' is on and what the warning is if you want to take a look.


    Here is the main part of the problem:
    Code:
    void userNumber(void)
    {
    
    	printf("Please enter your number and the program will add to it: ");
    	scanf("%i", &userVariable);
    	userTotal = DECIMAL + whole + userVariable;
    }
    
    
    /*Here is the line that is messing things up:*/
    
    userTotal = DECIMAL + whole + userVariable;
    So if anyone can help I would appreciate it, I think that it is bad because I am adding numbers that don't agree with one another but I don't know that much yet so...
    Last edited by CAP; 06-26-2002 at 10:05 AM.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > but it doesn't add on the decimal constant
    Yes it does, but you immediately lose it again when you cast the float result back to an int via the assignment

    If you make userTotal a float, and use %f to print it, should be OK

  6. #6
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Thanks Salem that worked just fine
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

Popular pages Recent additions subscribe to a feed