c++ program trouble with SWAP function?

/**************************************************************************************************
*
* File name :lab11_driver.cpp
*
* This program is going to take two strings (one using the c-strin class and one using a string class
* and compare the two outputing if equal, larger and so on. Afterwards the program is going to take the first
* string that was inputted and in a function reverse its contents.
*
* Programmer :Aaron Sunshine
*
* Date Written :11 - 24 - 2007
*
* Date Last Revised :11 - 24 - 2007
*
***************************************************************************************************/

#include <iostream>
#include <string>

using namespace std;

string reverse_str(string statement1,int size);
string swap1(string str, int cap);

int main()
{
char ch;
string str1;
string str2;

cout << " \t We are going to compare two STRINGS by using the string class format" << endl;
cout << "\nPlease enter the first string we are comparing: " << endl;
getline(cin, str1);
int length = str1.size();
cout << "Please enter the second string we are comparing: " << endl;
getline(cin, str2); // includes spaces of use input

if (str1 == str2)
cout << "String one is equal to string two " << endl;
if (str1 > str2)
cout << "String one is greater than string two" << endl;
if (str1 < str2)
cout << "String two is greater than string one" << endl;

char c_str1[20]; //declares a c-string of 20
char c_str2[20];

cout << "\n\t\t We are going to compare two C-STRINGS" << endl;

cout << "\nPlease enter your first C-STRING we are comparing: " << endl;
cin >> c_str1;

cout << "Please enter your second C-STRING we are comparing: " << endl;
cin >> c_str2;

strcmp(c_str1, c_str2);

if (strcmp(c_str1, c_str2) == 0)
cout << "C-STRING 1 is equal to C-STRING 2." << endl;
if (strcmp(c_str1,c_str2) > 0)
cout << "C-STRING 1 is greater than C-STRING 2" << endl;
if (strcmp(c_str1,c_str2) < 0)
cout << "C-STRING 2 is greater than C-STRING 1" << endl;

string reverse = reverse_str (str1,length);
cout << "\n\nSring one reverse is: " << reverse << endl;

cin.ignore();
cout << "Please press enter to continue" << endl;
ch = getchar();

return 0;
}
/**************************************************************************************************
*
* Function name :reverse_str
*
*
* This functions intended to take a string wich is entered in the function main and reverse it
*
*
* Input: str1
*
* Output: none
*
* Return value: str1 reversed
*
***************************************************************************************************/

string reverse_str (string statement1, int size)
{

int x = 0;
int y = size;



string reversal = swap1(statement1.at(x),statement1.at(y));
x++;
y--;

return statement1;
}


string swap1(string str, int cap)
{
int i = 0;
int j = cap;

while (i < j)
{

swap (str.at(i),str.at(j));
}
return str;
}

**THE MOST TIME CONSUMING PART IN THIS PROGRAM IS WHERE THE SWAP FUNCTION IS USED I CANT SEEM TO GET THIS UNDER MY BELT...FOR SOME REASON MY TEACHER IS MAKING US USE THIS SWAP FUNCTION AND I HAVE NO IDEA WHY THIS WONT WORK***
anybody willing to help
[3920 byte] By [sunshine1] at [2007-12-5 11:46:41]
# 1 Re: c++ program trouble with SWAP function?
Can you please enclose the code in code here tags.

Also.. try not to appear like you're yelling at us.. and ask proper questions.

Thanks,

chem
chemicalNova at 2007-12-5 14:47:17 >
# 2 Re: c++ program trouble with SWAP function?
I apologize for it to seem i was yelling...Im not sure i understand by putting the tags in, I'm pretty new t c++, any information helps me further my education though.

#include <iostream>
#include <string>

using namespace std;

string reverse_str(string statement1,int size);

int main()
{
char ch;
string str1;
string str2;

cout << " \t We are going to compare two STRINGS by using the string class format" << endl;
cout << "\nPlease enter the first string we are comparing: " << endl;
getline(cin, str1);
int length = str1.size();
cout << "Please enter the second string we are comparing: " << endl;
getline(cin, str2); // includes spaces of use input

if (str1 == str2)
cout << "String one is equal to string two " << endl;
if (str1 > str2)
cout << "String one is greater than string two" << endl;
if (str1 < str2)
cout << "String two is greater than string one" << endl;

char c_str1[20]; //declares a c-string of 20
char c_str2[20];

cout << "\n\t\t We are going to compare two C-STRINGS" << endl;

cout << "\nPlease enter your first C-STRING we are comparing: " << endl;
cin >> c_str1;

cout << "Please enter your second C-STRING we are comparing: " << endl;
cin >> c_str2;

strcmp(c_str1, c_str2);

if (strcmp(c_str1, c_str2) == 0)
cout << "C-STRING 1 is equal to C-STRING 2." << endl;
if (strcmp(c_str1,c_str2) > 0)
cout << "C-STRING 1 is greater than C-STRING 2" << endl;
if (strcmp(c_str1,c_str2) < 0)
cout << "C-STRING 2 is greater than C-STRING 1" << endl;

string reverse = reverse_str (str1,length);
cout << "\n\nSring one reverse is: " << reverse << endl;

cin.ignore();
cout << "Please press enter to continue" << endl;
ch = getchar();

return 0;
}

string reverse_str (string statement1, int size)
{

int i = 0;
int j = size;

while (i < j)
{

swap (statement1.at(i),statement1.at(j));
i++;
j--;
}
return statement1;

}
sunshine1 at 2007-12-5 14:48:17 >
# 3 Re: c++ program trouble with SWAP function?
Where my problem has been occuring is at the function where i need to have the first inputted string reversed, i know i have to find the length and make some sort of counter; however, now the assignment is to use a swap function...What my question is, ismy coding so far correct, and what am i don't wrong with the functions...I have a lot of trouble with functions.
sunshine1 at 2007-12-5 14:49:15 >
# 4 Re: c++ program trouble with SWAP function?
It looks fine to me.. are you getting errors? What is the exact issue?

chem
chemicalNova at 2007-12-5 14:50:20 >
# 5 Re: c++ program trouble with SWAP function?
when you actually compile the program it will ask you for string on string two and so on but once it need to enter the function the program just stops and crashes, what my professor told me was to actually make a swap function because c++ doesn't actually know what swap is. This is where im confused because there are no actual errors it just crashes.
sunshine1 at 2007-12-5 14:51:13 >
# 6 Re: c++ program trouble with SWAP function?
this is the error that just occured when you get passed the second c-string input:
Unhandled exception at 0x7c812a5b in Lab11.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012fbfc..
sunshine1 at 2007-12-5 14:52:23 >
# 7 Re: c++ program trouble with SWAP function?
when you actually compile the program it will ask you for string on string two and so on but once it need to enter the function the program just stops and crashes, what my professor told me was to actually make a swap function because c++ doesn't actually know what swap is. This is where im confused because there are no actual errors it just crashes.

There is a swap function defined in the standard library.. which I believe is defined in the string template.

Basically, when you call swap(), it calls the string's public member function.. so:

swap(string1, string2);

..gets turned into..

string1.swap(string2);

If you have to write your own.. perhaps something like this:

template<class T>
inline void Swap(T &a, T&b)
{
const T temp = a;
a = b;
b = temp;
}

string reverse_str (string statement1, int size)
{
int i = 0;
int j = size;

while (i < j)
{
Swap(statement1.at(i),statement1.at(j));
i++;
j--;
}
return statement1;
}

chem
chemicalNova at 2007-12-5 14:53:15 >
# 8 Re: c++ program trouble with SWAP function?
Hey thanks Chem, but just a last question the dummy variables used in the swap function come from where?
sunshine1 at 2007-12-5 14:54:18 >
# 9 Re: c++ program trouble with SWAP function?
Hey thanks Chem, but just a last question the dummy variables used in the swap function come from where?

You're passing the variables in by reference.. so when you enter the swap function, a = statement1.at(i) and b = statement1.at(j).

chem
chemicalNova at 2007-12-5 14:55:26 >
# 10 Re: c++ program trouble with SWAP function?
thank you very much, i dont have to worry about inline and class t because i have never actually seen those before.
sunshine1 at 2007-12-5 14:56:27 >
# 11 Re: c++ program trouble with SWAP function?
Well, for your benefit.. I will give you a basic rundown of them here :p

Inline.. basically means, the function call is replaced, inline with the rest of the code.. by the function at compile time. Hard to understand? Heres an example:

inline int Add(int one, int two)
{
return one + two;
}

int main()
{
int result = Add(45, 35);
}

..gets turned into..

int main()
{
int result = 45 + 35;
}

.. or, for another example

inline int MultiplyDivideAndSubtract(int Number1, int Number2, int Number3)
{
int mod = Number1 % Number2;
int result = ((Number1 * Number2) / Number3) - mod;

return result;
}

int main()
{
int result = MultiplyDivideAndSubtract(10, 40, 4);
}

Gets.. roughly turned into..

int main()
{
int result = ((10 * 40) / 4) - (10 % 40);
}

.. of course, its hard to know what the compiler will do.. but thats a basic example.

Templates are much more complex.. and way beyond the scope of a mere forum post. Basically, they allow for any type to be used.. see my swap function? That can swap any type..

char * string1 = "Hello";
char * string 2 = "World!";

Swap(string1, string2);

std::string string1 = "Hello";
std::string string2 = "World!";

Swap(string1, string2);

std::vector<int*> vec1;
std::vector<int*> vec2;

Swap(vec1, vec2);

class MyObject { /* ... */ };

MyObject obj1;
MyObject obj2;

Swap(obj1, obj2);

etc..

Note that, the above example demonstrates templates at their most basic use.

chem
chemicalNova at 2007-12-5 14:57:25 >
# 12 Re: c++ program trouble with SWAP function?
hahaha way over my scope of programming so far, but would there be a way of taking out the inline and temp and just use a and b as dummy variables? I mean i might just be confused but i would think it would just be easier to just make the swap function easier and then i would just call it in the main function before my reverse string?
sunshine1 at 2007-12-5 14:58:22 >
# 13 Re: c++ program trouble with SWAP function?
Yeah.. you can change the swap function to be something like this.. (what does .at() return? A char? I can't check as I'm at work at the moment):

void Swap(char * a, char * b)
{
const char temp = a;
a = b;
b = temp;
}

chem
chemicalNova at 2007-12-5 14:59:23 >
# 14 Re: c++ program trouble with SWAP function?
i believe it returns chars...I would call the swap function in the main function before the actual reverse function correct...And i don't have to delete n e thing from my reverse function now that we have the swap function? I'm sorry about all the questions but functions have been catching me up so bad lately.
sunshine1 at 2007-12-5 15:00:29 >
# 15 Re: c++ program trouble with SWAP function?
Now i am getting a very weird error from the swap function, it doesn't look like it like passing chars.

Lab11_driver.obj : error LNK2019: unresolved external symbol "void __cdecl swap(char,char)" (?swap@@YAXDD@Z) referenced in function "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl reverse_str(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int)" (?reverse_str@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@H@Z)
sunshine1 at 2007-12-5 15:01:32 >
# 16 Re: c++ program trouble with SWAP function?
Post your entire code..

I think you haven't provided a prototype for the function.. which should be above your main function..

void swap(char*, char*);

Either that.. or its trying to reference the swap in the std namespace.. in which case, remove "using namespace std;" and prefix all std functions/objects with "std::".

chem
chemicalNova at 2007-12-5 15:02:25 >
# 17 Re: c++ program trouble with SWAP function?
#include <iostream>
#include <string>

using namespace std;

string reverse_str(string statement1,int size);
void swap(string my_string1 , int my_arraysize);

int main()
{
char ch;
string str1;
string str2;

cout << " \t We are going to compare two STRINGS by using the string class format" << endl;
cout << "\nPlease enter the first string we are comparing: " << endl;
getline(cin, str1);
int length = str1.size();
cout << "Please enter the second string we are comparing: " << endl;
getline(cin, str2); // includes spaces of use input

if (str1 == str2)
cout << "String one is equal to string two " << endl;
if (str1 > str2)
cout << "String one is greater than string two" << endl;
if (str1 < str2)
cout << "String two is greater than string one" << endl;

char c_str1[20]; //declares a c-string of 20
char c_str2[20];

cout << "\n\t\t We are going to compare two C-STRINGS" << endl;

cout << "\nPlease enter your first C-STRING we are comparing: " << endl;
cin >> c_str1;

cout << "Please enter your second C-STRING we are comparing: " << endl;
cin >> c_str2;

strcmp(c_str1, c_str2);

if (strcmp(c_str1, c_str2) == 0)
cout << "C-STRING 1 is equal to C-STRING 2." << endl;
if (strcmp(c_str1,c_str2) > 0)
cout << "C-STRING 1 is greater than C-STRING 2" << endl;
if (strcmp(c_str1,c_str2) < 0)
cout << "C-STRING 2 is greater than C-STRING 1" << endl;

string reverse = reverse_str (str1,length);
cout << "\n\nSring one reverse is: " << reverse << endl;

cin.ignore();
cout << "Please press enter to continue" << endl;
ch = getchar();

return 0;
}

void swap(string my_string1, int my_arraysize)
{
my_string1.swap(my_string1.at(my_arraysize)
}

string reverse_str (string statement1, int size)
{

int i = 0;
int j = size;

while (i < j)
{
swap(statement1.at(i),statement1.at(j));
i++;
j--;
}

return statement1;

}
sunshine1 at 2007-12-5 15:03:32 >
# 18 Re: c++ program trouble with SWAP function?
I might have changed a little because i have been trying a million different ways to get this work...I can't believe i am having so much trouble with a little program i have been working on it for over 10hrs...i don't know what to do anymore, it is supposed to be simple using a swap and another function that reverses the first string input i cant get to in depth, just need to use for loops and swap and so on...
sunshine1 at 2007-12-5 15:04:36 >
# 19 Re: c++ program trouble with SWAP function?
Firstly.. this.. is all wrong:

void swap(string my_string1, int my_arraysize)
{
my_string1.swap(my_string1.at(my_arraysize)
}

You're missing a closing bracket for starters.. ".swap(mystring1.at(my_arraysize))".

Secondly.. in the call to swap in your "reverse_str" function, you're passing it two things to swap.. but, you're not swapping the second thing. See what I mean? Your function declaration has "int my_arraysize", but when you call it you pass that parameter "statement1.at(j)", which we've concluded is a char.

Do you have to use a standard string? This would be much easier..

void swap(char * str1, char * str2)
{
const char * temp = *str1;
*str1 = *str2;
*str2 = *temp;
}

char * reverse_str(char * str)
{
char * start = str;
char * end = str;
end += length;

do swap(*start++, *end--);
while (start < end);
}

chem
chemicalNova at 2007-12-5 15:05:34 >
# 20 Re: c++ program trouble with SWAP function?
I cant use anything else...my objective us

Declare two strings str1 and str2 to be initialized by user input.
Compare str1 and str2, print the results ( are they equal , which string is greater, etc ? )
clearly indicate this is using string class methods
Declare two c-strings c_str1 and c_str2 ( size is 20 ) to be initialized by user input.
Compare c_str1 and c_str2, print the results ( are they equal , which string is greater, etc ? )
- clearly indicate this is using c-string functions
Write a function called reverse_str which will reverse the entered string str1. For example, If the user
enters for str1, the string
How are you
reverse_str will output
uoy era woH
This function will operate on the string data type
sunshine1 at 2007-12-5 15:06:30 >
# 21 Re: c++ program trouble with SWAP function?
Well.. to reverse the std::string.. you need to access its members anyway, which is a char pointer.

string.data and string.c_str both give information about the underlying string.. for example:

std::string myString1 = "Hello World!";

std::cout << myString1 << " is the same as " << myString1.c_str() << std::endl;

chem
chemicalNova at 2007-12-5 15:07:36 >
# 22 Re: c++ program trouble with SWAP function?
well i mean isnt there a way to just start at the beginning of the string increment that and find the length of the string and decrement that while swapping each position?
sunshine1 at 2007-12-5 15:08:34 >
# 23 Re: c++ program trouble with SWAP function?
Yes.. of course.. I'm not sure if this works.. all of the code up until now I've been typing off the top of my head at work..

This might be what you're looking for..

void swapIts(std::string::iterator str1, std::string::iterator str2)
{
std::string temp = *str1;
*str1 = *str2;
*str2 = temp;
}

void reverse_str(std::string str1)
{
std::string::iterator start = str1.begin();
std::string::iterator end = str1.end();
end--;
do swapIts(start++, end--);
while (start < end);
}

..give that a shot. If it doesn't work.. wait another 4 hours, I'll be home.. and I'll write something up for you.

chem
chemicalNova at 2007-12-5 15:09:34 >
# 24 Re: c++ program trouble with SWAP function?
haha 4hrs here will be almost 3am...i used iteration and .begin but he wants its even smaller for beginners he literally wants to use for loops a swap function nothing to in depth very basic
sunshine1 at 2007-12-5 15:10:34 >
# 25 Re: c++ program trouble with SWAP function?
Well.. the swap function is there.. the only thing it uses that you've stated differently is a while loop instead of a for loop..

chem
chemicalNova at 2007-12-5 15:11:38 >
# 26 Re: c++ program trouble with SWAP function?
Theres also something like this.. without a swap function..

std::string str = "Hello World!";
std::string reversed;
std::string::iterator it;
for ( it=str.begin() ; it < str.end(); it++ )
reversed += *it;

Then.. the even easier method:

std::string str = "Hello World!";
std::reverse(str.begin(), str.end());

chem
chemicalNova at 2007-12-5 15:12:38 >
# 27 Re: c++ program trouble with SWAP function?
Figured it out in very basic programming:

void swap(string my_string1, string my_string2)
{
string tmp = my_string1;
my_string1 = my_string2;
my_string2 = tmp;
}


string reverse_str (string& statement1)
{

int start = 0;
int end = statement1.size(); //get length of statement1
string tmp = statement1;

while (start < end)
{
end--;
swap(tmp[start], tmp[end]); // swap the begining of statement1 and replace with the end of statement 1
start++;


}

return tmp;
}

THANK YOU FOR ALL YOUR HELP, dont worry ill be back haha
sunshine1 at 2007-12-5 15:13:38 >
# 28 Re: c++ program trouble with SWAP function?
No probs :p

chem
chemicalNova at 2007-12-5 15:14:43 >