Abel Zhukovskiy
Abel Zhukovskiy

Reputation: 21

Why do i get error error C2664: 'reverseString'

#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 

I get an error

C2664: 'reverseString' : cannot convert parameter 1 from 'char' to 'char []' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

Upvotes: 0

Views: 101

Answers (2)

Thomas W
Thomas W

Reputation: 14154

You are declaring name as of type char, when it should be typed as string80 (from your typedef).

You are also accidentally hiding your typedef by declaring char string80, which hides the typedef from the surrounding scope.

You want to declare name as of type string80, not of type char. Something like this:

string80 name; 

Upvotes: 0

Caesar
Caesar

Reputation: 9853

The reverseString function accepts char [81] for the x parameter yet you are sending it a char when you are calling it.

What you probably wanted to do was declare string80 and name as a char [81] and not a char

char string80[81], name[81]; 

Upvotes: 1

Related Questions