2016-05-13 10 views
0

私はこの課題に関して、新しい学習者のためのコンピュータプログラミングコースからの助けが必要です。文字配列C++コードが壊れているので、修正する必要があります。それを除いて、私は長い間ずっとそれに執着していました。私はいくつかの援助をしたいと思います。もし誰かがこれを理解するのを助けることができたら、それは本当に素晴らしいだろう!ありがとうございました!ただ、にやにや笑いのためC++文字配列、末尾に文字を配置

#include"stdafx.h" 
 
#include<iostream> 
 
#include<string> 
 
using namespace std; 
 

 
int main() 
 
{ 
 
    string word[20]; 
 
    char inputword; 
 
    int x; 
 
    cout<<"Enter a word "; 
 
    cin>>inputword; 
 
    if(word[0] = 'a' || word[0] = 'e' || word[0] = 'i' || 
 
    word[0] = 'o' || word[0] = 'u') 
 
     cout<<"Words that start with vowels are not easily translated to Pig Latin"<<endl; 
 
    else 
 
    { 
 
    cout<<"In Pig Latin: "; 
 
    while(word[x] != NULL) 
 
    { 
 
     cout<<word[x]; 
 
     ++x++; 
 
    } 
 
    cout<<word[0]<<"ay"<<endl; 
 
    } 
 
}

+0

C++での等価性をテストするために '=='を使用すると、 '= 'は代入を意味します。 –

+0

1)何が間違っている、2)あなたはこれまで何をしていますか? – immibis

+0

多くの変更が必要です。 'if()'で 'word []'を使用しているときは、未定義です。さらに、 'word []'は 'char'型として使用しているときには' string'型です。 –

答えて

0

、私は仕事にあなたのコードを変更しました。お役に立てれば。

#include"stdafx.h" 

#include<iostream> 
// #include<string> // working w/char arrays, don't need std strings 
#include <cstring> // include this file so we can use strlen (c-style strings) 
using namespace std; 

int main() 
{ 
    char word[20]; // you're working with a char array (not string array) 
    // char inputword; // you don't need a separate input data structure 
    int x; 
    cout<<"Enter a word "; 
    cin>>word; // input directly into the word array 

    // use == to compare for equality, using = is assigning to word[0] 
    if(word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || 
    word[0] == 'o' || word[0] == 'u') 
     cout<<"Words that start with vowels are not easily translated to Pig Latin"<<endl; 
    else 
    { 
    cout<<"In Pig Latin: "; 

    // get the size of the word: 
    // x = strlen(word); 

    // work backwards through the array and print each char: 
    for(int i=strlen(word); i>=0; --i) 
    { 
     cout << word[i]; 
    } 
    cout << "ay" << endl; 

    // use the for loop shown above instead of this 
    // while(word[x] != NULL) 
    // { 
    // cout<<word[x]; 
    // ++x++; 
    // } 
    // cout<<word[0]<<"ay"<<endl; 
    } 
} 
+0

このコードを手伝ってくれてありがとう。私は#を聞いたことがなかったまたはstrlen前に! –

0

だから、最初の与えられたプログラムの間違いを識別:

  1. あなたはchar型の配列を必要とし、与えられたプログラムでは、配列が文字列型とではない別の変数として宣言されています配列は文字として宣言されます。だから、何が必要です:後者は比較しばらく使用され、===の間に違いがあり

    cin>>word; 
    
  2. です:入力を服用すると

    char word[20]; 
    
  3. 、あなたが宣言した文字列を使用する必要があります前者は割り当てに使用されます。したがって、abと同じ場合はif(a==b)となりますが、bの値をaに入れたい場合はa = bとなります。 if(a = b)が、abの両方が有効な変数である場合、常に真であることに注意することが重要です。したがって、あなたはにif文を変更する必要があります:

    if(word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u') 
    
  4. 最後に、出力する文字列は後方あなただけの最初に配列の最後の文字から印刷を開始する必要があります。

    n = strlen(word); 
    

    それからちょうどそこから逆方向に行く:

    while(n>=0) 
    { cout<<word[n]; 
        n--; 
    } 
    
    配列の最後のインデックスを取得するには、最後のインデックスと同じになり、文字列の長さを取得するためにstrlenを関数を使用します

    あなたの文字配列は逆順になります。

+0

私にコードを説明してくれてありがとう!これは多くの助けになりました! :D –

関連する問題