-2

I'm a beginner at programming and I'm trying to make a Kilometer and Miles converter application. I'm using codeblocks for my code. Choice 2 keeps giving me 0 as the result. Here's the exact code I'm typing:

#include <iostream>
using namespace std;
double choice, value1, result;
// I'm sure I messed up somewhere after this:
double value2 = .621371;
double value3 = 1.609344;
// Are these two lines ^ supposed to be here?
int main()
{
    while (true) {
        cout << "1. Kilometers to Miles" << endl;
        cout << "2. Miles to Kilometers" << endl;
        cout << "3. Exit the Application" << endl;
        cout << "Please enter a choice: ";
        cin >> choice;
        if (choice == 3)
            break;
        cout << "Please enter the first value: ";
        cin >> value1;
        // This if statement keeps giving me 0:
        if (choice == 2)
            result = value1 * value2;
        // I believe this part here is okay:
        else if (choice == 3)
            result = value1 / value3;
        cout << "The result is: " << result << endl;
    }
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
MrAckills
  • 9
  • 1
  • 1
    Uh? Do you check two times if `choice` is `3` but none if `choice` is `1`? That's why a `switch` beats all thos chained `if else` – SJuan76 May 01 '16 at 23:40
  • Also, don't use `double` for your choice. That's where `int` (or another integral type) is very much appropriate. It won't cause the error in your case, but comparing doubles like that you're headed for trouble down the road. You'll learn about that when you learn about floating point inaccuracy. – Stjepan Bakrac May 01 '16 at 23:41
  • I've not yet learned the switch statement. I'm going to learn it soon though. – MrAckills May 01 '16 at 23:42
  • Well, one thing that's wrong with this code is that it's not properly indented, and is not very readable because of that. – Sam Varshavchik May 01 '16 at 23:50
  • I'll be sure to properly indent next time. Thanks Sam. – MrAckills May 01 '16 at 23:52
  • 1
    BTW, I used http://format.krzaq.cc/ to fix the formatting. – drescherjm May 02 '16 at 00:39
  • @drescherjm Yeah I saw that you edited it. Thanks for your edit. Really appreciate it. – MrAckills May 02 '16 at 00:53

2 Answers2

0

Here's how to fix your code:

1.Indent properly (Makes your code more readable)
2.This is making you code not work your code and is also very much unnecessary:
if(choice == 3) { break; }
3.Every single one of your if/else/else if statements need to have those curly braces after them:

if(whatever=whatever) {
     //whatever
}

4. Forget about using those break statements for programs this simple.
5. You missed "Kilometers to Miles".
6. The exit option is screwed up, change

else if(choice == 3)
result = value1 / value3  

to

else if(choice == 3) {
    return 0; // To end the program
}

7. Don't double the choice variable, it uses much, much, much, much more data then a simple intvalue (and what if the user enters 3.1 (or 4.838389, ect.) as a input?
8. You should make the precision of your result less, (you don't want 3.37374662232365 miles/kilometers, you want 3.3 miles/kilometers.) You can do that by also adding #include <iomanip> and adding in setprecision() before the declaring what "result" is (i.e: result = setprecision(1) value1 * value2;)
9. Including the whole std namespace is inappropriate in this case (it will slow your program down ALOT), so you should write std:: before each std function, or just write using std::whatever

Tacocat 4642
  • 13
  • 1
  • 1
  • 7
  • This really helps. Well I guess you are the best answer. I'll be sure to indent properly next time as well. I'm just a beginner so the setprecision(), #include etc. I did not know but now I do and I'll make sure to fix my mistakes. Thanks. – MrAckills May 02 '16 at 00:58
  • @user6279160 I'm a beginner too, I started 3 months ago but I just know a lot. I can provide a fully fixed and upgraded code for you if you want... – Tacocat 4642 May 02 '16 at 01:33
  • I just started learning about a few weeks ago. And yeah that would be great and very helpful, if of course it's of no problem to you. I just learning the switch statements and I'm trying to find a away to implement them into the current code. I believe by learning all this stuff now I should just delete the code and start from scratch. – MrAckills May 02 '16 at 01:40
  • @user6279160 kk, I will have some code for you edited into the answer tomorrow, but I still recommend you create a new code for your program. I believe that you will be a really awesome programmer soon! – Tacocat 4642 May 02 '16 at 01:46
  • Well thank you for the compliment. I will start off from scratch again. I will take your help into consideration tomorrow, because I am finished with the day. I will keep you updated with the progress. Your help is much appreciated. The if statements are appropriate for this situation right? – MrAckills May 02 '16 at 02:01
  • @user69279160 Yes, I think if statements are perfectly fine, I use them all the time and nothing has gone wrong yet. – Tacocat 4642 May 02 '16 at 02:07
  • 2
    A lot of these statements are either misleading or plain incorrect. (2) would not cause any issues and is fine in this case, (3) is wrong as you don't need braces around single line if-clauses. (7) is misleading (it doesn't use much more data), (8) is misleading and incorrectly formatted whilst (9) is also incorrect - it doesn't slow the program down, it's just very bad practice. – sjrowlinson May 02 '16 at 17:26
  • `if(whatever=whatever)` is very incorrect, and `using namespace std` will not slow down the program in any way. It's just bad and shouldn't be used due to namespace pollution – phuclv May 05 '16 at 02:37
0

I fixed the Problem Now. If any one can also comment below to me, is this a lot better than my original code? Basically is this new code a lot more readable to you all and is it more organized? I would love to get y'all's opinion on this updated code.

    #include <iostream>
float choice, value1, result;

int main(){
    while (true){
    std::cout << "Please enter a choice: " << std::endl <<
                 "1. Kilometers to Miles" << std::endl <<
                 "2. Miles to Kilometers" << std::endl <<
                 "3. Exit the Application" << std::endl;
    std::cin >> choice;
    if(choice == 3){
        break;
    }
    std::cout << "Enter the Value you would Like to Convert: ";
    std::cin >> value1;
    if (choice == 1){
        result = value1 * 0.62137;
    }
    else if (choice == 2){
        result = value1 * 1.609344;
    }
    std::cout << "The result is: " << result << std::endl;
    }
}
MrAckills
  • 9
  • 1