Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. menu

menu

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelpdiscussion
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S sardinka

    Please help Here is my code. I was able to modified to go to main menu. But If user enter a value outside the range it should not go to the main menu. It should display only: cout<<"Please enter 1 or 2 (3 to exit program):"; cin>>choice; #include //using namespace std; int main(void) { int choice; float c; //Declare variable to be displayed. float f; //Question:Will it be the best practice to do this: float c=0? //Question:I used float, but I am not sure if in this case better to use int or double? for(;;) // empty to promt a user to enter correct choice value { cout<<"Please select the task to perform:"<>choice; switch (choice) { // use switch in order to do validation and calculation case 1: { cout<<"Enter value in Fahrenheit:" ; cin>>f; c=((f-32)*5/9); cout<<"The value in Centigrade is:"<>c; f=((c+32)*9/5); cout<<"The value in Fahrenheit is:"<

    R Offline
    R Offline
    RobJones
    wrote on last edited by
    #2

    You could create another function to handle the switching.. I would make that function of type BOOL and if they enter a invalid response then you could return false and prompt the user again with the same question... Rob Whoever said nothing's impossible never tried slamming a revolving door!

    S 1 Reply Last reply
    0
    • R RobJones

      You could create another function to handle the switching.. I would make that function of type BOOL and if they enter a invalid response then you could return false and prompt the user again with the same question... Rob Whoever said nothing's impossible never tried slamming a revolving door!

      S Offline
      S Offline
      sardinka
      wrote on last edited by
      #3

      can you give the example? I am new to C++

      R 1 Reply Last reply
      0
      • S sardinka

        Please help Here is my code. I was able to modified to go to main menu. But If user enter a value outside the range it should not go to the main menu. It should display only: cout<<"Please enter 1 or 2 (3 to exit program):"; cin>>choice; #include //using namespace std; int main(void) { int choice; float c; //Declare variable to be displayed. float f; //Question:Will it be the best practice to do this: float c=0? //Question:I used float, but I am not sure if in this case better to use int or double? for(;;) // empty to promt a user to enter correct choice value { cout<<"Please select the task to perform:"<>choice; switch (choice) { // use switch in order to do validation and calculation case 1: { cout<<"Enter value in Fahrenheit:" ; cin>>f; c=((f-32)*5/9); cout<<"The value in Centigrade is:"<>c; f=((c+32)*9/5); cout<<"The value in Fahrenheit is:"<

        J Offline
        J Offline
        jhwurmbach
        wrote on last edited by
        #4

        Your while loop is definitely testing the wrong thing. Or is there a typo? You probably need to drop the useless for( ;; )-loop and switch to a while-loop testing for (choice == 3) as exit-condition. You obviously need to get the input for choice before this loop. As last statements in this loop (after the switch block), you need to duplicate the input for choice. That way, you will be asking for choice in any subsequent loops. The switch-statement will then have cases only for 1, 2, and default. Be careful what entering a letter into your float variable does! An additional hint:

        do
        // use do-while loop
        {

        seems to be a very silly comentary. Everyone can see this. Try not to comment what you do, but why and why exactly so and not different.


        Who is 'General Failure'? And why is he reading my harddisk?!?

        S 2 Replies Last reply
        0
        • J jhwurmbach

          Your while loop is definitely testing the wrong thing. Or is there a typo? You probably need to drop the useless for( ;; )-loop and switch to a while-loop testing for (choice == 3) as exit-condition. You obviously need to get the input for choice before this loop. As last statements in this loop (after the switch block), you need to duplicate the input for choice. That way, you will be asking for choice in any subsequent loops. The switch-statement will then have cases only for 1, 2, and default. Be careful what entering a letter into your float variable does! An additional hint:

          do
          // use do-while loop
          {

          seems to be a very silly comentary. Everyone can see this. Try not to comment what you do, but why and why exactly so and not different.


          Who is 'General Failure'? And why is he reading my harddisk?!?

          S Offline
          S Offline
          sardinka
          wrote on last edited by
          #5

          Here is my modified code. It still not going to the cout<<"Please select the task to perform:"< //using namespace std; int main(void) { int choice; float c; float f; bool enter; do { if (enter=true ) { cout<<"Please select the task to perform:"<>choice; switch (choice) { case 1: { cout<<"Enter value in Fahrenheit:" ; cin>>f; c=((f-32)*5/9); cout<<"The value in Centigrade is:"<>c; f=((c+32)*9/5); cout<<"The value in Fahrenheit is:"<

          1 Reply Last reply
          0
          • S sardinka

            can you give the example? I am new to C++

            R Offline
            R Offline
            RobJones
            wrote on last edited by
            #6

            I didn't compile this so it may have errors.. At least I think you will get the basic idea...

            BOOL OnPrompt();

            int main(void)
            {
            //Question:Will it be the best practice to do this: float c=0?
            //Question:I used float, but I am not sure if in this case better to use int or double?

            cout<<"Please select the task to perform:"<\>choice;
            switch (choice)
            {
            	// use switch in order to do validation and calculation
            	case 1:
            	{
            		cout<<"Enter value 
            		in Fahrenheit:" ;
            		cin>>f;
            		c=((f-32)\*5/9);
            		cout<<"The value 
            		in Centigrade is:"<\>c;
            		f=((c+32)\*9/5);
            		cout<<"The value in Fahrenheit is:"<
            

            Rob

            Whoever said nothing's impossible never tried slamming a revolving door!

            S 1 Reply Last reply
            0
            • J jhwurmbach

              Your while loop is definitely testing the wrong thing. Or is there a typo? You probably need to drop the useless for( ;; )-loop and switch to a while-loop testing for (choice == 3) as exit-condition. You obviously need to get the input for choice before this loop. As last statements in this loop (after the switch block), you need to duplicate the input for choice. That way, you will be asking for choice in any subsequent loops. The switch-statement will then have cases only for 1, 2, and default. Be careful what entering a letter into your float variable does! An additional hint:

              do
              // use do-while loop
              {

              seems to be a very silly comentary. Everyone can see this. Try not to comment what you do, but why and why exactly so and not different.


              Who is 'General Failure'? And why is he reading my harddisk?!?

              S Offline
              S Offline
              sardinka
              wrote on last edited by
              #7

              Here is my updated code, but it is not doing what it should. If user enters outside the range 1,2 it should show only cout<<"Please enter 1 or 2 (3 to exit program):"; in all other cases main menu #include //using namespace std; int main(void) { int choice; float c; float f; bool enter; enter==true; do { if (enter=true ) { cout<<"Please select the task to perform:"<>choice; switch (choice) { case 1: { cout<<"Enter value in Fahrenheit:" ; cin>>f; c=((f-32)*5/9); cout<<"The value in Centigrade is:"<>c; f=((c+32)*9/5); cout<<"The value in Fahrenheit is:"<

              D 1 Reply Last reply
              0
              • R RobJones

                I didn't compile this so it may have errors.. At least I think you will get the basic idea...

                BOOL OnPrompt();

                int main(void)
                {
                //Question:Will it be the best practice to do this: float c=0?
                //Question:I used float, but I am not sure if in this case better to use int or double?

                cout<<"Please select the task to perform:"<\>choice;
                switch (choice)
                {
                	// use switch in order to do validation and calculation
                	case 1:
                	{
                		cout<<"Enter value 
                		in Fahrenheit:" ;
                		cin>>f;
                		c=((f-32)\*5/9);
                		cout<<"The value 
                		in Centigrade is:"<\>c;
                		f=((c+32)\*9/5);
                		cout<<"The value in Fahrenheit is:"<
                

                Rob

                Whoever said nothing's impossible never tried slamming a revolving door!

                S Offline
                S Offline
                sardinka
                wrote on last edited by
                #8

                I am so sorry. But I could not compile your example. And also I did not understand :( Please explain one more time...

                D 1 Reply Last reply
                0
                • S sardinka

                  Here is my updated code, but it is not doing what it should. If user enters outside the range 1,2 it should show only cout<<"Please enter 1 or 2 (3 to exit program):"; in all other cases main menu #include //using namespace std; int main(void) { int choice; float c; float f; bool enter; enter==true; do { if (enter=true ) { cout<<"Please select the task to perform:"<>choice; switch (choice) { case 1: { cout<<"Enter value in Fahrenheit:" ; cin>>f; c=((f-32)*5/9); cout<<"The value in Centigrade is:"<>c; f=((c+32)*9/5); cout<<"The value in Fahrenheit is:"<

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #9

                  sardinka wrote: if (enter=true ) The compiler could have easily detected this error had it been written like: if (true=enter)... It's always good practice to place constants on the left of the operator and variables on the right of the operator. That way, if you ever decice to use = instead of ==, you'll receive a compiler error.


                  Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                  1 Reply Last reply
                  0
                  • S sardinka

                    I am so sorry. But I could not compile your example. And also I did not understand :( Please explain one more time...

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #10

                    For brevity, Rob left out some of the basics. It was assumed you knew how to add the missing pieces. Work through the compiler/linker errors one by one until you encounter one that you just cannot figure out.


                    Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                    A J 2 Replies Last reply
                    0
                    • D David Crow

                      For brevity, Rob left out some of the basics. It was assumed you knew how to add the missing pieces. Work through the compiler/linker errors one by one until you encounter one that you just cannot figure out.


                      Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                      A Offline
                      A Offline
                      Atlantys
                      wrote on last edited by
                      #11

                      DavidCrow wrote: Five birds are sitting on a fence. Three of them decide to fly off. How many are left? I would love to guess "two", but since that's just *way* too easy, I'm gonna go with "a number". :-D The kindest thing you can do for a stupid person, and for the gene pool, is to let him expire of his own dumb choices. [Roger Wright on stupid people]

                      1 Reply Last reply
                      0
                      • D David Crow

                        For brevity, Rob left out some of the basics. It was assumed you knew how to add the missing pieces. Work through the compiler/linker errors one by one until you encounter one that you just cannot figure out.


                        Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                        J Offline
                        J Offline
                        jhwurmbach
                        wrote on last edited by
                        #12

                        DavidCrow wrote: Five birds are sitting on a fence. Three of them decide to fly off. How many are left? None - if one bird takes off, so does the whole flock.


                        Who is 'General Failure'? And why is he reading my harddisk?!?

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups