Thread: modules help

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    3

    modules help

    I need help getting a remainder for when I'm converting inches to feet. i need the remainder to be in inches? Heres what i have
    Code:
    #include <iostream>
    #include <iomanip>
    
    
    usingnamespacestd;
    
    
    int main() {
        double centimeters, meters;
        int conversion, inches, feet, yards, remainInch;
    
    
    cout << "Display the entered lenght in:" <<endl;
        cout << "1. feet" << endl;
        cout << "2. yards" << endl;
    cout << "3. centimeters" << endl;
        cout << "4. meteres" << endl << endl;
    cout << "Enter your choice: " <<endl;
        cin >> conversion;
    
    cout << "Enter your lenght in inches :" << endl;
        cin >> inches;
    
    
    
        if  (conversion == 1){
            feet = inches / 12;
            remainInch =(feet) % 12;
            cout << "Your length is " << feet << " feet "<< "and " << remainInch << " inches"<< endl;
    
        }
    
        else if (conversion == 2){
            yards = inches/ 36;
            cout << "Your length in yards is " << yards << endl;
        }
    
        else if (conversion == 3) {
            centimeters = inches/ .39370;
            cout << "Your length in centimeters is " << centimeters << endl;
        }
    
        else if (conversion == 4) {
            meters = inches / 39.370;
            cout << "Your length in meters is " << meters << endl;
        }
        else if (conversion < 1 || conversion > 4) {
    cout << " This value is not an option." << endl;
        }
    
    
    
    
    
         return 0;
    }
    



  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is good that you posted in code bbcode tags, but it looks like your copy and pasting had some problems and/or your code formatting needs work. For example:
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main() {
        double centimeters, meters;
        int conversion, inches, feet, yards, remainInch;
    
        cout << "Display the entered lenght in:" <<endl;
        cout << "1. feet" << endl;
        cout << "2. yards" << endl;
        cout << "3. centimeters" << endl;
        cout << "4. meteres" << endl << endl;
        cout << "Enter your choice: " <<endl;
        cin >> conversion;
    
        cout << "Enter your lenght in inches :" << endl;
        cin >> inches;
    
        if (conversion == 1) {
            feet = inches / 12;
            remainInch = (feet) % 12;
            cout << "Your length is " << feet << " feet "<< "and " << remainInch << " inches" << endl;
        }
        else if (conversion == 2) {
            yards = inches/ 36;
            cout << "Your length in yards is " << yards << endl;
        }
        else if (conversion == 3) {
            centimeters = inches/ .39370;
            cout << "Your length in centimeters is " << centimeters << endl;
        }
        else if (conversion == 4) {
            meters = inches / 39.370;
            cout << "Your length in meters is " << meters << endl;
        }
        else if (conversion < 1 || conversion > 4) {
            cout << " This value is not an option." << endl;
        }
    
        return 0;
    }
    Anyway, the problem is that you have a mistake here:
    Code:
    remainInch = (feet) % 12;
    You should have written:
    Code:
    remainInch = inches % 12;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    3
    Can i ask for an example of how my code formatting needs work. I am very new to this and i thought it looked okey ?

  4. #4
    Registered User
    Join Date
    Feb 2016
    Posts
    3
    oh bye the way thank you for the help always appreciated !!!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Krista Koeplin
    Can i ask for an example of how my code formatting needs work. I am very new to this and i thought it looked okey ?
    I posted such an example in my previous post. Basically, within the body of a function, an if statement, a loop, etc, consistently indent by one level. You made some effort, which is good, but it was inconsistent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I need help getting a remainder for when I'm converting inches to feet. i need the remainder to be in inches?
    You know this: there are 12 inches per foot or 12 in/ft. Let this value equal x.

    We want a formula like this : ( 1 ft / 12 in ) * X = feet.

    Now, if you want the remainder to be in inches, you simply use std::div just like this :
    Code:
    // Example program
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    
    int main()
    {
        const int inches = 71;
        const int inches_per_foot = 12;
        std::div_t feet = std::div(inches, inches_per_foot);
        std::cout << feet.quot << "\' " << feet.rem << "\"" << std::endl;
    }
    Output:
    Code:
    5' 11"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modules
    By qwes3r in forum C Programming
    Replies: 9
    Last Post: 03-27-2015, 09:51 PM
  2. Help with modules
    By Jacob Boyd in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2012, 10:08 AM
  3. Modules
    By Hakim in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2006, 04:53 PM
  4. Modules
    By tim545666 in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2002, 11:44 PM
  5. Modules
    By Garfield in forum C Programming
    Replies: 4
    Last Post: 09-30-2001, 10:23 AM

Tags for this Thread