473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

program converting Celsius to Fahrenheit

Hi
I was doing this program on Visual Studio 2005 where the program converts C to F and vise versa but after debugging it, it doesnt give the expected result. Could someone tell me what is wrong with my code???


#include <iostream>
#include <iomanip>

using namespace::std;
using std::fixed;

int main()
{
int conversionType = 0;
double temp = 0.0;
double result = 0.0;

cout << "Enter -1 (F to C) or 2 (C to F): ";
cin >> conversionType;
cout << "Enter temperature: ";
cin >> temp;

if (conversionType == '-1')
{
result = (temp - 32) * 5 / 9 ;
}
else if (conversionType == '2')
{
result = temp * 9/5 + 32;

} //end if

cout << "Result: " << result << endl;

return 0;
} //end of main function
Apr 8 '08 #1
5 4613
Ganon11
3,652 Expert 2GB
Note that in C++ (and most programming languages), integer division is different than floating point division. Only integers are returned, so 9/5 is 1, and 5/9 is 0. You should replace those with 9.0/5.0 and 5.0/9.0, respectively.
Apr 8 '08 #2
vhm
4
Hi
I was doing this program on Visual Studio 2005 where the program converts C to F and vise versa but after debugging it, it doesnt give the expected result. Could someone tell me what is wrong with my code???


#include <iostream>
#include <iomanip>

using namespace::std;
using std::fixed;

int main()
{
int conversionType = 0;
double temp = 0.0;
double result = 0.0;

cout << "Enter -1 (F to C) or 2 (C to F): ";
cin >> conversionType;
cout << "Enter temperature: ";
cin >> temp;

if (conversionType == '-1')
{
result = ((temp - 32) * 5) / 9 ;
}
else if (conversionType == '2')
{
result = ((temp * 9) /5) + 32;

} //end if

cout << "Result: " << result << endl;

return 0;
} //end of main function


Thats right Gagnon. Here you have something a litle more polished but very faithfull to the original.


<Full code solution removed>
Apr 14 '08 #3
Banfa
9,065 Expert Mod 8TB
vhm, please note we do not allow the posting of full code solutions on this site for a number of reasons.

I suggest you read our Posting Guidelines

Banfa
Administrator
Apr 14 '08 #4
SpecialKay
109 100+
If i were you, i would use
C = F - 32 / 1.8
and
F = C + 32 * 1.8

solves all the problems.
Apr 14 '08 #5
If i were you, i would use
C = F - 32 / 1.8
and
F = C + 32 * 1.8

solves all the problems.
Except that those are actually wrong. They should be:

C = (F - 32) / 1.8
The parentheses are important, other wise it divides 32 by 1.8 and subtracts that from F, and the second one should be

F = C * 1.8 + 32
Apr 15 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Lazareth S. Link | last post by:
Hiya to whoever might stumble across this. I've recently taken up learning how to script/program in python. I've made my first program, a simple fahrenheit-celsius converter, both ways. For...
4
by: nyy | last post by:
Hello everybody on this group. I have this program that is supposed to display a temp. conversion from fahrenheit to celsius, can anybody kindly can tell me what is wrong? Thanks. <!DOCTYPE html...
2
by: ff2 | last post by:
Ok i'm using Borland CBX and Windows XP. My program is to convert farenheit to celsius. I got the code off the CD that came with "Sams Teach Yourself C++ in 24 Hours" so i doubt the code is wrong. I...
1
by: Jay204 | last post by:
i have a simple program im making with basic knowledge of java its supposed to prompt for a celsius temperature do the calculation to convert it to fahrenhiet then pop up saying the result ...
21
by: AsheeG87 | last post by:
Hey Everyone~ I'm still a C++ Rookie so please bear with me on this. I'm doing a temperature conversion program with prototype functions. Basicly, I was wondering if some of you would take a look...
4
by: kipj77 | last post by:
Hi i was wondering why the hell this program isn't working for me, I am in a 100 level computer science class and need help. This program is supposed to take in data of degrees Fahrenheit and spit...
14
by: geebanga88 | last post by:
Hi have this program which keep crashing upon entering input, i think there is somthing wrong with the get_temp function returning the variables back to main?. #include <stdio.h> #include...
11
by: icarus | last post by:
Hi, this is a simple temperature converter (Fahrenheit to Celsius to Kelvin). Using gcc 4.0. The user is supposed to enter q or any other non-character to exit the program. Problem with this...
3
by: jmf777 | last post by:
Hi here is the problem when my program tries to convert Fahrenheit to Celsius it gives an answer of zero regardless of what degree you put in. Any help in solving this is much appreciated. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.