2010-09-21 85 views
0

我的代碼基本上是列出一個字符串,它是輸入的ASCII碼點,我下面的代碼很簡單,在這裏:C++:打印/分配簡單陣列打印亂碼

#include <iostream> 
#include <string.h> 

using namespace std; 

int main() { 
    char str[20]; 
    int result[20]; 
    cin >> str; 

    for(int i = 0; i != strlen(str); i++) { 
     result[i] = (int)i; 
    } 

    for(int i = 0; i != 20; i++) 
     cout << result[i] << ", "; 
} 

當我運行它,不管是什麼輸入其輸出亂碼像未定義的存儲一堆像這樣:

0, 1, 2, 3, 4, 5, 1, -1217349408, -1220040795, -1220041307, -1076427112, 134514781, -1218903292, 134519344, -1076427096, 134514004, -1217411568, 134519344, -1076427048, 134514681, 

我缺少的東西在簡單的我如何追加每個整數數組?

請注意這是一個簡單的例子,我的輸入不會超過20個字符。

編輯錯字在我的結果.. CIN >>結果CIN >>海峽

+2

固定大小的char數組?我眼睛疼!在C++中使用'std :: string'。 – fredoverflow 2010-09-21 11:22:23

+0

不要在循環退出條件中使用strlen - 對每個循環都進行此調用效率不高。首先將strlen存儲爲本地,因爲字符串在循環內不變。 – 2010-09-21 13:51:59

回答

2

該循環將迭代多次,等於'str'的長度。也就是說,它將爲'str'中的每個字符迭代一次,並停止在'null terminator'(char值爲0)這是c字符串的結束方式。在每個循環中,'i'的值是循環數,從0開始 - 這是您在結果數組中指定給該索引的值。

for(int i = 0; i != strlen(str); i++) { 
    result[i] = (int)i; 
} 

因此,例如,對於長度爲5的一個字符串,就會在這些索引分別分配值「0,1,2,3,4」到結果陣列。結果數組中的其他值未分配 - 因此可以保存任何值(通常,在開始使用它之前,無論內存位置如何)。如果您的字符串超過20個字符,則會遇到問題,因爲您將開始嘗試訪問索引爲20及以上的數組,這不屬於程序的內存。

for(int i = 0; i != 20; i++) 
    cout << result[i] << ", "; 

因此,將打印初始化值,並且,如果:在索引19

這個循環打印出所有的值的「結果」陣列中,從索引0處的值的值字符串長度小於20個字符,也是未初始化的值。

在最低限度,開始得到類似的東西你後的結果,你想改變

result[i] = (int)i; 

result[i] = str[i]; 

但其他人所說,並逃避一些我上面提到的內存訪問問題,如果使用迭代器來獲取字符值會更好。

for(string::iterator i = str.begin(); i != str.end(); i++) 
    // access char here using '*i' 
+0

謝謝,這麼晚了,在我的OP中一直犯錯誤。那是一個愚蠢的錯誤。謝謝。 – John 2010-09-21 11:19:37

0

你沒有初始化str和你正在服用的strlen

當你做

cin >> result; // this does not even compile!!! 

我想你的意思是

cin >> str; 

它不清楚你想做什麼。不過你可以試試這個得到一些有意義的結果:

char str[20]; 
int result[20] = {0}; 
cin >> str; 
...// rest all unchanged. 
+0

隨着我的編輯..我如何初始化? 'char str [20] = {「」}; int result [20] = {0};',我不確定如何初始化數組。我的結果是20個左右的零個數字,也許這是我分配給數組的錯誤.. – John 2010-09-21 11:08:27

1

strlen(str)會給你一個未定義的輸出,因爲你沒有初始化的str[]內容。

0

stlen(str)將給出空終止符之前的字符數

這意味着只有strlen(str)整數是有效的。其餘未初始化。

另請參閱:看看std::transform。你可以避免臨時的整數數組達到相同的數值,或者合併成一個整數。

int to_codepoint(char c) { return (int) c; } 


// ... 
char cs[] = "abcd"; 
std::transform(cs, cs+strlen(cs) 
      , std::ostream_iterator<int>(std::cout, ", "), to_codepoint); 

// or transform into an array: 
int is[ 1000 ]; // 1000 enough? 
std::transform(cs, cs+strlen(cs) 
       , is, to_codepoint); 

test code at codepad.org

1

你有3個問題:

  1. 你沒有用正確的字符串初始化str,從而strlen將返回一個不可預測的值。
  2. 您初始化了result的第一個strlen(str)位置,但後來您將其打印到索引20之前。您應該在兩個循環中使用相同的條件。
  3. 你一定要使用std::string及其迭代器。
+0

而你,你不能算。但2似乎是答案:) – xtofl 2010-09-21 11:21:12

+0

@xtofl:哎呀,確實忘了更新計數。謝謝! – jweyrich 2010-09-21 11:23:09

1

本質上,您未能正確初始化字符串,並且未檢查它是否是正確的大小。正確的代碼:

#include <iostream> 
#include <string> // NOT <string.h>, <string> 

int main() { 
    std::string str; 
    std::cin >> str; 
    std::cin.ignore(); 
    for(std::string::iterator it = str.begin(); it != str.end(); it++) { 
     std::cout << (int)(*it); 
     if (it + 1 != str.end()) 
      std::cout << ", "; 
     else 
      std::cout << "\n"; 
    } 
    std::cin.get(); 
}