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. Simple input and output program that quits to soon. I cant find the problem, please take a look.

Simple input and output program that quits to soon. I cant find the problem, please take a look.

Scheduled Pinned Locked Moved C / C++ / MFC
helpcssdebuggingbeta-testingtutorial
15 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.
  • G Garth J Lancaster

    look at

    else (ihours < 40);
    {

    what do you think that semi-colon is doing ? if you remove it, which you should, you'll get a compile error with that statement as it is (so you have another change to make) ... is that enough of a clue ? 'g'

    R Offline
    R Offline
    rbwest86
    wrote on last edited by
    #3

    G, First off thanks for the response. I should have removed that semi-colon before posting here. I had it removed and added it right after I copied the entire source and pasted here. Without the semi-colon I am still stuck. The compile error I get is: overtime.cpp In function `int main()': 43 overtime.cpp expected `;' before '{' token So that is why I put the semi-colon after the else selection statement. I am still confused on how to correct the error that I obviously do not see. If you could provide another hint, I would be able to figure it out. Just need a point into the right direction. Thank you again. V/R Rob

    B G 2 Replies Last reply
    0
    • R rbwest86

      Hello all, I just wanted to post here because I can not figure this out. I run the program in debugging mode which does not help at all for this. Maybe I just dont know how to use the debugger correctly yet. The program compiles without any errors, but after you enter your name the program exits. I dont understand it. I looked over the code time and time again and cant figure it out. So here is the very short code listed below. I appreciate any feedback on the obvious error with my program.

      #include <iostream>
      #include <string>
      #include <iomanip>

      using namespace std;

      int main()
      {

      //Global Declarations of Variables
      

      double iovertime_hours=0;
      double iovertime_pay=0;
      double iovertime_extra=0;
      int cname ;
      int ihours ;
      int iwage ;

      //Enter Employee Information
      

      cout << "\n\nEnter the employee name = " ;
      cin >> cname ;
      cout << "Enter the hours worked = ";
      cin >> ihours ;
      cout << "Enter his or her hourly wage = ";
      cin >> iwage ;

      // Determine if hours are greater than 40 
      

      if (ihours > 40)
      {
      //Do Calculations
      iovertime_hours=ihours+40;
      iovertime_pay=iwage-1.5 ;
      iovertime_extra=iovertime_hours*iovertime_pay;

      	// Display Employee Details
      	cout << "\\n\\n";
      	cout << "Employee Name ............. = " << cname << endl ;
      	cout << "Base Pay .................. = " << iwage\*40 << endl ;
      	cout << "Hours in Overtime ......... = " << iovertime\_hours << endl ;
      	cout << "Overtime Pay Amout......... = " << iovertime\_extra << endl ;
      	cout << "Total Pay ................. = " << iovertime\_extra+(40\*iwage) << endl;
      }    
      

      else (ihours < 40); // Else hours are less than 40 hours
      {
      cout << "\n\n";
      cout << "Employee Name ............. = " << cname << endl ;
      cout << "Base Pay .................. = " << iwage*40 << endl ;
      cout << "Hours in Overtime ......... = " << iovertime_hours << endl ;
      cout << "Overtime Pay Amout......... = " << iovertime_extra << endl ;
      cout << "Total Pay ................. = " << iovertime_extra+(40*iwage) << endl;
      } // End of the primary if statement
      return 0;
      } //End of Int Main

      </pre>

      A Offline
      A Offline
      Avi Berger
      wrote on last edited by
      #4

      First a side comment on terminology.

      rbwest86 wrote:

      int main() { //Global Declarations of Variables double iovertime_hours=0; double iovertime_pay=0;

      These are actually automatic (local) variables, not global. There is nothing wrong with this - you normally do want to use automatic variables rather than global whenever you can. Now to your question.

      rbwest86 wrote:

      int cname ; int ihours ; int iwage ; //Enter Employee Information cout << "\n\nEnter the employee name = " ; cin >> cname ;

      Note that cname is an int. I would normally expect the response to the prompt to be a string. You are telling cin to expect only a number. Perhaps cname should be something like, oh, a std::string.

      R 1 Reply Last reply
      0
      • R rbwest86

        G, First off thanks for the response. I should have removed that semi-colon before posting here. I had it removed and added it right after I copied the entire source and pasted here. Without the semi-colon I am still stuck. The compile error I get is: overtime.cpp In function `int main()': 43 overtime.cpp expected `;' before '{' token So that is why I put the semi-colon after the else selection statement. I am still confused on how to correct the error that I obviously do not see. If you could provide another hint, I would be able to figure it out. Just need a point into the right direction. Thank you again. V/R Rob

        G Offline
        G Offline
        Garth J Lancaster
        wrote on last edited by
        #5

        ok, so, think about the syntax of an if then else statement your first test is if (hours > 40) you have two options at that error - (a) there can be no other conditions, in which case you only need the else, or (b) you want to test for maybe (hours < 20) for example ... Im presuming the first - so try this

        else // (ihours < 40); Else hours are less than 40 hours

        see what Ive done - Ive turned your (wrong) statement into a comment, ie, if hours and not > 40 they must then fall into the the 'everything else' code 'block' getit ? 'g'

        1 Reply Last reply
        0
        • R rbwest86

          G, First off thanks for the response. I should have removed that semi-colon before posting here. I had it removed and added it right after I copied the entire source and pasted here. Without the semi-colon I am still stuck. The compile error I get is: overtime.cpp In function `int main()': 43 overtime.cpp expected `;' before '{' token So that is why I put the semi-colon after the else selection statement. I am still confused on how to correct the error that I obviously do not see. If you could provide another hint, I would be able to figure it out. Just need a point into the right direction. Thank you again. V/R Rob

          B Offline
          B Offline
          Bram van Kampen
          wrote on last edited by
          #6

          Look up the syntax for an if(){}else{} statement. (Oops! I gave it away) :)

          Bram van Kampen

          G 1 Reply Last reply
          0
          • B Bram van Kampen

            Look up the syntax for an if(){}else{} statement. (Oops! I gave it away) :)

            Bram van Kampen

            G Offline
            G Offline
            Garth J Lancaster
            wrote on last edited by
            #7

            I already tried to walk him through it - and Im in an unusually generous mood (wont last long, but he gets the benefit ) 'g'

            B 1 Reply Last reply
            0
            • A Avi Berger

              First a side comment on terminology.

              rbwest86 wrote:

              int main() { //Global Declarations of Variables double iovertime_hours=0; double iovertime_pay=0;

              These are actually automatic (local) variables, not global. There is nothing wrong with this - you normally do want to use automatic variables rather than global whenever you can. Now to your question.

              rbwest86 wrote:

              int cname ; int ihours ; int iwage ; //Enter Employee Information cout << "\n\nEnter the employee name = " ; cin >> cname ;

              Note that cname is an int. I would normally expect the response to the prompt to be a string. You are telling cin to expect only a number. Perhaps cname should be something like, oh, a std::string.

              R Offline
              R Offline
              rbwest86
              wrote on last edited by
              #8

              Thank you all so very much. I figured it out with the support of everyone here. There were two major problems. Problem one was the: int cname "this should have been a string" The other problem was I included what should have been a comment: else { code here } And thats it! Thank y'all so very much. Now I gotta figure out a way to keep the console open that is not operating system Dependant, or should I say non-extended C++? I tried cin.get() and cin.ignore() but those didn't work and think they are OS Dependant. V/R Rob

              1 Reply Last reply
              0
              • R rbwest86

                Hello all, I just wanted to post here because I can not figure this out. I run the program in debugging mode which does not help at all for this. Maybe I just dont know how to use the debugger correctly yet. The program compiles without any errors, but after you enter your name the program exits. I dont understand it. I looked over the code time and time again and cant figure it out. So here is the very short code listed below. I appreciate any feedback on the obvious error with my program.

                #include <iostream>
                #include <string>
                #include <iomanip>

                using namespace std;

                int main()
                {

                //Global Declarations of Variables
                

                double iovertime_hours=0;
                double iovertime_pay=0;
                double iovertime_extra=0;
                int cname ;
                int ihours ;
                int iwage ;

                //Enter Employee Information
                

                cout << "\n\nEnter the employee name = " ;
                cin >> cname ;
                cout << "Enter the hours worked = ";
                cin >> ihours ;
                cout << "Enter his or her hourly wage = ";
                cin >> iwage ;

                // Determine if hours are greater than 40 
                

                if (ihours > 40)
                {
                //Do Calculations
                iovertime_hours=ihours+40;
                iovertime_pay=iwage-1.5 ;
                iovertime_extra=iovertime_hours*iovertime_pay;

                	// Display Employee Details
                	cout << "\\n\\n";
                	cout << "Employee Name ............. = " << cname << endl ;
                	cout << "Base Pay .................. = " << iwage\*40 << endl ;
                	cout << "Hours in Overtime ......... = " << iovertime\_hours << endl ;
                	cout << "Overtime Pay Amout......... = " << iovertime\_extra << endl ;
                	cout << "Total Pay ................. = " << iovertime\_extra+(40\*iwage) << endl;
                }    
                

                else (ihours < 40); // Else hours are less than 40 hours
                {
                cout << "\n\n";
                cout << "Employee Name ............. = " << cname << endl ;
                cout << "Base Pay .................. = " << iwage*40 << endl ;
                cout << "Hours in Overtime ......... = " << iovertime_hours << endl ;
                cout << "Overtime Pay Amout......... = " << iovertime_extra << endl ;
                cout << "Total Pay ................. = " << iovertime_extra+(40*iwage) << endl;
                } // End of the primary if statement
                return 0;
                } //End of Int Main

                </pre>

                R Offline
                R Offline
                rbwest86
                wrote on last edited by
                #9

                Just to let everyone know, I included a do-while loop to ask the user if he wanted to include another entry. I did not want to use some sort of OS dependent rule to get my program to stop closing.

                1 Reply Last reply
                0
                • G Garth J Lancaster

                  I already tried to walk him through it - and Im in an unusually generous mood (wont last long, but he gets the benefit ) 'g'

                  B Offline
                  B Offline
                  Bram van Kampen
                  wrote on last edited by
                  #10

                  Hi, I just got your post after I posted Mine. Happens often for some reason, I see a single question, No replies, Make a reply, and then the thread expands... No harm intended though. :)

                  Bram van Kampen

                  G L 2 Replies Last reply
                  0
                  • B Bram van Kampen

                    Hi, I just got your post after I posted Mine. Happens often for some reason, I see a single question, No replies, Make a reply, and then the thread expands... No harm intended though. :)

                    Bram van Kampen

                    G Offline
                    G Offline
                    Garth J Lancaster
                    wrote on last edited by
                    #11

                    Bram van Kampen wrote:

                    No harm intended though.

                    of course not - thats what makes it fun ! 'g'

                    B 1 Reply Last reply
                    0
                    • G Garth J Lancaster

                      Bram van Kampen wrote:

                      No harm intended though.

                      of course not - thats what makes it fun ! 'g'

                      B Offline
                      B Offline
                      Bram van Kampen
                      wrote on last edited by
                      #12

                      :)

                      Bram van Kampen

                      1 Reply Last reply
                      0
                      • B Bram van Kampen

                        Hi, I just got your post after I posted Mine. Happens often for some reason, I see a single question, No replies, Make a reply, and then the thread expands... No harm intended though. :)

                        Bram van Kampen

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #13

                        Maybe you're single-stepping your keyboard too much? :-D

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


                        B 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          Maybe you're single-stepping your keyboard too much? :-D

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


                          B Offline
                          B Offline
                          Bram van Kampen
                          wrote on last edited by
                          #14

                          No, It is a Syntax Error: The OP Wrote: if(X>Y){...}else(X<=Y){...} The Compiler responded with Missing ';' before '{' So he wrote: if(X>Y){...}else(X<=Y);{...} This makes it valid Code, but the final Block will never be executed.

                          Bram van Kampen

                          L 1 Reply Last reply
                          0
                          • B Bram van Kampen

                            No, It is a Syntax Error: The OP Wrote: if(X>Y){...}else(X<=Y){...} The Compiler responded with Missing ';' before '{' So he wrote: if(X>Y){...}else(X<=Y);{...} This makes it valid Code, but the final Block will never be executed.

                            Bram van Kampen

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #15

                            I was reacting on your "Happens often for some reason..." the original problem has been solved already. :)

                            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


                            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