2

I am a beginner and I am taking my first C++ programming class. We are using an IDE named Codeblocks and I am stumped. I have searched the forums for an answer but I just cannot figure it out on my own.

I am trying to figure out why my code is not exiting when entering 3 in the menu on command prompt. Also I am trying to figure out why when I am trying to convert fahrenheit to celsius my formula is not working, when I believe it is the correct formula.

The code is below:

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main () 
{

    float celsius;
    float fahrenheit;
    char x;

    //Menu for user to choose which option they would like to
    //preform. Also looping in case they type in the incorrect
    // response and would like to choose again.
    do
    {
        cout << "Please choose an option. Then please press Enter. \n";
        cout << "1. Convert Celsius to Fahrenheit.\n";
        cout << "2. Convert Fahrenheit to Celsius. \n";
        cout << "3. Exit Program \n";
        cin >> x;
        if (x == '1')
            system ("cls");
        {
            cout << "Please enter degrees in Celsius.  \n";
            cin >> celsius;
            system ("cls");

            fahrenheit = 9.0 / 5 * celsius + 32;
            //Calculate the formula for converting Celsius to Fahrenheit.
            cout << fixed << showpoint << setprecision(2);
            cout << fixed << "The degrees in Fahrenheit is \n" << fahrenheit;
            cout << static_cast<char>(248) << endl;
            cout << "Thank you have a great day!";

            (x = '3');

        }



        // User does not want to convert Celsius to Fahrenheit
        // Since user does not want to convert, display a Thank you message.


        if (x == '2')
        {
            cout << "Please enter degrees in Fahrenheit.  \n";
            cin >> fahrenheit;


            celsius = (fahrenheit - 32) * 5.0 / 9 ;

            //Calculate the formula for converting Fahrenheit to Celsius.
            cout << fixed << showpoint << setprecision(2);
            cout << fixed << "The degrees in Celsius is \n" << celsius;
            cout << static_cast<char>(248) << endl;
            cout << "Thank you have a great day!";

            (x = '3');

        } while (x != '3')

            return 0;

    }
}
vsoftco
  • 55,410
  • 12
  • 139
  • 252
A. Brown
  • 21
  • 1
  • 1
    I'm going to write an answer, but I think the most pressing issue for you right now should be your code formatting quality. I'll rewrite your main with some tips on formatting style as well as fixes for that code itself. – Aidan Gomez Oct 03 '15 at 01:38

3 Answers3

0

Your code formating is not very good. Also I don't get the do-while loop, when you're ending the program after the conversion anyway.

Here is my version:

#include <iostream>
using namespace std;

int main()
{
    float celsius;
    float fahrenheit;
    char x;

    cout << "Please choose an option. Then please press Enter. \n";
    cout << "1. Convert Celsius to Fahrenheit.\n";
    cout << "2. Convert Fahrenheit to Celsius. \n";
    cout << "3. Exit Program \n";
    cin >> x;

    if(x == '1')
    {
        cout << "Please enter degrees in Celsius.  \n";
        cin >> celsius;
        system("cls");

        fahrenheit = 9.0 / 5 * celsius + 32;

        printf("The degrees in Fahrenheit is %0.2f%c\nThank you have a great day!", fahrenheit, (char)248);
    }

    else if(x == '2')
    {
        cout << "Please enter degrees in Fahrenheit.  \n";
        cin >> fahrenheit;
        system("cls");

        celsius = (fahrenheit - 32) * 5.0 / 9;

        printf("The degrees in Celsius is %0.2f%c\nThank you have a great day!", celsius, (char)248);
    }

    else
    {
        return 0;
    }

    getchar();
    getchar();
    return 0;
}
ProXicT
  • 1,903
  • 3
  • 22
  • 46
0

I recommend you check out some code style guides for c++. It's development community are generally very strict with how they want things to be done.

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main () {

    float celsius;
    float fahrenheit;
    char x = 0;

    while (x != 3) {
        cout << "Please choose an option. Then please press Enter. \n";
        cout << "1. Convert Celsius to Fahrenheit.\n";
        cout << "2. Convert Fahrenheit to Celsius. \n";
        cout << "3. Exit Program \n";
        cin >> x;

        if (x == 1) { // You need to wrap if statement with brackets
            cout << "Please enter degrees in Celsius.  \n";
            cin >> celsius;

            fahrenheit = 9.0f / 5.0f * celsius + 32;
            //Calculate the formula for converting Celsius to Fahrenheit.
             cout << fixed << showpoint << setprecision(2);
             cout << fixed << "The degrees in Fahrenheit is \n" <<fahrenheit;
             cout << static_cast<char>(248) << endl;
             cout << "Thank you have a great day!";
        } // here is the first "if"s terminating bracket
        // (x = '3'); I think you're trying to say ("if x == 3")? Which still isn't necessary
        // there was an absurd amount of space here, should only be one or two lines

        else if (x == 2) { // spaces around operator and again your "if" brackets were wrong
            cout << "Please enter degrees in Fahrenheit.  \n";
            cin >> fahrenheit;

            celsius = fahrenheit - 32 * 5.0f / 9.0f; // both sides of a divisor should be double for clarity

            //Calculate the formula for converting Fahrenheit to Celsius.
            cout << fixed << showpoint << setprecision(2);
            cout << fixed << "The degrees in Celsius is \n" <<celsius;
            cout << static_cast<char>(248) << endl;
            cout << "Thank you have a great day!";
        }

        //(x = '3'); again, what is trying to be done here?

    } 

return 0;

}

In your case, you probably don't want to be using a do-while loop since you can simply zero-initialize x.

You also are better of using "if-else" statements or even "case-switch" control statement (both can be googled; I've shown "else-if").

You also don't handle invalid input, but that's not required for basic functionality.

Always indent code within control statements and method declarations

if () {
    int a = 0; // correct
int b = 1; // incorrect
}

Optional: You also seem to drop your open bracket below your control statements like below:

if (x == 1)
{
    // by some standards incorrect
}
// as opposed to
if (x == 1) {
    // correct by most standards
}

Your (x = 3); lines are simply saying x = 3; which will immediately terminate your loop if you don't enter a 1 (if you were to enter a 2, the x = 3; assignment below the if (x == 1) statement would terminate your program before it could reach the if (x == 2).

In summation, code styling is considered by most to be among the principals of greatest importance. I would strongly recommend you read and abide by a strict set of rules. Such as Google's C++ Style Guide.

Community
  • 1
  • 1
Aidan Gomez
  • 8,167
  • 5
  • 28
  • 51
  • Thank you. I am a first year programming student. I will take the tips here and I will for sure read and use the C++ style guide. Sorry for the code, I didnt realize it was that bad. I do understand most of what you were telling me in the comments of the code you re-wrote. – A. Brown Oct 03 '15 at 03:48
  • @A.Brown Best wishes with your coding career! As far as stack overflow, you can accept the answer that solves your question ;) – Aidan Gomez Oct 03 '15 at 04:13
-1

For starters what you should do is instead of having char x you should make it int x since you are dealing with numbers as you inputs. This is probably why your code is not working because you are using integers as your values for chars. Your code should look like this:

if(x == 2); // and so on for the rest of the places you use x;

As for your problem with entering 3 as the value of x, in your code you gave x the value of three like this:

(x == '3') 

even though you aren't supposed to use parentheses. Your code should look like this:

x = 3; // This is after you declare x as an int value

Another thing that you should try doing is instead of using

float celsius;
float fahrenheit;

try doing

double celsius;
double fahrenheit;

since you are dealing with double data type.

I hope this was helpful and have a good time coding.

  • He's using char variable as the input and nothing is wrong with that. He's always checking for char values and not for int. Int would be memory inefficient, since it has 4 bytes, while char is 1 byte. Float variable is (as the name suggests) data type for floating point numbers, so using float is ok. Double is just (as the name suggests again) for double precision. – ProXicT Oct 03 '15 at 02:13