2013-04-18 70 views
0

因此,這裏是我的代碼:需要幫助我的程序修復的小問題

#include <iostream> 
#include <string> 
#include <vector> 

using namespace std; 

class Point 
{ 
private: 
    double px; 
    double py; 

public: 
    void setX(const double x); 
    void setY(const double y); 
    double getX() const; 
    double getY() const; 
}; 

class Rectangle 
{ 
private: 
    string name; 
    Point blPoint; 
    double length, height; 

public: 
    // member functions 
    void setName(const string & inName); 
    void setBottomLeft(const double x, const double y); 
    void setDimensions(const double inLength, const double inHeight); 

    string getName() const; 
    Point getBottomLeft() const; 
    double getLength() const; 
    double getHeight() const; 

    double area() const; 
    double perimeter() const; 
    Point midPoint() const; 
    void scaleBy2(); 
    void display() const; 
}; 

// FUNCTION PROTOTYPES GO HERE: 
void welcome(); 
bool read_rec(const string promptName, const string errorInvalid, const string errorUsed, string & inName, vector<Rectangle> & list); 
void read_coord(const string promptPoint, double & x, double & y); 
void read_length(const string promptLength, double & inLength, double & inHeight); 
void add_rec(const string Name, double x, double y, double inLength, double inHeight, vector<Rectangle> & list); 

int main() 
{ 
    // Define your local variables, e.g. a vector of class Rectangle 
    Rectangle rec; 
    vector<Rectangle> list; 
    string prompt1stName = "Enter the name of the first rectangle: "; 
    string promptName = "Enter the name of the next rectangle: "; 
    string errorInvalid = "Invalid input. Type 'rec' following by the name or 'stop' if done."; 
    string errorUsed = "This name is already being used!"; 
    string inName; 
    string Name; 

    // Display welcome banner 
welcome(); 

    /* Prompt user for first rectangle or 'stop' */ 
    bool read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list); 

    // WHILE user input is invalid 
    while (read == false) 
    { 

     // Display "Try again! " 
cout << "Try again! " << endl; 
     read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list); 
    } 

    // IF user input is not 'stop' 
     if (inName != "stop") 
    { 
     // Extract rectangle name from user input 
       int a = inName.length() - 4; 
     Name = inName.substr(4, a); 

     // Prompt for bottom left point 
       double x, y; 
     string promptPoint = "Enter " + Name + "'s bottom left x and y coords: "; 
     read_coord(promptPoint, x, y); 

     // Prompt for length and height 
       double inLength, inHeight; 
     string promptLength = "Enter " + Name + "'s length and height: "; 
     read_length(promptLength, inLength, inHeight); 

     // Add rectangle to the rectangle list 
     add_rec(Name, x, y, inLength, inHeight, list); 
    } 
    /* Prompt user for next rectangle or 'stop' */ 
    // WHILE user input not 'stop' 
     while (inName != "stop") 
    { 
     // Display "Thank you! " 
cout << "Thank you! "; 
bool read = read_rec(promptName, errorInvalid, errorUsed, inName, list); 

     // WHILE user input is invalid while (read == false) 
     { 

      // Display "Try again! " 
         cout << "Try again! " << endl; 
      read = read_rec(promptName, errorInvalid, errorUsed, inName, list); 
     } 

      // IF user input is not 'stop' 
        if (inName != "stop") 
     { 

       // Extract rectangle name from user input 
          int a = inName.length() - 4; 
      Name = inName.substr(4, a); 

       // Prompt for bottom left point 
          double x, y; 
      string promptPoint = "Enter " + Name + "'s bottom left x and y coords: "; 
      read_coord(promptPoint, x, y); 

       // Prompt for length and height 
          double inLength, inHeight; 
      string promptLength = "Enter " + Name + "'s length and height: "; 
      read_length(promptLength, inLength, inHeight); 


       // Add rectangle to the rectangle list 
      add_rec(Name, x, y, inLength, inHeight, list); 
     } 
    } 

    // IF the rectangle list is not empty 
     if (list.size() != 0) 
    { 
     // Display all rectangles in the rectangle list 
int rec_num = 0; 
int i = 1; 
while (i< list.size()) 
{ 
    rec_num++; 
    i++; 

} 
      cout << "You have " << rec_num+1 << " rectangle(s) in your list: "; 
      cout << endl; 

       for (int i = 0; i < list.size(); i++) 
     { 
      cout << "Rectangle '" << list[i].getName() << "' : "; 
      list[i].display(); 
      list[i].scaleBy2(); 
      cout << "  After scale by 2: "; 
      list[i].display(); 
      cout << endl; 
     } 
    } 

    // ELSE 
    else 
     { 
     // Display that no rectangles are in the list 
       cout << "You have no rectangles in your list." << endl; 
    } 

    return 0; 
} 

// FUNCTION DEFINITIONS GO HERE: 
void welcome() 
{ 
    cout << "Welcome! Create your own list of rectangles." << endl; 
    cout << "You will be asked to provide information about each rectangle in your list by name." << endl; 
    cout << "Type the word 'stop' for the rectangle name when you are done." << endl; 
    cout << endl; 
} 

bool read_rec(const string promptName, const string errorInvalid, const string errorUsed, string & inName, vector<Rectangle> & list) 
{ 
    cout << promptName; 
    getline(cin, inName); 

    if (inName == "stop") 
    { 
     return(true); 
    } 
    else if (inName.substr(0,4) != "rec ") 
    { 
     cout << errorInvalid; 
     return(false); 
    } 
    else 
    { 
     int j = 0; 
     for (int i = 0; i < list.size(); i++) 
     { 
      if (inName == "rec " + list[i].getName()) 
      { 
       j = j+1; 
      } 
     } 
     if (j == 0) 
     { 
      return(true); 
     } 
     if (j != 0) 
     { 
      cout << errorUsed; 
      return(false); 
     } 
    } 
} 

void read_coord(const string promptPoint, double & x, double & y) 
{ 
    cout << promptPoint; 
    cin >> x; 
    cin >> y; 
    } 

void read_length(const string promptLength, double & inLength, double & inHeight) 
{ 
    cout << promptLength; 
    cin >> inLength; 
    cin >> inHeight; 
    cout << endl; 

    while (inLength <= 0 || inHeight <= 0) 
    { 
     cout << "Make length and height positive values. Try again."; 
     cout << promptLength; 
     cin >> inLength; 
     cin >> inHeight; 
     cout << endl; 
    } 
} 

void add_rec(const string Name, double x, double y, double inLength, double inHeight, vector<Rectangle> & list) 
{ 
    Rectangle rec; 
    rec.setName(Name); 
    rec.setBottomLeft(x, y); 
    rec.setDimensions(inLength, inHeight); 
    list.push_back(rec); 
} 

// CLASS MEMBER FUNCTION DEFINITINOS GO HERE: 

void Point::setX(const double x) 
{ 
    px = x; 
} 

void Point::setY(const double y) 
{ 
    py = y; 
} 

double Point::getX() const 
{ 
    return (px); 
} 

double Point::getY() const 
{ 
    return (py); 
} 

void Rectangle::setName(const string & inName) 
{ 
    name = inName; 
} 

void Rectangle::setBottomLeft(const double x, const double y) 
{ 
    blPoint.setX(x); 
    blPoint.setY(y); 
} 

void Rectangle::setDimensions(const double inLength, const double inHeight) 
{ 
    length = inLength; 
    height = inHeight; 
} 

string Rectangle::getName() const 
{ 
    return (name); 
} 

Point Rectangle::getBottomLeft() const 
{ 
    return (blPoint); 
} 

double Rectangle::getLength() const 
{ 
    return (length); 
} 

double Rectangle::getHeight() const 
{ 
    return (height); 
} 

double Rectangle::area() const 
{ 
    // area = length * height 
    return(length * height); 
} 

double Rectangle::perimeter() const 
{ 
    // perimeter = 2 * (length + height); 
    return(2 * (length + height)); 
} 

Point Rectangle::midPoint() const 
{ 
    Point midPoint; 
    double mx = blPoint.getX() + 0.5 * length; 
    double my = blPoint.getY() + 0.5 * height; 
    midPoint.setX(mx); 
    midPoint.setY(my); 
    return(midPoint); 
} 

void Rectangle::scaleBy2() 
{ 
    double midx = blPoint.getX() + 0.5 * length; 
    double midy = blPoint.getY() + 0.5 * height; 
    double newblPx = midx - length; 
    double newblPy = midy - height; 
    length = 2*length; 
    height = 2*height; 
    blPoint.setX(newblPx); 
    blPoint.setY(newblPy); 
} 

void Rectangle::display() const 
{ 
    cout << " Location is (" << blPoint.getX() << ", " << blPoint.getY() << "), length is " << length << ", height is " << height << "; Area is " << area() << "; perimeter is " << perimeter() << ", midpoint is located at (" << midPoint().getX() << ", " << midPoint().getY() << ")" << endl; 
} 

唯一的問題的我現在已經與該程序是,它總是輸出「無效的輸入型‘REC’由名以下。或者'停止'如果完成。「,我不知道如何改變這一點。而且,如果您在rec fire和rec fire中輸入了重複的答案,則會說rec火已被使用,然後繼續提示輸入該矩形,而不是詢問其他名稱。任何幫助將非常感激!!

+1

這是很多的代碼,並不真正適合本網站的問答風格,請嘗試http://meta.stackexchange.com/questions/tagged/codereview-se – mark

+0

@mark:小心你如何使用短語 - 它聽起來可能聽起來像你說的「嘗試Meta SO」([OP])(http://meta.stackexchange.com/questions/177082/need-help-fixing-minor-issues-in-my-program )) –

+0

@arazan:Mark建議你標記你的帖子,並要求版主將其移至Code Review。 –

回答

1

這是錯誤的

/* Prompt user for first rectangle or 'stop' */ 
bool read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list); 

// WHILE user input is invalid 
while (read == false) 
{ 

    // Display "Try again! " 
    cout << "Try again! " << endl; 
    bool read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list); 
} 

你有讀變量,while條件讀變量指的是讀變量聲明第一,讀變量聲明第二個是從未使用過。你想要的是這樣的

/* Prompt user for first rectangle or 'stop' */ 
bool read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list); 

// WHILE user input is invalid 
while (read == false) 
{ 

    // Display "Try again! " 
    cout << "Try again! " << endl; 
    read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list); 
} 

現在你只有一個讀取變量。這解釋了我描述的第二個錯誤。

編碼此的另一種方式是這樣的

for (;;) 
{ 
    bool read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list); 
    if (read) 
     break; 
    cout << "Try again! " << endl; 
} 

在我看來,這種循環是更好,因爲它不具備重複調用read_rec,所以用這種風格的循環,你犯的錯是不可能的。

+0

謝謝!修正了這個錯誤,你有什麼想法,如何解決它從輸出errorInvalid時,它不是一個無效的錯誤?我相信它是這樣做的,因爲它不會= rec,但是此時用戶沒有輸入任何內容。 – arazan

+0

不確定,對不起。我將使用調試器逐步跟蹤程序中發生的情況。 – john

+0

感謝您的幫助!我現在就下載一個! – arazan