2014-07-14 19 views
0
#include <iostream> 
#include <string> 
#include <iomanip> 

using namespace std; 

typedef char string80[81];  // create a synonym for another type 
void reverseString(string80);  // function prototype 
int main() 
{    
    // start program compilation here 
    char string80, name;   // variable to contain the name of the user 

    cout << "Enter your name =====> " ; 
    cin >> name,81; 

    cout << "\n\nWelcome to Computer Science 1106 " << name << endl<< endl; 

    reverseString(name); 

    cout << "Your name spelled backwards is " << name << endl << endl; 

    return 0; 
} // end function main 
    // Function to reverse a string 
    // Pre: A string of size <= 80 Post: String is reversed 

void reverseString(string80 x) 
{ 
    int last = strlen(x)- 1; // location of last character in the string 
    int first = 0;  // location of first character in the string 
    char temp; 

    // need a temporary variable 
    while(first <= last) 
    { // continue until last > first 

    temp = x[first];  // Exchange the first and last characters 
    x[first] = x[last];   
    x[last] = temp; 
    first++;  // Move on to the next character in the string 
    last--;    // Decrement to the next to last character in the string 
    }// end while 
}// end reverseString 

我得到一個錯誤爲什麼我得到錯誤錯誤C2664: 'reverseString'

C2664: 'reverseString':無法從 '字符' 轉換參數1至 '的char []' 轉換,從整型到指針類型需要 的reinterpret_cast,C樣式轉換或函數樣式轉換

+4

您是不是指'string80 name;'而不是'char string80,name;'? PS你可以使用'std :: string'和'std :: reverse'標準函數。 –

+2

也'cin >>名字,81;'? – billz

+2

另外,'cin >> name,81;'失敗,你的意思是'cin >> setw(81)>> name;'' –

回答

1

reverseString函數接受char [81]爲x參數尚未你發送一個char當你叫它。

什麼你可能想要做的是申報string80name作爲char [81]而不是char

char string80[81], name[81]; 
0

您聲明namechar型的,當它應該輸入爲string80(從你的typedef) 。

您還通過聲明char string80意外隱藏了您的typedef,它隱藏了來自周圍範圍的typedef。

你想聲明name類型string80,而不是char類型。類似這樣的:

string80 name;