Hello,
I am trying to create a time conversion calculator and I think I have it down but I do not know how to get it to display just one answer per selection. Like I can select 1 and it shows results for 1 and 2 . . or I can select 3 and it says program terminated and it also shows results for 2. I'm sure it's something simple but I can't figure it out . . please help?

Thank you!

/* Specification:
This converts time  */

#include <iostream>

using namespace std;

namespace time{
     enum menuOptions {Minutes, Hours, Seconds, Modulus, Exit};
}

int main (){
         //ask the user for minutes
	   const int timeCalc = 60;
       int minutes = 0;
       int hours = 0;
       int seconds = 0;
       int modulus = 0;
       cout << endl << "Welcome to the Time Conversion Calculator!\n";
       cout << endl << "Enter minutes: ";
       cin >> minutes;
       cout << endl << endl;

        //ask the user for desired conversion method
        int convMeth = 0;

        cout << "Choose a conversion method:\n\n";
        cout << time::Hours << " - Minutes to hour(s) and minutes\n";
        cout << time::Seconds << " - Minutes to seconds\n";
        cout << "Make your selection: ";
        cin >> convMeth;
        
     //perform calculations and output result
        if (convMeth < 1 || convMeth > 2)
       {
        cout << endl << "Invalid choice - program terminated\n\n";
        }
        if (convMeth == 1)
       {
        hours = minutes / timeCalc;
        modulus = minutes % timeCalc;
        cout << endl << "Result:  " << minutes << " minutes equals "   << hours << " hour(s) and " << modulus << " minutes. \n\n";
        }
        if (convMeth = 2)
       {
        seconds = minutes * timeCalc;
        cout << endl << "Result:  " << minutes << " minutes equals " << seconds << " seconds. \n\n";
       }
            cout << endl << endl << "end of program\n\n";

return 0;
}

Recommended Answers

All 9 Replies

Line 44

if (convMeth = 2)

should be

if (convMeth == 2)

Do you understand this ?

if( something) { do something }

else if( it is something else ) { then do something else }

else if( it is something else) { then do something else }

The elses increase efficiency, but my point is " if (convMeth = 2) " always evaluates to true, so that if block will always execute. Adding elses in the existing code will definitely prevent both cases from executing, but if a third was later added, the logic would never get beyond the second test.

The elses increase efficiency, but my point is " if (convMeth = 2) " always evaluates to true, so that if block will always execute. Adding elses in the existing code will definitely prevent both cases from executing, but if a third was later added, the logic would never get beyond the second test.

Yep, so what are you trying to achieve again?

me? nothing, just trying to get jenn on the right track :)

Line 44

if (convMeth = 2)

should be

if (convMeth == 2)

Yes that did it, thank you.

Do you understand this ?

if( something) { do something }

else if( it is something else ) { then do something else }

else if( it is something else) { then do something else }

I do and I was trying to use the else and I couldn't get it to work for some reason . . I just started doing this a couple of weeks ago so bear with me.

I did change the = to == and everthing seemed to be fine . . I got the results I was looking for so that is good :)

Yep, so what are you trying to achieve again?

I guess I am kind of confused as to when to use the if/else structure . . is it poorly written the way that I have done it??

I was just trying to get one result to print at a time . . kept getting 2 or all 3 (including program terminated) printing at the same time with any input I entered.

The difference between using "if/else" and using "if" multiple times is each "if" block gets checked all the time, and "if/else" will skip all remaining blocks after one of them resolves to true. For example, in this code:

int i = 0;
if (i < 1) // test a
{
    print("i is less than one!");
}
if (i < 2) // test b
{
    print ("i is less than 2!");
}

compare with:

int i = 0;
if (i < 1) // test c
{
    print("i is less than one!");
}
else if (i < 2) // test d
{
    print ("i is less than 2!");
}

In the "if/if" block, test a resolves to true, and prints out our message. Then, test b resolves true and prints our second message. In the "if/else" block, test c resolves true and prints our message. Test d is skipped, because the first one passed, so no second message gets printed.

Summary:
test a: resolves true, message printed
test b: resolves true, message printed
test c: resolves true, message printed
test d: skipped (because c passed), message not printed.

If we set i = 1 in both examples, then we get:
test a: resolves false, no message
test b: resolves true, message
test c: resolves false, no message
test d: not skipped (because c failed), message printed.

if/else is great when you want only one of the options processed. If it is possible for multiple matches, then use multiple ifs. Hope this helps!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.