Need some advice with C++ Assignment

Can anyone give me some advice
I need to make the source code for Create a C++ console application that converts a time entered in minutes into either a hour/ minute format or into seconds.
It must contain a structure a decision statement and enumeration and constants for the conversion factors. Hers’s what I have so far but I cannot figure out the rest.


#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
enum menuOptions {hrsMin = 1, secs = 2, exit = 3};

//Displays menu
cout << "Please make a selection:\n";
cout << hrsMin << " - Hours and Minutes\n";
cout << secs << " - Seconds\n";
cout << exit << " - Exit\n";
cout << "Enter your choice: ";
int usersChoice = 0;
cin >> usersChoice;
//Performs selected operation
int min;
int hh , mm;
int sec;
cout << "Enter minutes: ";
cin >> min;
if (usersChoice == 1);
hh = min / 60;
mm = min % 60;
if (usersChoice == 2);
sec = min % 60;
if(usersChoice == 3);
cout << "You have chosen to exit";

cout << setw(2) << setfill('0') << hh << ":"
<< setw(2) << setfill('0') << mm << '\n';

return 0;
I could not quite understand what you need but here is something.
An enum is unnecessary for the menu.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
     // Variables
     int usersChoice = 0;
     int min;
     int hrs;
     int sec;

while (1) // Main program loop
{
     //Displays menu
     cout << "Please make a selection" << endl;
     cout << "1 - Hours and Minutes" << endl;
     cout << "2 - Seconds" << endl;
     cout << "3 - Exit" << endl;
     cout << "Enter your choice: ";
     
     // Ask for menu choice
     cin >> usersChoice;

     //Main operations
     if (usersChoice == 1){
          cout << "Enter minutes: ";
          cin >> min;
          system("cls");
          cout << min << " Minutes = " << min / 60 << " Hours, and " << min % 60 << " Minutes" << endl << endl;
          }


     if (usersChoice == 2){
          cout << "Enter minutes: ";
          cin >> min;
          system("cls");
          cout << min << " Minutes = " << min * 60 << " Seconds" << endl << endl;
          }


     if (usersChoice == 3){
          system("cls");
          cout << "Closing";
          return 0;
          
          }
     }
}
Last edited on
As favor929 said, enumerations aren't really needed but if you say it must have one, then it is extected you use the enum constants at a later time. So you may declare usersChoice as the enum type.

Hmm...advice...
- Don't put an ; after the if statements*
- Are you sure that the user will get 00:00 if exit is chosen? If not, you have to put that before the if statement of choice 3.
- It seems that setw() and setfill() aren't necessary here, the default width will be fine enough to print what you have. What if the user enters a very large number for min so that min/60 is three digits? setw(2) truncates the output.

- Avoid using system()


*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// This
if (usersChoice == 1);
   DoSomething();
// means
if (usersChoice == 1)
{
   ;
}
DoSomething();
// but what you want is
if (usersChoice == 1)
{
   DoSomething();
}
// or
if (usersChoice == 1)
   DoSomething();
Last edited on
Topic archived. No new replies allowed.