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;
}
}
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 int
value (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 (ie: 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
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.