Thread: Problem with function

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    30

    Problem with function

    Question regarding the response issue. I am having the user input 1 in order to calculate for meters/centimeters and 2 for inches and feet. It has to be an if/else statement, but whenever I run it, it continues onto the next calculation without selecting a choice. Any suggestions would be appreciated
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    void get_input ( double inches, double feet );
    
    
    void give_output( double centimeters, double meters );
    
    
    int main ()
    {
        double inches, feet, meters, centimeters;
        char response = '1' || '2';
    
    
        cout << "We are going to convert inches and feet into meters." << endl;
        cout << "Please input the measurement output you would like.";
        cout << "(1 for meter and centimeter   output, 2 for inches and feet output)";
        cin >> response;
        cout << endl;
    
    
        if ( response = 1 ) {
    
    
            void get_input ( double inches, double feet );
            {
    
    
            cout << "Please input the feet measurement: ";
            cin >> feet;
            cout << endl;
            cout << "Please input the inches measurement: ";
            cin >> inches;
            cout << endl;
            }
    
    
        feet = inches * 12;
        meters = feet * 0.3048;
        centimeters = meters * 100;
        inches = feet / 12;
    
    
            void give_output( double centimeters, double meters );
            {
            cout << " There are ";
            cout << meters;
            cout << " meters from the given measurements." << endl;
            cout << " There are ";
            cout << centimeters;
            cout << " centimeters from the given measurements." << endl;
            }
        }
        else ( response = 2 ); {
    
    
        void get_input ( double meters, double centimeters );
            {
            cout << "Please input the meters measurement: ";
            cin >> meters;
            cout << endl;
            cout << "Please input the centimeters measurement: ";
            cin >> centimeters;
            cout << endl;
            }
    
    
            void give_output( double feet, double inches );
            {
            cout << " There are ";
            cout << feet;
            cout << " feet from the given measurements." << endl;
            cout << " There are ";
            cout << inches;
            cout << " inches from the given measurements." << endl;
            }
        }
    }
    Last edited by iGuardian; 09-27-2011 at 10:52 PM.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Code:
     char response = '1' || '2';
    //what do you think this does?
    Code:
     if ( response = 1 ) { 
    //this is the assignment operator. You need if(response ==1)
    Additionally, since you define response as a char (not sure why you would do that) that evaluation is always false. You really need to sit down and re-read your textbook. You are missing some serious knowledge here.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I am thinking you need to read some basic tutorials on if statements and functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    Code:
    1. #include <iostream>
    2. using namespace std;
    3. void get_input ( double &inches, double &feet );
    4. void give_output( double centimeters, double meters );
    5. int main ()
    6. {
    7. double inches, feet, meters, centimeters;
    8. char response = '1' || '2' ;
    9. char answer = 'y';
    10. cout << "We are going to convert inches and feet into meters." << endl;
    11. cout << "Please input the measurement output you would like." << endl;
    12. cout << "(1 for meter and centimeter output, 2 for inches and feet output): ";
    13. cin >> response;
    14. cout << endl;
    15. if ( response == '1' ) {
    16. get_input ( inches, feet );
    17. feet = inches * 12;
    18. meters = feet * 0.3048;
    19. centimeters = meters * 100;
    20. give_output ( centimeters, meters );
    21. cout << " Would you like to re-use this program ( Y - yes, N - no ): ";
    22. cin >> answer;
    23. }
    24. else if ( response == '2' ) {
    25. void get_input ( double meters, double centimeters );
    26. {
    27. cout << "Please input the meters measurement: ";
    28. cin >> meters;
    29. cout << endl;
    30. cout << "Please input the centimeters measurement: ";
    31. cin >> centimeters;
    32. cout << endl;
    33. }
    34. inches = feet * 12;
    35. feet = meters * 39.37;
    36. meters = centimeters / 100;
    37. void give_output( double inches, double feet );
    38. {
    39. cout << " There are ";
    40. cout << feet;
    41. cout << " feet from the given measurements." << endl;
    42. cout << " There are ";
    43. cout << inches;
    44. cout << " inches from the given measurements." << endl;
    45. }
    46. cout << " Would you like to re-use this program ( Y - yes, N - no ): ";
    47. cin >> answer;
    48. }
    49. }
    50. void get_input ( double &inches, double &feet )
    51. {
    52. cout << "Please input the feet measurement: ";
    53. cin >> feet;
    54. cout << endl;
    55. cout << "Please input the inches measurement: ";
    56. cin >> inches;
    57. cout << endl;
    58. }
    59. void give_output( double centimeters, double meters )
    60. {
    61. cout << " There are ";
    62. cout << meters;
    63. cout << " meters from the given measurements." << endl;
    64. cout << " There are ";
    65. cout << centimeters;
    66. cout << " centimeters from the given measurements." << endl;
    67. }
    Is this on the right track?

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Yes, you are on the right track. Lines 8 and 9 only need to declare the variable; specifically line 8 doesn't really do anything useful. Your program doesn't actually loop; take a look at Lesson 3:Loops. Additionally, you still haven't wrapped your head around functions yet - do not define them in main. Read Lesson 4:Functions.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Function problem
    By elwad in forum C Programming
    Replies: 5
    Last Post: 06-14-2009, 04:14 PM
  3. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  4. Replies: 5
    Last Post: 10-17-2006, 08:54 AM
  5. Function Problem
    By niroopan in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2002, 11:15 AM