-5
#include <iostream>  
using namespace std;

int main()
{
    string feet;
    string conversion;
    float value;


    cout << "What's you current unit?" ;
    cin >> conversion;


    if (value == feet)  {          //the program only runs this part and doesnt ask for input, just gives 0 as a value
    cout << "Enter a number: ";
    cin >> value;

    value = value/3.281;

    cout << value << "meters";

    }
    else {
    cout<<"Enter a number: ";
    cin>> value;

    value = value*3.281;

    cout << value << "feet";

    } 

    return 0;

}

I left my problem in the comment above. I think I'm missing something, cause its not running completely and for some reason its giving me a value of 0.

user4581301
  • 33,082
  • 7
  • 33
  • 54
anon_123
  • 1
  • 1
  • 2
    Post your code (no links) and specify where you are having a problem. – Anon Mail May 21 '19 at 19:51
  • Now that you fixed the `double` / `std::string` bug. `feet` is an empty string. Maybe you don't want a feet variable at all. You probably wanted `if (conversion == "feet") {` – drescherjm May 21 '19 at 20:23
  • At `if (value == feet)`, `value` has no defined value, nor can it be directly compared with a `string`. This cannot compile. If your code runs, it is running a stale copy of the executable. – user4581301 May 21 '19 at 20:23
  • Your program has _undefined behavior_, You're accessing `float value;` though it never has been initialized. ` – πάντα ῥεῖ May 21 '19 at 20:28

2 Answers2

2

There are some problems in your code, the main one being that you use the value of feet before it is initialized. This is undefined behaviour and needs to be fixed first.

I presume you wanted something along the line of

#include <iostream>  
#include <string>

int main()
{
    const double conversion = 3.281;
    std::string unit;
    double value;

    std::cout << "What's you current unit?" ;
    std::cin >> unit;
    std::cout << "Enter a number: ";    
    std::cin >> value;

    if (unit == "feet")  {
        std::cout << value / conversion << " meters";
    } else {
        std::cout << value * conversion << " feet";
    } 
} 

What I changed:

  • using namespace std; is considered bad pratice. I personally simply fail to see the advantage of using it.
  • you had a value == feet when value was a float and feet a string, but there is no operator== for those two types.
  • Magic numbers should be avoided. In different words: Never repeat a number literal in your code, instead give it a name and use that.
  • Most of the code in the if cases was identical, so it could be moved outside.
  • Be strict and consistent with names, it helps to get the logic right. Asking the user for their "unit" and storing the input as conversion cannot be right.

...and I almost forgot to #include <string> ;)

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1
#include <iostream> 
#include <string>
using namespace std;

int main() {
    string conversion;
    float value;


    cout << "What's you current unit?\n";
    cin >> conversion;

    for (auto& c : conversion)
        c = tolower(c);

    if (conversion == "feet") {
        cout << "Enter a number: ";
        cin >> value;

        value = value / 3.281;

        cout << value << " meters";

    }
    else {
        cout << "Enter a number: ";
        cin >> value;

        value = value*3.281;

        cout << value << " feet";

    }

    system("pause");
    return 0;

}

If you need help understanding the code, ask in comments.

Jihad92
  • 43
  • 8