2016-04-29 10 views
0

私はこの問題に取り組んでおり、私はそれを解決するのにかなり近いと思います。私は文字列の最初の文字でtoupperを使って変換するのに問題があります。すべての文字を小文字に変換する方法を解明しようとしていたときに(私はこれをやる必要があると言っていました)、私はtolower(最初の[0])を使ってみました。小文字なので、最初の文字を大文字にするときにtoupper(最初の[0])を試してみると、なぜ動作しないのですか?C++:Pig Latinコンバータのヘルプ。 (toupperとstrings)

また、文字列の最初の文字を最後の場所に移動する方法はありますか?

ありがとうございます。ただ、簡単な失態

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

int main() 
{ 

    char ans; 

    do{ 

    string first, last; 
    char first_letter, first_letter2; 

    cout << "This program will convert your name " 
    << "into pig latin.\n"; 
    cout << "Enter your first name: \n"; 
    cin >> first; 
    cout << "Enter your last name: \n"; 
    cin >> last; 

    cout << "Your full name in pig latin is "; 

    for(int x = 0; x < first.length(); x++){ 
     first[x] = tolower(first[x]); 
    } 

    for(int x = 0; x < last.length(); x++){ 
     last[x] = tolower(last[x]); 
    } 

    first_letter = first[0]; 

    bool identify; 

    switch (first_letter) 
     { 
     case 'a': 
     case 'e': 
     case 'i': 
     case 'o': 
     case 'u': 
    identify = true; 
    break; 

     default: 
    identify = false; 
     } 

    if(identify == true){ 
     toupper(first[0]); 

     cout << first << "way" << " "; 
    } 

    first_letter2 = last[0]; 

    bool identify2; 

    switch (first_letter2) 
     { 
     case 'a': 
     case 'e': 
     case 'i': 
     case 'o': 
     case 'u': 
    identify2 = true; 
    break; 

     default: 
    identify2 = false; 
     } 

    if(identify2 == true){ 
     toupper(first[0]); 

     cout << last << "way" << endl; 
    } 

    cout << "You you like to try again? (Y/N)\n"; 
    cin >> ans; 

    } while(ans == 'y' || ans == 'Y'); 


    return 0; 

} 
+0

のputcharが欠落しているがintで最初の文字のみを変更する必要がある場合はBTW、次にforループを使用する理由は、Raphael Miedlが言及したように最初の[0]にする必要があります。 – piyushj

+0

ユーザーがすべて大文字手紙。プログラムの最終出力は、最初の文字のみが大文字になっているので、Oscarway Allenwayのように、豚ラテンの名前と姓でなければなりません。 – Bluasul

答えて

2

は、 '不足している明白なことを見ることができない' 症候群の

toupper(first[0]); 

通常の場合と

first[x] = tolower(first[x]); 

を比較...私は、これらのミスを憎みます。私は通常、単純なケースのためにsubstr()を使用したい最後に、最初の文字を移動するためとして

:)(tolowerを、TOUPPER()の戻り値として、

str = str.substr(1) + str[0]; 
+0

ありがとう! – Bluasul