jimblumberg's Profile User Rating: *****

Reputation: 4278 Guru
Group:
Mentors
Active Posts:
14,219 (7.81 per day)
Joined:
25-December 09
Profile Views:
70,481
Last Active:
User is offline Today, 08:28 AM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Linux
Favorite Browser:
FireFox
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
50
Expert In:
C/C++

Latest Visitors

Icon   jimblumberg has not set their status

Posts I've Made

  1. In Topic: If and Else If Error

    Posted 19 Dec 2014

    Please describe what you think is happening in this snippet:

        string Nuclear = Nuclear;
    

    What do you think the value of this variable will be?

    And this one:
     if(input != Nuclear)
    

    When will input ever equal Nuclear? Remember Nuclear is a variable not a value.

    And this one:

        else if(input > Nuclear)
    

    When do you think the value held in the variable input will ever be greater than the value held in the variable Nuclear?


    Perhaps you need to insert some print statements so you can see what each variable contains in the various parts of your program.


    Jim
  2. In Topic: Adding exception handling code to my calculate mortgage program.

    Posted 19 Dec 2014

    Just cutting and pasting code without understanding what it is actually doing is not a very good way of solving your problems. The code samples posted are not meant to be used blindly, you should study the code and if you don't understand some part of the provided code ask questions.

    How many times do you want the user to enter a value for myMortgage.Principal?

    Your latest code is trying to retrieve this value at least twice, why?

    You have a return in the code you blindly cut and pasted, do you understand what happens when the program encounters a return statement? Do you understand that there was a reason I put that code inside a function instead of just repeating the code everywhere you need to retrieve a double from the user?

    Jim
  3. In Topic: Adding exception handling code to my calculate mortgage program.

    Posted 19 Dec 2014

    Proper input validation is one of the trickiest parts of programming. To do what you're trying to do it will probably be easier to avoid stream failures. To do this I recommend retrieving your user input into a std::string because there will be fewer stream failures. Something like:

    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    struct calcMortgage
    
    {
       double Principal;
       double numYears;
       double IntRate;
       double monthlyPayments;
    };
    
    double getDoubleInput();
    
    int main()
    {
       calcMortgage myMortgage;
    
       cout << " Welcome to ABC Bank! " <<endl <<endl;
    
       cout << " How much are you wanting to finance?  " <<endl;
    
       myMortgage.Principal = getDoubleInput();
    
       cout << "Principal: " << myMortgage.Principal << endl;
    
       return 0;
    }
    
    
    double getDoubleInput()
    {
       double retValue = 0.0;
       std::string input;
    
       try
       {
          cin >> input; // Remember a white space character will terminate this input.
          if(input.find_first_not_of("0123456789.") != std::string::npos)
             throw(input);
       }
       catch(std::string)
       {
          size_t pos;
          while((pos = input.find_first_not_of("0123456789.")) != std::string::npos)
             input.erase(pos, 1);
       }
    
       // Now convert the string into the number.
       std::istringstream ins(input);
       ins >> retValue;
    
       return retValue;
    }
    
    


    And even though exceptions seem to be part of this learning experience, remember this can be handled, IMO better, without exceptions.
    double getDoubleInput()
    {
       double retValue = 0.0;
       std::string input;
    
       cin >> input; // Remember a white space character will terminate this input.
       if(input.find_first_not_of("0123456789.") != std::string::npos)
       {
          size_t pos;
          while((pos = input.find_first_not_of("0123456789.")) != std::string::npos)
             input.erase(pos, 1);
       }
    
       // Now convert the string into the number.
       std::istringstream ins(input);
       ins >> retValue;
    
       return retValue;
    }
    
    


    Lastly if you allow your stream to fail you must clear the error condition and discard all the "bad" characters from the input buffer.
    int number;
    cin >> number;
    
    if(!cin)
    {
       cin.clear(); // Clear the error flags.
       cin.ignore(1024, '\n'); // Clear the input buffer.
    }
    



    Jim
  4. In Topic: Adding exception handling code to my calculate mortgage program.

    Posted 18 Dec 2014

    First trying to use exceptions to handle invalid entries is not a good idea.

    Now to the problem, you need to specify a valid exception in your catch() clause. Try something like:

    #include <iostream>
    #include <stdexcept>
    
    using namespace std;
    
    struct calcMortgage
    
    {
       double Principal;
       double numYears;
       double IntRate;
       double monthlyPayments;
    };
    
    int main()
    {
       std::cin.exceptions(std::ios_base::failbit);
       calcMortgage myMortgage;
    
       cout <<" Welcome to ABC Bank! " <<endl <<endl;
    
       cout <<" How much are you wanting to finance?  " <<endl;
    
    
       try
       {
          cin >> myMortgage.Principal;
       }
       catch(const std::ios_base::failure &failure) 
       {
          std::cerr << "Error, do something!\n";
          return(1);
       }
    
       cout <<" Do you want a 15 or 30 year loan? " <<endl;
    
    
       return 0;
    
    }
    
    
    
    


    Jim
  5. In Topic: How do I avoid using the goto function?

    Posted 17 Dec 2014

    Having a program with hundreds of variables is not uncommon, needing to pass all these variables into all of your functions is extremely rare. To limit the number of variables that must be passed into functions there are several methods available. The first method is eliminate the use of global variables and to make your functions small and thereby only requiring a few parameters. Next is to use classes or structures to group related variables, thereby requiring only passing one parameter in many cases, the class/structure instance.

    As far as the goto goes you should learn to use the other loop constructs like the for() loop, while() loop, and the do{}while() loop.

    Jim

My Information

Member Title:
Age:
Age Unknown
Birthday:
December 6
Gender:

Contact Information

E-mail:
Private

Comments

Page 1 of 1
  1. Photo

    modi123_1 Icon

    17 Nov 2014 - 08:11
    I like brocken's comment "I don't know much about coding, but it would be really easy".
    Heh.. I don't know anything about the topic, but I know it is easy.
    Riiiiiiiiiight.
  2. Photo

    brockenglass101 Icon

    22 Oct 2014 - 02:34
    I know that this isn't what this site is for but many people in the pc gaming comunity have been looking for screen magnification program that would zoom in on the center of the screan rather then the cursor (turned on and off with a hotkey). Now, I am not going to lie, I don't know much about coding, but it would be really easy to make program like this one.
    I hope you will find ...
  3. Photo

    KnifeTea Icon

    11 May 2014 - 05:55
    thanks for the help Jim!
  4. Photo

    tarmizi_adam2005 Icon

    04 Dec 2013 - 07:15
    Just stopping by to say Hello Jim. I've learned quite a few things from your posts in the c/c++ forum.
Page 1 of 1