2016-10-30 27 views
-1

我想隨機添加一個字符串中的空格,直到總共80個字符長的字符串。出於某種原因,我的程序無法正常工作。我在這裏錯過了什麼嗎?它只在同一位置輸入空格,而不是隨機輸入:/。如何進行文字對齊?

#include <iostream> 
#include <string> 
#include <ctime> 
#include <cstdlib> 

using std::cin; using std::cout; using std::string; using std::endl; 

const int line_width = 80; 

int main() 
{ 
    //declare string and get user input 
    string not_justified; 
    cout << "Input a line of text less than 80 characters to be justfied: " << endl; 
    getline(cin, not_justified); 

    int position = not_justified.find(' '); 

    //start random number generator 
    srand(time(nullptr)); 

    while (not_justified.size() != line_width) 
    { 
     //find space position 
     position = not_justified.find(' '); 

     //get random number from 1-80 
     int random_number = rand() % 80 + 1; 

     //test to see if number is less than 40, if it is return true 
     random_number < 40 ? true : false; 

     //if true, insert a space 
     if (true) 
      not_justified.insert(position, " "); 

     position += position; 
    } 

    cout << "Your justified line is: " << not_justified << endl; 
} //end main 

我的輸出是這樣的:

Input : My name is bob 

OutPut: Debug Error! abort() has been called 
+1

在調試器中逐行執行代碼。原因應該變得明顯。 –

+0

你爲什麼使用隨機數字?我的理解是,理由取決於行中的單詞和行的寬度,不需要隨機數字。 –

+0

我正在使用一個隨機數,因爲我不需要將空間均勻分佈,所以隨機性似乎是下一個最佳選擇。 – Inert

回答

1

首先,我真的很討厭的事實,我無法評論,除非我有超過50聲譽;因此我的大部分輸入都由假設組成。

你做了什麼錯

首先,你總是會在相同的位置上安放的空間,在第一次(實際上是實現定義的)空間中的位置。其中,對於字符串"My name is Bob"將在位置2。其次,您的隨機生成器對空間插入發生的位置沒有貢獻。

最後,您檢查是否隨機生成的數字在限制範圍內的方法是不正確的。此語句random_number < 40 ? true : false;沒用,它根本沒有貢獻或改變代碼的行爲,並且很可能已被編譯器優化。你還應該注意,random_number < 40確實是一樣的,但代碼污染較少。

固定碼

#include <iostream> 
#include <string> 
#include <ctime> 
#include <cstdlib> 
#include <random> 
#include <vector> 

using std::cin; using std::cout; using std::string; using std::endl; 

const int line_width = 80; 

std::vector<size_t> find_all_of(const std::string &str, const char &what = ' ') 
{ 
    auto count = 0u; 
    std::vector<size_t> result; 
    for (auto &elem : str) 
    { 
     if (elem == what) 
      result.emplace_back(count); 
     ++count; 
    } 
    return result; 
} 

int main() 
{ 
    //declare string and get user input 
    string not_justified; 
    cout << "Input a line of text less than 80 characters to be justfied: " << endl; 
    getline(cin, not_justified); 

    std::mt19937 rng{ std::random_device()() }; // random number generator 
    while (not_justified.size() < line_width) 
    { 
     auto spaces = find_all_of(not_justified); // find all of the current spaces 
     std::uniform_int_distribution<size_t> distribution{ 0, spaces.size() - 1 }; // only allow results within the bounds of spaces 
     auto where = spaces[distribution(rng)]; // select a random position using the distribution method 
     not_justified.insert(where, " "); // insert it. 
    } 
    cout << "Your justified line is: " << not_justified << endl; 
    cin.get(); 
} //end main 

其他景點

rand()被認爲是有害的。 Source

+0

嗨@yamiez,你可以更新你的答案給一些高層次的細節,以'蘭特()'是被認爲有害。您鏈接的視頻中的一些關鍵點是所有需要的。原因在於,如果視頻消失,它仍然與您的答案具有上下文相關性。 – Adrian