新程式員從基礎開始。我需要撰寫一個代碼來檢查字串(行)的長度并對其進行處理。在開始下一部分任務之前,我正在努力使長度正確。
當我運行以下代碼時發生了什么,無論我輸入什么,strlen(string1) 似乎都默認為“40”?
我不確定出了什么問題!任何幫助將不勝感激。
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
char line, just, jline;
void prompt();
int main() {
cout << "Enter a line of text: " << endl;
char string1[line];
cin >> line;
char string2[] = "1234567890123456789012345678901234567890";
line = strlen(string1);
just = strlen(string2);
cout << "Length of string is: " << strlen(string1) << endl;
if ((strlen(string1)) > (strlen(string2))) {
cout << "Error, your line must be less than 40 characters" << endl;
}
}
uj5u.com熱心網友回復:
您的代碼存在 2 個問題,如下所述:
問題 1
在標準 C 中,陣列的大小必須是編譯時間常數。這意味著您的程式中的以下內容不正確:
char string1[line]; //not standard C because line is not a constant expression
問題 2
請注意,這還有另一個問題。由于line是一個全域變數,它將被靜態初始化為0. 因此char string1[line];實際上等價于char string1[0];。
`但是從陣列宣告器檔案中:
如果運算式是常量運算式,則它的值應大于零。
因此,char string1[line];也因這個原因無效。
解決方案
更好的是std::string如下所示:
#include <iostream>
#include <string>
int main() {
std::cout << "Enter a line of text: " << std::endl;
std::string inputLine;
std::getline(std::cin, inputLine);//take input from user and put it into inputLine
std::string var = "1234567890123456789012345678901234567890";
//----------------------------------------------------vvvvv----------->use size member function of std::string
std::cout << "Length of string is: " << inputLine.size() << std::endl;
if (inputLine.size() > var.size()) {
std::cout << "Error, your line must be less than 40 characters" << std::endl;
}
else
{
std::cout<<"valid input"<<std::endl;
}
}
演示。
在上面的代碼片段中,我們曾經std::string::size知道std::string.
此外,應盡可能避免使用全域變數。請參閱全域變數是否不好?.
uj5u.com熱心網友回復:
到目前為止,您的 line 變數是 char 型別。這意味著它包含一個字符。如果要存盤字串,可以使用固定大小的字符陣列(c 樣式字串)或字串物件。在 c 中,字串物件應該是首選,因為它們比 c 風格的字串提供了廣泛的優勢,包括更高的安全性和卓越的易用性。
例如,如果您想獲取用戶輸入的字串物件的長度,您可以撰寫如下內容
#include <iostream>
#include <string>
// if you don’t want to use std:: put using namespace std; after the includes
int main(){
std::string str;
int strLength
std::cout << “Enter a Single Word: “;
std::cin >> str;
strLength = str.lenght();
std::cout << “The length of the word is “ << strLength << std::endl;
return 0;
}
如果你想得到一整行文本,你會替換
std::cout << “Enter a Single Word: “;
std::cin >> str;
和
std::cout << “Enter a Line of text: “;
std::getline(cin, str);
您也可以使用 c 樣式的字串和 strlen() 但是,要安全地執行此操作要困難得多,并且通過執行諸如撰寫比所用固定大小陣列的大小更長的字串之類的操作會更容易引起問題這可能會導致問題,因為額外的資料可以寫入不應寫入的區域,因此如果您剛剛開始,您可能應該使用字串物件。
如果您想了解有關 std::string 的更多資訊,請訪問 https://en.cppreference.com/w/cpp/string/basic_string
關于為什么 strlen 回傳 40。 strlen() 的作業方式是它接受一個稱為指標的變數,該指標指向字符陣列的開頭,然后遍歷從指標開始的字符陣列并查找稱為空字符 '\0' 并回傳陣列中此空字符之前的字符數。如果 c 樣式的字串不以空字符結尾,這可能會導致問題。
在您提供的代碼的情況下,string2 包含“”內的所有 40 個字符加上末尾的空字符,因此當使用 string2 呼叫 strlen() 時,它會計算空字符之前的 40 個字符。
在 string1 的情況下,當它被創建時,它的大小等于未初始化的變數 line 中包含的值,在這種情況下看起來是 0。這導致 string1 被創建為大小為 0(根據對于語言標準,這是未定義的行為,但在某些情況下它可能仍然“有效”)。因為 string2 稍后在記憶體中 string1 之后的位置創建,所以在將 string1 傳遞給 strlen 時創建的指向 string1 開頭的指標指向 string2 的開頭,從而導致 strlen 在呼叫 string1 時有效地測量 string2 的長度。
最后,作為提示。盡量避免將變數放在全域范圍內(在函式或其他封裝物體(如類)之外)。這些全域變數有被函式無意更改的風險,可能會導致代碼出現問題。通常最好將這些變數放在 main 中并將它們傳遞給要在其中使用它們的函式。如果想讓函式影響這些變數,可以將它們作為參考傳遞給函式。為此,您將在函式的引數型別后面放置一個 & ,該變數將被傳入,如下所示。
void function(std::string &str);
//… some other code
//… somewhere in main or another function
function(alteredString);
// some other code …
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/475455.html
上一篇:將原始字串轉換回十六進制字符
