2013-03-04 48 views
-1

我的字符串d顯示空(甚至沒有空格)我的控制檯上,這讓我很困惑,因爲我初始化爲「空」,我試圖以一個新值分配給它的不是一個空值。顯示我的初始化的變量空

int main(){ 

    string user_String[99]; 
    int user_Input = 0; 

    cout << "Please insert up to one hundred Strings: "; 
    cin >> user_Input; 


    //Check for range 
    bool check = false; 
    while(check == false){ 
     if (user_Input < 1 || user_Input >100){ 
      cout << "Please insert up to one hundred Strings: "; 
      cin >> user_Input;} 
     else{ 
      check = true; 
      break;} 
    } 

    //User input 
    cout <<"Please enter string"<< endl; 
    for (int counter = 0; counter < user_Input; counter++){ 
     int counter2 = counter + 1; 
     cout << "Enter String " << counter2 << ": "; 
     cin >> user_String[counter]; 
    } 

    //Loopig for most letters 
    string c = "NULL"; 
    for(int counter = 0; counter < user_Input; counter++){ 
     //Making Sure Coun doesn't go out of range 
     int coun = 0; 
     if (counter < user_Input){ 
      coun = counter +1;} 
     else{ 
      coun = counter; 
     } 

     string a = user_String[counter]; 
     string b = user_String[coun];  

     if (a.length() < b.length() && c == "NULL"){ 
      c = b; 
     } 

     if(a.length() < b.length() && c!="NULL" && c.length() < b.length()){ 
      c = b; 
     } 
     else{ 
      continue; 
     } 

    } 
    cout << "The string "<< c <<" have the most letters." << endl; 

    //Looping for least letters 
    string d = "NULL"; 
    for(int counter = 0; counter < user_Input; counter++){ 
     //Making Sure Coun doesn't go out of range 
     int coun = 0; 
     if (counter < user_Input){ 
      coun = counter +1;} 
     else{ 
      coun = counter; 
     } 

     string a = user_String[counter]; 
     string b = user_String[coun]; 

     if (a.length() > b.length() && d == "NULL"){ 
      d = b; 
     } 

     if(a.length() > b.length() && d!="NULL" && d.length() > b.length()){ 
      d = b; 
     } 
     else{ 
      continue; 
     } 
    } 
    cout << "The string " << d <<" have the least letters." << endl; 


    system("pause"); 
    return 0; 
} 
+1

我會變得非常混亂,如果一個程序叫我輸入字符串,並期望一個整數。 – chris 2013-03-04 20:09:47

+1

您如何確定您分配的新值不是空的?我會建議使用調試器來查看實際發生的情況。 – 2013-03-04 20:10:08

回答

1

您允許用戶輸入最多100個字符串,但您的陣列最多隻能容納99個字符串。如果他們實際輸入了100個字符串,最後一個字符串會損壞內存,假設你的代碼不會完全崩潰。

而且,你的信迴路中都有一些錯誤的邏輯。 if (counter < user_Input)總是爲真,所以coun永遠是counter+1並且因此超出陣列的邊界時counter到達陣列的端部。此外,你的循環對於他們正在嘗試做的事情來說是不必要的複雜。

試試這個:

int main() 
{ 
    string user_String[100]; 
    int user_Input; 

    do 
    { 
     cout << "Please enter the number of Strings (1-100): "; 
     if (cin >> user_Input) 
     { 
      if ((user_Input >= 1) && (user_Input <= 100)) 
       break; 
     } 
     else 
      cin.clear(); 
    } 
    while (true); 

    for (int counter = 0; counter < user_Input; ++counter) 
    { 
     cout << "Enter String " << counter + 1 << ": "; 
     cin >> user_String[counter]; 
    } 

    string b = user_String[0]; 
    for(int counter = 1; counter < user_Input; ++counter) 
    { 
     string a = user_String[counter]; 
     if (a.length() > b.length()) 
      b = a; 
    } 

    cout << "The string " << b << " has the most letters." << endl; 

    b = user_String[0]; 
    for(int counter = 1; counter < user_Input; ++counter) 
    { 
     string a = user_String[counter]; 
     if (a.length() < b.length()) 
      b = a; 
    } 

    cout << "The string " << b <<" has the least letters." << endl; 

    system("pause"); 
    return 0; 
} 

雖這麼說,你可以得到完全擺脫陣列和循環融合在一起:

int main() 
{ 
    string user_String; 
    int user_Input; 

    do 
    { 
     cout << "Please enter the number of Strings: "; 
     if (cin >> user_Input) 
     { 
      if (user_Input >= 1) 
       break; 
     } 
     else 
      cin.clear(); 
    } 
    while (true); 

    string fewestLetters, mostLetters; 
    for (int counter = 1; counter <= user_Input; ++counter) 
    { 
     cout << "Enter String " << counter << ": "; 
     cin >> user_String; 

     if (counter == 1) 
     { 
      mostLetters = user_String; 
      fewestLetters = user_String; 
     } 
     else 
     { 
      if (user_String.length() > mostLetters.length()) 
       mostLetters = user_String; 

      if (user_String.length() < fewestLetters.length()) 
       fewestLetters = user_String; 
     } 
    } 

    cout << "The string " << mostLetters << " has the most letters." << endl; 
    cout << "The string " << fewestLetters << " has the least letters." << endl; 

    system("pause"); 
    return 0; 
} 
+1

在第二個例子中,最少不要總是長度爲0,因此從不分配任何東西?我會添加一個檢查!empty()以確保第一個字符串被分配給它以開始滾動。 – 2013-03-04 20:20:55

+0

@RetiredNinja:好的。 – 2013-03-04 20:35:18

+0

如果輸入'a'作爲字符串的數量,則會進入無限循環。嘗試'while(!(std :: cin >> user_Input)){std :: cin.clear(); std :: cout <<「請輸入字符串數(1-100):」;}' – 2013-03-04 21:18:49