I am trying to create a program that converts a fahrenheit number to a celsius number, and convert a celsius number to a fahrenheit number. It takes user input and decides which sub-program to run depending on which letter is entered. Once it decides this, it asks for the number to be converted. After it has been converted, it closes the program.

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    char a = 'a';
    char b = 'b';
    char enter;
    float enter_c_to_f;
    float storage_c_to_f;
    float enter_f_to_c;
    float storage_f_to_c;
    cout<<"If you would like to convert Fahrenheit to Celsius,\n type the lowercase letter -a- (without the hyphens).\n If you would like to convert Celsius to Fahrenheit\n type the lowercase letter -b- (without hyphens).\n You also have to make every number a\n decimal (example: 28 = 28.0)\n";
    cin>> enter;
    if (enter == a) {
        cout<<"Enter your number (in fahrenheit) to be converted: \n";
        cin>> enter_f_to_c;
        storage_f_to_c = enter_f_to_c;
        cout<< storage_f_to_c (((*5)/9)+32) <<;
    }

(this is the first section of the program, i'm trying to figure out the first section before i move on to the second so it is less confusing). Whenever I compile this code it displays the following errors:

error: invalid type argument of unary '*' (have integer)
error: 'storage_f_to_c' cannot be used as a function
error: expected primary-expression before ';' token

All of the errors above where on line 21. Thank you in advance for the help!

P.S. I have also tried changing the code:

storage_f_to_c = enter_f_to_c;
cout<< storage_f_to_c (((*5)/9)+32)

To the code:

storage_f_to_c = enter_f_to_c (((*5)/9)+32);
cout<< storage_f_to_c <<;

It didn't work either.

Edited 4 Years Ago by coolikedat99

error: 'storage_f_to_c' cannot be used as a function
cout<< storage_f_to_c (((*5)/9)+32) <<;

You should take the error literally, because you are attempting to use "storage_f_to_c" as a function. Here is your code with an added variable...

a=((*5)/9)+32;
cout<< storage_f_to_c(a);

It's much more clear now, isn't it? You are trying to call a function which is actually a variable. I haven't looked at the others too much, but the last one seems like it's caused by the extra <<, and the other because you are attempting to use 5 as a pointer, but I could be wrong.

Edited 4 Years Ago by DrunkenCoffin

There are a few things wrong with your code and you can make a few improvements too.

You are getting those errors because your syntax is wrong. If you wanted to input that equation it should look like cout << (storage_f_to_c - 32.0f)*5.0f/9.0f << endl;. So not only is the equation incorrect, but you had the multiplication operator with a closing bracket to the left (you would never see this in written math or input for a computer). Another problem is that you have "<<" at the end of the line and that is looking for a right-hand operand.

Some imporovements that you could make would be to use a variable like "input" to store both enter_f_to_c and enter_c_to_f (and if you were to expand this to Kelvin and Rankine then you would need loads more variables), also should make "result" hold the storage variations of these variables and the variables enter, a, and b can go into a single char variable called "option" and to select which equation to use you can check it with a switch() statement.

#include <iostream>
#include <cmath> //in C++ use cmath not math.h (this goes for all the standard .h headers from C)

using namespace std;

int main()
{
    char option;
    float input;
    float result = 0.0f;

    cout << "If you would like to convert Fahrenheit to Celsius,\n type the lowercase letter -a- (without the hyphens).\n If you would like to convert Celsius to Fahrenheit\n type the lowercase letter -b- (without hyphens).\n You also have to make every number a\n decimal (example: 28 = 28.0)\n";
    cin >> option;

    switch( option )
    {
        case 'a':
            cout << "Enter your number (in fahrenheit) to be converted: \n";
            cin>> input;
            cin.get(); //since reading in integers leaves the newline
                       //character at the end of the stream we need to pull that out too
            result = (input - 32.0f)*5.0f/9.0f;
            break;

        case 'b':
            cout << "Enter your number (in fahrenheit) to be converted: \n";
            cin>> input;
            cin.get(); //since reading in integers leaves the newline
                       //character at the end of the stream we need to pull that out too
            result = input*9.0f/5.0f + 32.0f;
            break;

        default:
            break;
    }

    cout << result << endl;

    cin.get(); //pauses program at the end (Visual Studio auto closes)
    return 0;
}

Thank you for helping get rid of one of the errors. But the other two are still showing up. Do you know another way of multipling (besides using *

when multiplying, you can try and bracket the variables on either side, then apply the operator

All three errors are from line 23 in your original post. If you read my post then you should have been able to correct that line and you would not have any more errors.

Thank you for all of the help guys! This is my final product. Sorry for the post about
only helping to of the problems. I was telling it to DrunkenCoffin not Sfuo (I posted it a little bit after Sfuo posted the solution). Also thank you for the cmath information.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    char a = 'a';
    char b = 'b';
    char enter;
    float enter_c_to_f;
    float storage_c_to_f;
    float enter_f_to_c;
    float storage_f_to_c;
    cout<<"If you would like to convert Fahrenheit to Celsius,\n type the lowercase letter -a- (without the hyphens).\n If you would like to convert Celsius to Fahrenheit\n type the lowercase letter -b- (without hyphens).\n";
    cin>> enter;
    if (enter == a) {
        cout<<"Enter your number (in fahrenheit) to be converted: \n";
        cin>> enter_f_to_c;
        storage_f_to_c = enter_f_to_c;
        cout<< (storage_f_to_c - 32.0f)*5.0f/9.0f;
    }
    if (enter == b) {
        cout<<"Enter your number (in celsius) to be converted: \n";
        cin>> enter_c_to_f;
        storage_c_to_f = enter_c_to_f;
        cout<< storage_c_to_f*9.0f/5.0f + 32.0f;
    }
}

One more thing. How would I be able to display if the number is too long. I'm using the latest version of Code::Blocks, and if I enter too many numbers is displays inf and finishes the program. I would also like it to say: Error: The number entered is too large to be calculated. Please try again. The largest number my compiler will let me use is a 45 digit number (all nines) If you enter more than that it will display inf I would also like it to say the thing up top ^ Anyways I got it to work, but the problem is it ALWAYS displays the error message. I wasn't sure if I made a mistake in this code, or if I placed it in the wrong part of the program, or what I did wrong. Could you guys (and gals) please help me to finish this error message?

`if (enter_c_to_f >= one || enter_f_to_c >= one) {
        cout<<"\nError: Number entered is too large to be calculated. Please try again.\n";
    }`

Edited 4 Years Ago by coolikedat99

[pedantic]
Since the hottest recorded temperature is a mere 3.6 billion degrees, why would you care if the program worked for 10 Quattuordecillion?
[/pedantic]

Actually, the problem is the limits of binary math. 45 digits is too many for computers handle. The only way to cleanly deal with numeric abnormalities with any certainty is to input everything as strings and do a verification before converting to binary.

And before anyone says "don't use CodeBlocks -- it's too old" see this

Edited 4 Years Ago by WaltP

For a case like this you could use something like this to make sure that you are geting the input you want

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::stringstream ss;
    std::string input;
    int number;

    std::cout << "Please enter a number less than 45 digits: ";
    std::cin >> input;
    std::cin.ignore();  // clear buffer so we can pause at the end
    if(input.size() < 45)  // check if the size is greater than 45
    {
        ss << input;  // input string into the stringstream
        ss >> number;  // output the contents of the stringstream into the interger
    }
    else
        std::cout << "You entered a number that is to large.";
    std::cin.get();
    return 0;
}

Edited 4 Years Ago by NathanOliver

This question has already been answered. Start a new discussion instead.