Need some advice with my c++ assignment

Cbick
Cbick used Ask the Experts™
on
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. Herss 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;
	//declare variables
    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;
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Because it is an assignment I can't give much concrete help but:

* you don't seem to understand what an enum is and how it works
* you don't seem to understand the syntax of if
* the required structure might store hours, minutes and seconds
Top Expert 2009

Commented:
I see you have conversion factors already (60).  So convert those to constants using either #define or 'const int', thats pretty easy.

A decision statement could be a simple if(). I see you have those already.

Any ideas for a struct? Maybe you can include the overall time in a struct with members of hours, minutes and seconds.
Top Expert 2009

Commented:
>>if (usersChoice == 1);

In C/C++ a semicolon is considered an empty statement. So your line above does nothing. it is the same as...

if(...)
   ;      // statement

The only different is the ; is on the 2nd line.

You need to say:

if this then
  do that


If you want the line of code after the if to be part of the if statement you need to either remove that semicolon, or use brackets.

if (usersChoice == 1) {
    // statement 1
    // statement 2
}


Notice no semicolons after the if parentheses.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Author

Commented:
Yes I can get the first part of my code to work but I cannot figure out how to get the seconds to work. Everytime I try to add else to this it comes up with an error when I go to compile.
This is only my second week of learning C++. I don't want some one to do this assignment for me  I just need some advice.  I will try what was posted in here to see if I can get this to compile.
Top Expert 2009

Commented:
>>Everytime I try to add else to this it comes up with an error when

Probably because you've written the if / else syntax wrong, as I pointed out above.

I encourage you to use brackets for now until you are more comfortable with where single semicolon's go.

if() {

}
else {

}

Author

Commented:
I tried this but Now I am getting these errors
Error      1      error C2181: illegal else without matching if      35
Error      2      error C2144: syntax error : 'int' should be preceded by ';'      36

here is my code

      

#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;
	//declare variables
    int min;
    int hh , mm;
    cout << "Enter minutes: ";
    cin  >> min;
	if (usersChoice == 1){
    hh = min / 60;
	mm = min % 60;
	}cout << setw(2) << setfill('0') << hh << ":"
          << setw(2) << setfill('0') << mm << '\n'
    
	else{ (usersChoice == 2)
         int sec;
	sec = min % 60;
	}cout << "The seconds are:  " << secs << endl; 
 
  return 0;
}

Open in new window

Well it is exactly what it says. The else is following a cout not an if.
Top Expert 2009

Commented:
Ok you've got a jumble of code and it appears you need to work on your formatting for readability. I have reformatted your code but have not changed it at all. So tell me what is wrong with the picture below, considering how you know if/else blocks should be (refer to my note above if need be)

if (usersChoice == 1){
   hh = min / 60;
   mm = min % 60;
}
cout << setw(2) << setfill('0') << hh << ":"                           //  <== why is this statement here? It must be inside a block.
        << setw(2) << setfill('0') << mm << '\n'
   
else{ (usersChoice == 2)                                                       //  <==  else { (...)   is not how else works. else is simply else.
   int sec;                                                                                //           You may want to use   else if(...) instead
   sec = min % 60;
}
cout << "The seconds are:  " << secs << endl;



Author

Commented:
I think that I must have the ending brace in the wrong place for the first code block. I tried else if but still gives me the same error illegal else without matching if  so I don't understand what I am doing wrong there.
Top Expert 2009

Commented:
You cannot just put arbitrary statements in the middle of

if() {
       // statements can go here
}
else {
       // statements can go here
}

No where else. You put a cout statement in between the closing } of the if() block and the else keyword. Nothing can go there. And if using "else if" it goes like so:


if() {
       // statements can go here

}
else if() {
       // statements can go here

}
else if() {
       // statements can go here

}
else {
       // statements can go here

}

Author

Commented:
Ok now I have changed a couple of things in my code and it will compile now but I guess I have some syntax or logical error because the program doesn't do what I want it to And I know that I haven't written the exit code block yet. Just trying to compile it a little at a time

#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;
	//declare variables
    int min;
    int hh , mm;
    cout << "Enter minutes: ";
    cin  >> min;
	if (usersChoice == 1){
    hh = min / 60;
	mm = min % 60;
    cout << setw(2) << setfill('0') << hh << ":"
		<< setw(2) << setfill('0') << mm << '\n';}
	
    
	
	 else if (usersChoice == 2){
	int sec;
	sec = min % 60;}
	;cout << "The seconds are:  " << secs << endl; 
 
  return 0;
}

Open in new window

Top Expert 2009
Commented:
Please read my statement CLOSELY and in detail.

Did you read my previous suggestion about code formatting. Are you familiar with indentation? Indent all lines of code that are at the same scope level. Every open block { means a new nested scope level, so more indentation until the }

while() {
   if() {
      // statement
      // statement
      // statement
   }
   else {
      // statement
      // statement
   }
}


Please fix your formatting before trying to go further.

Don't put a } at the end of a line like where you have:

<< setw(2) << setfill('0') << mm << '\n';}

Put the } on an empty line to line up indentation-wise with the if() that started the statement, that way you know which statement the } goes with.

Do you prefer this?

if() {
   statement1;
   statement2; }

or this?

if() {
   statement1;
   statement2;
}


I prefer the latter.

Author

Commented:
Ok Now I'm getting it. Its finally starting to compile correctly. My math was wrong for the seconds I was trying to use modulus division. Here is my reformatted code
#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;
 
	//declare variables
    int min;
    int hh , mm;
    cout << "Enter minutes: ";
    cin  >> min;
	if(usersChoice == 1){
            hh = min / 60;
	   mm = min % 60;
            cout << setw(2) << setfill('0') << hh << ":"
	        << setw(2) << setfill('0') << mm << '\n';
	   }
	
    
	
	 else if(usersChoice == 2){
	   int secs;
	   secs = min * 60;
	   cout << "The seconds are:  " << secs << endl;
            }
 
	
  return 0;
}

Open in new window

Author

Commented:
It never paste correctly in the code snipit box

Author

Commented:
Now all I need to do is figure out how to get the program to stop asking for the minutes when 3 is selected from the menu.
Top Expert 2009

Commented:
>>It never paste correctly in the code snipit box

Due to use of tabs instead of spaces.

Author

Commented:
Ok I think I got it now Thanks everyone for all you help especially you mrjoltcola. Now I understand this much better than before. I just need to remember to have proper indenting and put the braces in the right places. Here is my revised source code, could you tell me what you think. Everything compiles and runs correctly.
#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;
 
	//declare variables
         int min;
         int hh , mm;
	//prompts user to enter minutes
	if(usersChoice > 3){
	   cout << "You have made an invalid selection\n";
	   }
	else if(usersChoice == 3){
	   cout << "You have chosen to exit the program\n";
	   }
	else if(usersChoice == 1 || 2){
            cout << "Enter minutes: ";
            cin  >> min;
	   }
	//end ifs
	//performs calculations
	if(usersChoice == 1){
       hh = min / 60;
	   mm = min % 60;
	   cout << setw(2) << setfill('0') << "The hours and minutes are:  " << hh << ":"
		    << setw(2) << setfill('0') << mm << '\n';
	}
	
    
	
	 else if(usersChoice == 2){
	   int secs;
	   secs = min * 60;
	   cout << "The seconds are:  " << secs << endl;
	   }
 
	  //end ifs
 
	return 0; //end of main function
}

Open in new window

Author

Commented:
One other concern, How can you lock this thread so that anyone else trying to do this assignment will not have access to my source code. I don't want anyone to be able to cheat.

Author

Commented:
Thanks for all you help. Sorry for being such a newb but I guess we all have to start somewhere. I have learned a lot from you today
Top Expert 2009

Commented:
Not much option to do that. Best thing is not to share your username and also not to post identifying information in your question so it won't come up on a Google / EE search.

I am glad I helped! Good luck. Feel free to post more questions.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial