Need help please?

So, Im new to C++, just started my TAFE course (Technical and Further Education)

I have homework to do, and it is to create a command line program with a functional use; I chose a Celcius to Farenheit converter.

Here is my code, Im not entirely sure where im going wrong though...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <iostream.h>
#include <conio.h>

using namespace std;

int main()
{
    cout << "Farenheit to ceclius:F=9/5C+32 Celcius to Farenheit: 9/5*C+32" <<std::endl;
    system ("pause");
    
    int X;
    char Y;
    
    cout<< "Enter Degrees Celcius/Farenheit";
    cin>>X;
    cout<< "Enter your choice (Celcius-Farenheit [D] Farenheit-Celcius [F]";
    cin>>Y;
    
    Switch(Choice);
}

{
                  Case 'F':   cout<< "Farenheit-Celcius="<<9/5*X+32;
                              break;
                  
                  Case 'D':   cout<< "Celcius-Farenheit="<<X*9/5+32;
                              break;
                  
                  Default:    cout<< "Invalid Choice!";

    getch ();
    return 0;
}



Every time I try to compile the program (BTW I am using Dev-CPP 4.9)
I get these errors:

'Choice undeclared (First use of function)'
'switch undecalared (first use of function)'

There was also some problems to fo with an outdated header, but i know about that, i need the "iostream.h" because one of the functions doesnt wok with just "iostream"

I think I know whats wrong with string and Choice, Did i miss a library or something?

Also if there is any other blindingly obvious mistakes it would be duly noted if you told me :P

Thanks :P
in the switch it should be
Switch (Y)
Not
Switch (choice)
closed account (17ihb7Xj)
Also if there is any other blindingly obvious mistakes it would be duly noted if you told me :P


This necessarily isn't a mistake, but it's a convention to use descriptive variables to help understand your code.

For example, you could change int X to something a little more descriptive, such as int degrees, or int temperatureValue. Better yet, you could change the integer variable to float types, and be able to handle decimal values as well. You could change your char Y into something more meaningful as well, such as char choice.

Another thing, C++ is a case-sensitive programming language. Your Switch statement should be lowercase, as well as your Case statements.

1
2
3
4
5
6
7
8

switch (choice)
{
     case 'F' || 'f': //handles the user inputting a lowercase character
          //place code here
     case 'D' || 'd':
          //place code here
}
Last edited on
closed account (3qX21hU5)
The main problem it wont work is this. Your switch statement is outside of main.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    Switch(Choice);
}

{
                  Case 'F':   cout<< "Farenheit-Celcius="<<9/5*X+32;
                              break;
                  
                  Case 'D':   cout<< "Celcius-Farenheit="<<X*9/5+32;
                              break;
                  
                  Default:    cout<< "Invalid Choice!";

    getch ();
    return 0;
}


Notice that right after switch (choice) you have a closing }. Also you have a ; after switch (choice) which shouldn't be there. Here is what it should look like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    Switch(Y)  // No ;
    { // Beginning of switch
        Case 'F':   
            cout<< "Farenheit-Celcius="<<9/5*X+32;
            break;
                  
        Case 'D': 
            cout<< "Celcius-Farenheit="<<X*9/5+32;
            break;
                  
        Default:  
            cout<< "Invalid Choice!";
    } // End of switch statement

    getch ();
    return 0;
} // End of main 


That is the main things that I seen in there. Here is a few tips also. First upgrade your IDE and compiler to a new up to date version. Bloodshed Dev C++ has not been updated in over 5 years so you are missing a lot of features and it has many bugs that will never be fixed. If you like the way Dev C++ look you can check out Orwell dev C++ which has just been updated recently (5.3 I think?). Otherwise some other good IDE's are Codeblocks, or Visual Studio 2012 Express. All of them are good ones.

Second I would recommend that you rethink your formatting of your code. It has a lot of indentation and it quick long. The general rule of thumb is not to have more then 80 characters on one line. The IDE's I have just mention earlier will have feature in them that tells you when you go over 80 characters. Now everyone has their own style for programming and you don't need to change it if you don't want to but it will make it easier to read for other people. The most important thing is to be consistent with whatever style you use.
Last edited on
Lots of upper-case e.g. "Switch" and "Case" which should be lower-case.
Incorrect formula (same used in both cases). Integer division, which would give incorrect result.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    cout << "Fahrenheit to Celsius: F=(F-32)*5/9\nCelsius to Fahrenheit: 9/5*C+32" << endl;

    float x;
    char choice;

    cout << "Enter Degrees Celsius/Fahrenheit  ";
    cin >> x;

    cout<< "Enter your choice (Celsius-Fahrenheit [C] Fahrenheit-Celsius [F]  ";
    cin>> choice;

    switch (choice)
    {
        case 'F':
        case 'f':
            cout << "Fahrenheit-Celsius="<< (x-32)*5.0/9.0 << endl;
            break;

        case 'C':
        case 'c':
            cout<< "Celsius-Fahrenheit=" << x*9.0/5.0 + 32 << endl;
            break;

        default:
            cout<< "Invalid Choice!";
    }

    getch();

    return 0;
}



One thing which stands out is the use of an outdated compiler. Latest DevC++ is version 5.3.0.4 http://orwelldevcpp.blogspot.co.uk/ though other folks recommend code::blocks http://www.codeblocks.org/
Last edited on
@MrJake
Oops - not quite right.
 
    case 'F' || 'f': // Wrong 

Should be
1
2
        case 'F':
        case 'f':

closed account (17ihb7Xj)
Right, getting confused between my work project and the OPs question. Shouldn't be coding and working on that at the same time. :p
Thanks, I've been checking a lot of tutorials and Ive got it working now and added a few things to make it easier to use, like a return function so you can convert something else without restarting the program

Thanks :P
Topic archived. No new replies allowed.