Link to home
Start Free TrialLog in
Avatar of Cbick
Cbick

asked on

Need some advice with my c++ assignment

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

Avatar of Let_Me_Be
Let_Me_Be
Flag of United States of America image

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
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.
>>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.
Avatar of Cbick
Cbick

ASKER

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.
>>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 {

}
Avatar of Cbick

ASKER

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.
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;



Avatar of Cbick

ASKER

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.
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

}
Avatar of Cbick

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cbick

ASKER

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

Avatar of Cbick

ASKER

It never paste correctly in the code snipit box
Avatar of Cbick

ASKER

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.
>>It never paste correctly in the code snipit box

Due to use of tabs instead of spaces.
Avatar of Cbick

ASKER

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

Avatar of Cbick

ASKER

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.
Avatar of Cbick

ASKER

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
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.