Variable may be uninitialized/is used uninitialized


Variable may be uninitialized/is used uninitialized



I'm writing an English-Metric converter program in C++, and I'm trying to use try, throw, catch to reject negative/non-numeric values in the 'main' function.



My two problems are:



1.)
Whenever I enter, say, 'g' into the console, I get an output: 0 inches is equal to 0 centimeters AND THEN I get the error display that I want to pop up. What i need is only the error display to be output.



2.) When I enter a negative number, like -3, I get the proper conversion as a negative number when I would like it to tell me my input is invalid.



Here is my code.


#include <iostream>
#include <cctype>
char menuSelect();
using namespace std;

int main(int argc, const char * argv)
{
double inches;
double centimeters;
char select;
try
{
do
{

if (centimeters < 0.0 || inches < 0.0)
throw 0;
if (!cin)
throw 0;

select = menuSelect();
if (select == 'E')
{
cout << "Enter the number of inches: ";
cin >> inches;
centimeters = inches * 2.54;
cout << inches << " inches is equal to " << centimeters
<< " centimeters." << endl;
}
else if (select == 'M')
{
cout << "Enter the number of centimeters: ";
cin >> centimeters;
inches = centimeters / 2.54;
cout << centimeters << " Centimeters is equal to " << inches
<< " inches." << endl;
}

} while (select != 'Q');

}
catch (int errID)
{
cout << "Error: " << errID << endl;
cout << "Please enter a positive number. ";
}
return 0;

}





Cannot reproduce: "undefined reference to `menuSelect()'" Minimal, Complete, and Verifiable example please.
– user4581301
Jul 2 at 3:14





inches and centimeters are both uninitialized where you first use them. If you want to validate your input you'll need to do it where you actually accept it, not in another iteration of the loop.
– Retired Ninja
Jul 2 at 3:15






Don't test for both centimeters < 0.0 and inches < 0.0. You might have only set one of them. Worse, if you want centimeters checking inches is not necessary AND you may error out over an old value negative value in inches.
– user4581301
Jul 2 at 3:22


centimeters < 0.0


inches < 0.0


centimeters


inches


inches





Please don't use exceptions for flow of control on the main program path. That does not make for good C++.
– StoryTeller
Jul 2 at 5:07






You're checking if the input is invalid before you read them and after you outputted the result. Consider going through your program line by line in the order that it gets executed and explain everything to a rubber duck.
– Fei Xiang
Jul 2 at 18:32





1 Answer
1



It should be something like that: (I added a flag bool bNeg = false, if (inches>=0) and if (centimeters>=0) just after the input & if the input is negative for either 'E' or 'M' set bNeg = true)


bool bNeg = false


if (inches>=0)


if (centimeters>=0)


bNeg = true


bool bNeg = false;

if (select == 'E')
{
cout << "Enter the number of inches: ";
cin >> inches;
if (inches>=0) { // No meaning to negative values
centimeters = inches * 2.54;
cout << inches << " inches is equal to " << centimeters
<< " centimeters." << endl;
}
else bNeg = true;

}
else if (select == 'M')
{
cout << "Enter the number of centimeters: ";
cin >> centimeters;
if (centimeters>=0) { // No meaning to negative values
inches = centimeters / 2.54;
cout << centimeters << " Centimeters is equal to " << inches
<< " inches." << endl;
}
else bNeg = true;
}

if (bNeg) {
// tbd: say what you want to say
}





I recommend marking and explaining what you changed and why you changed it. Answer looks good, but with nothing but code, the asker might learn nothing but an incremental improvement in their cut and pasting skills.
– user4581301
Jul 2 at 6:32






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?