#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樣式轉換或函數樣式轉換
您是不是指'string80 name;'而不是'char string80,name;'? PS你可以使用'std :: string'和'std :: reverse'標準函數。 –
也'cin >>名字,81;'? – billz
另外,'cin >> name,81;'失敗,你的意思是'cin >> setw(81)>> name;'' –