Help these programs not working!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mgrubbs
    New Member
    • Dec 2008
    • 3

    #1

    Help these programs not working!!!

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {	
     int code = 0;
     int charges = 0;
    
     //enter input data
     cout << "State code: ";
     cin >> code;
     cout << "Shipping charges: ";
     cin >> charges;
    
     switch (code)
     {
     case 1:
    	     charges = 25;
    		 break;
     case 2:
     case 5:
     case 6:
    	     charges = 30;
    		 break;
     case 3:
     case 4:
    	    charges = 40;
    	    break;
     default:
    	     cout << "Incorrect state code" << endl;
     }    //end switch
    
     //display charges
     cout << "Charges: " << charges << endl;
    
        return 0;
    }   //end of main function
    
    It shows no errors on build but when go to enter number it does not display the charges? Have worked and worked on it cannot figure it out would appreciate some help in this matter as quick as possible. I am a newbie and I am really struggling!! same thing with the Net one below!!
    
    #include <iostream> 
    #include <string>
    #include <iomanip>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::fixed;
    using std::setprecision;
    
    int main()
    {
    	double hours = 0.0;
    	double rate  = 0.0;
    	double gross = 0.0;
    	
    	//enter input data
    	cout << "Hours worked: ";
    	cin >> hours;
    	cout << "Pay rate: ";
    	cin >> rate;
    		
    	//validate hours
    	if (hours <= 0.0)
    		cout << "The hours worked must be greater than 0." << endl;
    	//validate rate
    	else if (rate <= 0.0)
    		cout << "The pay rate must be greater than 0." << endl;
    	//calculate gross pay
    	else if (hours <= 40.0)
    		gross = hours * rate;
    	else gross = 40.0 * rate + (hours - 40.0) * rate * 1.5;
    	//end ifs
    
    	//display gross pay
    	cout << fixed << setprecision(2);
    	cout << "The gross pay for working " << hours << " hours at $"
    		<< rate << " per hour is $" << gross << endl;
    
    	return 0;
    }   //end of main function
    Again would really appreciate all the help! Have a wonderful Holiday Season.
    Last edited by Markus; Dec 10 '08, 09:07 PM. Reason: Added [code] tags.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Some things I should point out
    1. You didn't use [code] tags when posting code,
    2. You've flat out asked for help with no information on the problem,
    3. You didn't provide a meaningful title and
    4. You've posted the question in the wrong forum.


    Now, I see you're a newbie, so we'll let you off for now.

    Please read the Posting Guidelines to familiarize yourself with our way of life.

    Thanks,
    Markus.

    PS - I wrote a whole load here, explaining what he should do now and in future questions, but, for some reason, the website refused to respond, and I lost my post. Awesome!
    PSS - I'll wait for someone else to move this, because I don't know the language it is in. I'm guessing Java?

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Nope, that's C++. Moved to C/C++ Forum.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by insertAlias
        Nope, that's C++. Moved to C/C++ Forum.
        Thanks, iA. :D

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          OK, here's the thing. There's nothing technically wrong with either of these programs. They both compile and run just fine.

          However, with a console app, usually the console closes when the app is done running.

          It is quite possible that the correct output is being sent but the console is closing too fast for you to see it.

          Try adding this line:
          Code:
          system("pause");
          right before the return 0; calls.

          Also, I really can't follow the logic in the first program. You ask the user to input the code and the charges, but then you set the charges based on the code. Why ask for input if you are just going to override it?

          Comment

          • YarrOfDoom
            Recognized Expert Top Contributor
            • Aug 2007
            • 1243

            #6
            Originally posted by insertAlias
            Try adding this line:
            Code:
            system("pause");
            right before the return 0; calls.
            I've read in another thread that this isn't always the best way to go.


            Another way is to just launch the command line, and then launch the program from there.

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              Well, I'm by absolutely no means a C++ expert. It's just a quick way I thought of to test his problem, if he's using windows.

              Comment

              • mgrubbs
                New Member
                • Dec 2008
                • 3

                #8
                thanks let me explain

                What it amounts to is when I enter the code it is suppose to pop up the shipping charge when I go into to start without debugging it is suppose to pop up enter code then I put 1 or whatever code for a shipping charge then it is suppose to pop up that number. That is the problem it is going to the enter the code and when I do it does not come up with the shipping charge. Maybe that will help some. I am a newbie and really confused to say the least. Appreciate all your help

                Comment

                • Curtis Rutland
                  Recognized Expert Specialist
                  • Apr 2008
                  • 3264

                  #9
                  OK, honestly I don't understand your last post.

                  Did you try what I said?

                  Look the flow of your first program right now is:
                  Prompt
                  input
                  Prompt
                  input
                  switch logic
                  Display
                  Exit

                  And if you don't inject some kind of stop or pause between the Display and Exit, you'll never actually see what was displayed.

                  I copy/pasted your code, and added the system("pause") ; line, and it worked for me.

                  Your logic, on the other hand, I can't help you with until you more clearly explain the flow of your program.

                  Comment

                  • mgrubbs
                    New Member
                    • Dec 2008
                    • 3

                    #10
                    tried your suggestion

                    Originally posted by insertAlias
                    Nope, that's C++. Moved to C/C++ Forum.
                    tried your suggestion but did not work will look at the othe option you stated.Thanks again for your help, Mark

                    Comment

                    Working...