Bumper sticker problem

I have almost completed this question but I keep getting an error on line 4 in the .cpp. Any suggestions would be appreciated.

This is the question:
A bumper sticker company has decided to expand their business by creating bumper stickers for the front bumper. These bumper stickers would be designed to be read in the rear-view mirror of the car ahead. Therefore, the letters should be reversed (right-to-left) so their reflection in a mirror would be displayed left-to-right. For this project, you will create a program that will display a bumper sticker saying in reverse order.
Minimum Requirements:

You must include a class that has a string as a private data member (5 points).
You must include a member function to add a bumper sticker saying, such as: "Get Outta My Way"! (5 points)
Create another function to display all letters in the string in reverse order. This function must use a loop structure to accomplish this (5 points).
You will also need a client program that creates an object of your class and calls the function(s) you created to meet the requirements of this project (5 points).

Here is my work:

.h:
#ifndef BUMPER_H
#define BUMPER_H

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

class Bumper
{

private:
string s;
string result="";
int i;


public:
void displaystring()
{
cout << "Get Outta My Way"<<endl;
}

string reversestring(string s)
{
string result="";
for (int i=0;i<s.length();i++) {
result=s[i] + result;
}
return result;
}

};


#endif // BUMPER_H

.cpp:
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include "bumpersticker.h"
using namespace std;

int main()
{
Bumper B;
string mystring;
cout<<"Enter a string: ";
getline(cin,mystring);
mystring= B.reversestring(mystring);
cout<<"Reverse bumpersticker: "<<endl;
cout<<mystring<<endl;
cout<<"Display bumpersticker: "<<endl;
B.displaystring();

}

Thanks!

Your program compiles and works for me. Post the exact error. [edit] And the compiler you are using: VC++, MinGW/GCC, Borland?
Last edited on
4. C:\Users\dane\Desktop\C++\Golf\Bumpersticker.cpp - In file included from C:\Users\dane\Desktop\C++\Golf\Bumpersticker.cpp

Well at least I know it works lol. Thats the error I get above, I'm using Dev C++
That compiler is too old.

1
2
3
4
5
class Bumper
{
  ...

  string result="";  // It is complaining about this line 

A std::string will automatically initialize to the empty string. Change the line to

      string result;                                          

and it will work for you.

Hope this helps.
Worked like a charm. Thanks for the help!
Topic archived. No new replies allowed.