c++ question

Discussion in 'Homework Help' started by TAKYMOUNIR, Oct 29, 2011.

  1. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    //can some one please chech the wrong in this code i am new in c++ i feel that this may be easy question but it is hard for me
    thanks

    // Chapter 6, Programming Challenge 2
    #include <iostream>
    using namespace std;

    // Write the prototypes for the getLength,
    // getWidth, getArea, and displayData
    // functions here.
    double getLength(double);
    double getWidth(double);
    double getArea(double);
    void displayData();

    int main()
    {
    double length, // The rectangle's length
    width, // The rectangle's width
    area; // The rectangle's area

    // Get the rectangle's length.
    length = getLength(length);

    // Get the rectangle's width.
    width = getWidth(width );

    // Get the rectangle's area.
    area = getArea(length,width);

    // Display the rectangle's data.
    displayData(length, width, area);
    system("pause");
    return 0;
    }

    //***************************************************
    // You must write the getLength, getWidth, getArea, *
    // and displayData functions. *
    //***************************************************
    double getLength(double x)
    {
    cout<<"enter the length"<<endl;
    cin>>x;
    return x;
    }
    double getWidth(double x)
    {
    cout<<"enter the width"<<endl;
    cin>>x;
    return x;
    }
    double getArea(double x,double y)
    {
    double area=x*y;
    return area;
    }
    void displayData(double x,double y,double z)
    {
    cout<<"the length is\t"<<x<<endl;
    cout<<"the width is\t"<<y<<endl;
    cout<<"the area is\t"<<z<<endl;
    }
     
    Last edited: Oct 29, 2011
  2. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    The declaration of prototype for the functions getArea() and displayData() is wrong
    Always initialize your variables...
    Code ( (Unknown Language)):
    1.  
    2. [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Chapter 6, Programming Challenge 2[/COLOR][/SIZE]
    3. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<iostream>[/COLOR][/SIZE]
    4. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] std;[/SIZE]
    5. [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Write the prototypes for the getLength,[/COLOR][/SIZE]
    6. [SIZE=2][COLOR=#008000]// getWidth, getArea, and displayData[/COLOR][/SIZE]
    7. [SIZE=2][COLOR=#008000]// functions here.[/COLOR][/SIZE]
    8. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] getLength([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    9. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] getWidth([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    10. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] getArea([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Error two parameters[/COLOR][/SIZE]
    11. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] displayData([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Error three parameters[/COLOR][/SIZE]
    12. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] main()[/SIZE]
    13. [SIZE=2]{[/SIZE]
    14. [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Always initialize your variables...[/COLOR][/SIZE]
    15. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] length = 0, [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// The rectangle's length[/COLOR][/SIZE]
    16. [/COLOR][/SIZE][SIZE=2]width = 0, [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// The rectangle's width[/COLOR][/SIZE]
    17. [/COLOR][/SIZE][SIZE=2]area = 0; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// The rectangle's area[/COLOR][/SIZE]
    18. [SIZE=2][COLOR=#008000]// Get the rectangle's length.[/COLOR][/SIZE]
    19. [/COLOR][/SIZE][SIZE=2]length = getLength(length);[/SIZE]
    20. [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Get the rectangle's width.[/COLOR][/SIZE]
    21. [/COLOR][/SIZE][SIZE=2]width = getWidth(width );[/SIZE]
    22. [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Get the rectangle's area.[/COLOR][/SIZE]
    23. [/COLOR][/SIZE][SIZE=2]area = getArea(length,width);[/SIZE]
    24. [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Display the rectangle's data.[/COLOR][/SIZE]
    25. [/COLOR][/SIZE][SIZE=2]displayData(length, width, area);[/SIZE]
    26. [SIZE=2]system([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/SIZE]
    27. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 0;[/SIZE]
    28. [SIZE=2]}[/SIZE]
    29. [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//************************************************** *[/COLOR][/SIZE]
    30. [SIZE=2][COLOR=#008000]// You must write the getLength, getWidth, getArea, *[/COLOR][/SIZE]
    31. [SIZE=2][COLOR=#008000]// and displayData functions. *[/COLOR][/SIZE]
    32. [SIZE=2][COLOR=#008000]//************************************************** *[/COLOR][/SIZE]
    33. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] getLength([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] x)[/SIZE]
    34. [SIZE=2]{[/SIZE]
    35. [SIZE=2]cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"enter the length"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<endl;[/SIZE]
    36. [SIZE=2]cin>>x;[/SIZE]
    37. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] x;[/SIZE]
    38. [SIZE=2]}[/SIZE]
    39. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] getWidth([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] x)[/SIZE]
    40. [SIZE=2]{[/SIZE]
    41. [SIZE=2]cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"enter the width"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<endl;[/SIZE]
    42. [SIZE=2]cin>>x;[/SIZE]
    43. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] x;[/SIZE]
    44. [SIZE=2]}[/SIZE]
    45. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] getArea([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] x,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] y)[/SIZE]
    46. [SIZE=2]{[/SIZE]
    47. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] area=x*y;[/SIZE]
    48. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] area;[/SIZE]
    49. [SIZE=2]}[/SIZE]
    50. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] displayData([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] x,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] y,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] z)[/SIZE]
    51. [SIZE=2]{[/SIZE]
    52. [SIZE=2]cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"the length is\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<x<<endl;[/SIZE]
    53. [SIZE=2]cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"the width is\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<y<<endl;[/SIZE]
    54. [SIZE=2]cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"the area is\t"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<z<<endl;[/SIZE]
    55. [SIZE=2]}[/SIZE]
    56.  
    Next time try to read the compiler's error...and please post your code using code tags

    Good Luck
     
    TAKYMOUNIR likes this.
  3. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    thanks very much for your help and please how can i send my code

     
Loading...