Help with C++ Program

This is a discussion on Help with C++ Program within the C++ Programming forums, part of the General Programming Boards category; This code is supposed to calculate and print out the temperature where the temperature in celsius is the same in ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    14

    Help with C++ Program

    This code is supposed to calculate and print out the temperature where the temperature in celsius is the same in Fahrenheit. So it should print -40 but instead it prints nothing. Please help.

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {int celsius=100, fahrenheit;
    do {
          fahrenheit=9/5*celsius+32;
          celsius--;}
    while(celsius+1!=fahrenheit);
    cout<<"Celsius and fahrenheit are the same at "<<fahrenheit;
    
    return 0;
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    9/5 is 1 in C++
    You should reorganise that expression so that the multiplication is executed before the division
    That's horrible formatting btw, never put the closing brace on the end of a line of code.
    Add some whitespace too.
    Check the FAQ for why your console disappears so quickly.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Indent, format. 9/5 is integer divsision; thus it yields one. If you do 9.0/5, you should see that you'll get some different answers.
    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.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Hmm, and then after you convert to floating point there is the issue of floating point equality.

    abs( fahrenheit - celsius ) < 0.0001

    Or however close you want to be, but not exact.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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