0

ok so this is what i have but I am getting an error says 'complexReverseString': identifier not found??? where am I going wrong I looked it over to no avail

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

int main()
{
    ifstream input;
    string forward;

    cout << "Please enter a string to see its reverse. (Will display reverse twice)" << endl;
    input.open("information.txt");
    cin >> forward;
    //reverseString(forward, findStringSize(forward)); a function I'm not actually calling
    input >> forward;
    complexReverseString();

    input.close();


}
int complexReverseString(string userInput)
{
    string source(userInput);
    string target( source.rbegin(), source.rend() );
    cout << "The reversed string is " << target << endl;

    return 0;
}
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
Kc North
  • 31
  • 1
  • 10
  • 2
    Please tag your question with **the language** this is about. – deceze Apr 23 '12 at 02:40
  • 1
    If you cannot see the loop, it doesn't mean there isn't one. –  Apr 23 '12 at 03:11
  • 1
    @KcNorth: Understanding compilation errors is a vital part of learning any compiled language. Looking at the rest of the questions you have posted today, it is definitely time for you to read [a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). The StackOverflow community cannot teach you a language or how to program, that is not how this site works, sorry! – johnsyweb Apr 23 '12 at 11:12

4 Answers4

4

You have to prototype complexReverseString() first before you call it.

Either do it in the source file before the main entry-point like this:

int complexReverseString(string userInput);
int main() { ... }

or do it in a separate header:

#ifndef COMPLEX_STR_HPP
#define COMPLEX_STR_HPP

int complexReverseString(string userInput);

#endif /* COMPLEX_STR_HPP */

/* int main.cpp */
#include "complex_str.hpp"

int main() { ... }

Secondly, you forgot to pass a parameter to the function:

 complexReverseString( /* parameter here */ );

 e.g
 complexReverseString(forward);

Thirdly, since you did not change the string in complexReverseString() I suggest taking a const string, and perhaps a reference to the string:

int complexReverseString(const string& userInput);
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
2

you need to pass the parameter to the method? as your method expects a parameter of type string else you need to overload it .. It is currently failing cos it is looking for a function definition complexReverseString()

change your code to

complexReverseString(forward);
Baz1nga
  • 15,485
  • 3
  • 35
  • 61
2

The simplest way to reverse a string or any other container without an explicit loop in C++ is to use reverse iterators:

string input;
cin >> input;
// Here is the "magic" line:
// the pair of iterators rbegin/rend represent a reversed string
string output(input.rbegin(), input.rend());
cout << output << endl;
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

In C/C++, the compiler reads the source code from top to bottom. If you use a method before the compiler reads it (ex. when the method is defined on the bottom, and you're using it in main() and main() is at the top), the compiler will not run through all source/header files in your project/folder until it finds the method or have gone through all files.

What you need to do is either prototype your method, or define it at the top.

A function prototype is basically a method header, used to inform the compiler that the method will be found somewhere else in the source.

Prototype:

#include <iostream>    
#include <fstream>    
#include <string>

using namespace std;

// DEFINE PROTOTYPES BEFORE IT'S CALLED (before main())
int complexReverseString(string);

int main()    
{    
    // your main code
}    

int complexReverseString(string userInput)    
{    
    // your reverse string code
}

Prototyping will allow you to see the methods and method headers more easily.

Or define compledReverseString() at top:

#include <iostream>    
#include <fstream>    
#include <string>

using namespace std;

// DEFINE METHOD BEFORE IT'S CALLED (before main())
int complexReverseString(string userInput)    
{    
    // your reverse string code
}

int main()    
{    
    // your main code
}

Either one of these styles will yield the same results, so it's up to you to decide which one you want to use.

Aside from this, when you called complexReverseString() in main, you forgot to pass its parameters, which should be forward. So instead of complexReverseString(), it's complexReverseString(forward)

Alex
  • 3,111
  • 6
  • 27
  • 43
  • sweet didnt know the post went this far down thank you for all of your speedy responces this sight is the best I appreciate all the help – Kc North Apr 23 '12 at 03:38