2014-05-10 46 views
1

所以我有一個硬件問題我的班級,我必須存儲十個名字,大寫他們,然後按字母順序排序。我們剛開始使用字符串,有些東西對我來說仍然有點困惑。此代碼可以將所有名稱存儲在字符串數組中,但UpperCase似乎不起作用。我不知道,但我認爲這是因爲我有第二次循環運行帽的次數,這將是10.並且因爲不是每個字符串都會有10個元素,我遇到問題了嗎?..是這樣嗎?這是別的嗎?那麼我試圖通過使用.length(函數?)來解決這個問題,以查找數組中每個名稱的長度,但我總是會得到錯誤。任何幫助表示讚賞,謝謝!字符串排列作業

#include<iostream> 
#include<string> 
using namespace std; 

void UpperCase(string names[],int cap); 

void print(string names[],int cap); 
void swap(string names[],int &x,int &y); 

string names[10]; 

int main(){ 
    char a; 
    cout<<sizeof(a); 
    for(int i=0;i<10;i++){ 
     cout<<"Enter a name for student "<<i+1<<" : "; 
     cin>>names[i]; 
     cout<<endl; 
    } 

    UpperCase(names,10); 
    cout<<endl; 
    print(names,10); 

    cout<<endl; 
    print(names,10); 

    return 0; 
} 

void print(string names[],int cap){ 
    for(int i=0;i<cap;i++) 
     cout<<names[i]<<endl; 
} 

void UpperCase(string names[],int cap){ 
for(int student=0;student<cap;student++){ 
    for(int letter=0;letter<names[student].length();letter++){ 
     if(names[student][letter]>='a') 
      names[student][letter]-=('a'-'A'); 
    } 
} 

}

+0

STL是否沒有問題? –

+2

你說'cap'是'UpperCase()'中的內循環的錯誤終止條件,'.length()'是正確的,所以請給出後者的代碼。另外:如果名稱包含字符「a」會發生什麼? –

+1

有趣的是,您將使用'std :: string',然後使用數組而不是更高級的容器類。 –

回答

3

你的內環應迭代的字符串的長度。 string::length()是一個函數,而不是一個字段,所以你需要括號。還有一個用於將字符轉換爲大寫的標準庫函數。

#include <cctype> 
using std::toupper; 

void UpperCase(string names[],int cap){ 
    for(int student=0;student<cap;student++){ 
     for(int letter=0;letter<names[student].length();letter++){ 
      names[student][letter] = toupper(names[student][letter]); 
     } 
    } 
} 
1

...,但大寫似乎無法正常工作

所以寫一個最小的程序上已知輸入是只是電話UpperCase。它使調試變得很容易,並且會提出更好的問題。我們不需要看到打印或交換來提供幫助,並提示用戶輸入與UpperCase是否有效無關。

說了這麼多,你應該使用std::string::length() - 但是,你說

嗯,我試圖通過使用。長度(功能?),找到每個名字的長度數組中解決這個問題,但我總是得到錯誤

但不顯示你實際嘗試了什麼,或者錯誤是什麼。


這裏有一個最小的,完整的,獨立的程序,使用std::toupper按照T.C.的答案。我改變它以展現更現代的風格,並且展示有更簡單的方法來確認你的功能工作,而不是編寫一個完整的程序,然後發現它已經壞了。

#include <algorithm> // for transform, for_each 
#include <cctype>  // for toupper 
#include <string> 
#include <vector>  // easier to use (correctly) than bare arrays 

// change a string to upper case 
void str_toupper(std::string &s) { 
    std::transform(s.begin(), s.end(), 
        s.begin(), 
        [](char c) -> char { return std::toupper(c); }); 
} 
// change a vector of strings to upper case 
void vec_toupper(std::vector<std::string>& v) { 
    std::for_each(v.begin(), v.end(), str_toupper); 
} 

using namespace std; 
int main() { 
    vector<string> const input = { "bob", "alice cooper", "Eve" }; 
    vector<string> const expected = { "BOB", "ALICE COOPER", "EVE" }; 

    vector<string> working = input; 
    vec_toupper(working); 
    return working == expected ? 0 : -1; 
    // use cout or debugger to solve problem only if program returns nonzero 
} 
+0

Lambda並非真正需要進行轉換工作。 – imbtfab

+0

gcc 4.8.2在沒有幫助的情況下找不到有效的重載...我可以用'(int(*)(int))std :: toupper'替換它,但是我認爲這比讀取更難以閱讀拉姆達。 – Useless

+0

只需使用:: toupper而不是std :: toupper,那麼不需要強制轉換。在場景中也有一個版本,或許這就是它與演員解決混合的那個版本。 這裏有一個簡短的討論:http://stackoverflow.com/a/7131881/3332992 – imbtfab