Some help in C++ requested (new to programming)

This is a discussion on Some help in C++ requested (new to programming) within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a program that converts Celsius to Fahrenheit and the reverse. I just started learning C++ so ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Some help in C++ requested (new to programming)

    I'm trying to write a program that converts Celsius to Fahrenheit and the reverse. I just started learning C++ so this might not be the most efficient way to write this. I'm experiencing errors at the "else if" and "while" parts (line 24 and 32). Any tips?

    Code:

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int whichconversion;
    char continue_ques;
    double Fahrenheit;
    double Celsius;
    double newFtemp;
    double newCtemp;
    void FtoC(double n);
    void CtoF(double n);
    
    int main() {
    	do {
    	cout << "Choose what you would like to : " << endl;
    	cout << "1) Convert F to C" << endl;
    	cout << "2) Convert C to F" << endl;
    	cin >> whichconversion;
    	if( whichconversion == 1 ); {
    		cout << "Enter the F temp you want to convert: " << endl;
    		cin >> Fahrenheit;
    		FtoC(Fahrenheit);
    	}else if( whichconversion == 2); {
    		cout << "Enter the C temp you want to convert: " << endl;
    		cin >> Celsius;
    		CtoF(Celsius);
    	}
    	cout << "Would you like to conduct another conversion? (Yes/No)" << endl;
    	cin >> continue_ques;
    	}
    	while ( continue_ques == Yes );
    }
    
    
    void FtoC(double n){
    	newCtemp = 0.555555555555556*Fahrenheit-32.0
    	cout << Fahrenheit << "Fahrenheit is equal to " << newCtemp << "Celsius" << endl;
    }
    
    void CtoF(double n){
    	newFtemp = 1.8*(Celsius+32.0)
    	cout << Celsius << "Celsius is equal to " << newFtemp << "Fahrenheit" << endl;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > if( whichconversion == 1 );
    Yeah, those trailing ; are bad news.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    if else statements should not end with ; cause thatll just end that statement

    so it should be like this:
    Code:
    if( whichconversion == 1 ) {
    		cout << "Enter the F temp you want to convert: " << endl;
    		cin >> Fahrenheit;
    		FtoC(Fahrenheit);
    	}else if( whichconversion == 2) {
    		cout << "Enter the C temp you want to convert: " << endl;
    		cin >> Celsius;
    		CtoF(Celsius);
    	}
    Code:
    void FtoC(double n){
    	newCtemp = 0.555555555555556*Fahrenheit-32.0
    	cout << Fahrenheit << "Fahrenheit is equal to " << newCtemp << "Celsius" << endl;
    }
    
    void CtoF(double n){
    	newFtemp = 1.8*(Celsius+32.0)
    	cout << Celsius << "Celsius is equal to " << newFtemp << "Fahrenheit" << endl;
    }
    in here u r MISSING ; in the temp variables

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    You might want to understand the concepts of scopes and statements. Statements ends with a semicolon, but keywords that introduce a scope does not (for example, if, do, while).
    Also, a function can return a value, like:

    Code:
    double CtoF(double n)
    {
    	return 1.8*(Celsius+32.0);
    }
    
    double x = CToF(10.0);
    This way, you can move all your variables inside main. Global variables are considered bad.
    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. Requested registry access is not allowed
    By arby220 in forum C# Programming
    Replies: 1
    Last Post: 06-24-2009, 12:57 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Help Requested
    By heerojyuy in forum C Programming
    Replies: 6
    Last Post: 12-22-2003, 05:15 PM
  4. Suggestions requested on file handling
    By ss3x in forum C++ Programming
    Replies: 0
    Last Post: 02-21-2002, 09:42 PM

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