Thread: What's wrong here?

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    9

    What's wrong here?

    Hey, I have a C programming course and I am currently working on a problem that asks for:
    Write a complete C program that will ask the user for a series of temperatures for a city. The value -99 will signify the end of entries. Have the program display the hottest temperature and also the coldest.

    I have this as my code and whenever I try it on C it just results in asking me for 2 temperatures then ends. Please help!!


    CODE:
    insert
    Code:
    #include<stdio.h>
    
    
    int
    main(void)
    {
        int temp, hottest, coldest;
        printf("Enter a temperature: ");
        scanf("%d", temp);
        temp = hottest;
        temp = coldest;
        while(temp != -99)
        {
            printf("Enter another temperature: ");
            scanf("%d", temp);
            
            if(temp>hottest)
            {
                temp = hottest;
                printf("The hottest temperature is %d", hottest);
            }
            else
            {
                printf("The hottest temperature is %d", hottest);
            }
            
            if(temp<coldest)
            {
                temp = coldest;
                printf("The hottest temperature is %d", coldest);
            }
            else
            {
                printf("The hottest temperature is %d", hottest);
            }
        }
        
        return(0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When using scanf to read an int, you need to pass a pointer as the corresponding argument, so in both cases you should have written:
    Code:
    scanf("%d", temp);
    Also, recall that assignment with = assigns the right hand side to the left hand side. You're using it wrongly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    9
    but they already say that no? Sorry I am terrible at programming lol

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh sorry. Forgot to modify to add the pointer:
    Code:
    scanf("%d", &temp);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    9
    thanks!

  6. #6
    Registered User
    Join Date
    Mar 2019
    Posts
    9
    do you notice any other errors? It gives a weird number for the lowest and highest temperature.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, I wrote about it in my first post.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Okay so...

    If I want to give a variable a value, I do this...
    Code:
    banana = 4;
    If I then want to give that value to another variable...

    Code:
    banana = 4;
    apple = banana;
    Have a closer look at
    Code:
    temp = hottest;
    ... The value in "temp" is lost, which is not what you want
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why so wrong numbers? As I write so wrong?
    By Dmy in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2017, 02:10 PM
  2. Replies: 3
    Last Post: 11-14-2011, 06:35 PM
  3. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM

Tags for this Thread