Backward String

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1053 Views - Last Post: 05 March 2013 - 07:37 PM Rate Topic: -----

#1 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Backward String

Posted 03 March 2013 - 05:04 PM

So. I just don't know. I can't get it to even run. Every time I try tweaking this program, I just get more issues. Here are my error codes as is:

1>Backward_String_N.obj : error LNK2001: unresolved external symbol "void __cdecl makeBackward(char *)" (?makeBackward@@YAXPAD@Z)
1>c:\users\owner\documents\visual studio 2010\Projects\Backward_String_N\Debug\Backward_String_N.exe : fatal error LNK1120: 1 unresolved externals


And here is my program:

// Backward_String_N.cpp : Defines the entry point for the console application.

// This program belongs to NM.
#include "stdafx.h"
#include <iostream>
using namespace std;

// Function prototype.
void makeBackward (char *);

int main()
{
	const int size = 51;
	char word[size];
	char *wo;
	wo = &word[size];

	// Get string.
	cout << "Enter a word, name, or phrase: ";
	cin.getline(word, size);

	// Function call.
	makeBackward(wo);

	return 0;
}

// Function header.
void makeBakward(char *wo)
{
	int c;
	
	cout << "Here is what you entered, but backward: ";
	for (c = 0; c >= 0; c--)
	{
		cout << wo[c];
	}
}


Any help is very much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Backward String

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,224
  • Joined: 23-August 08

Re: Backward String

Posted 03 March 2013 - 05:23 PM

Double-check your spelling.
Was This Post Helpful? 2
  • +
  • -

#3 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Backward String

Posted 03 March 2013 - 10:45 PM

Ohhhhhhh my goodness. Thank you very much. Hahaha!
Now that I finally got it to run, I am getting a strange symbol as my output instead of my word coming out backward.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: Backward String

Posted 03 March 2013 - 10:57 PM

On line 16 you are pointing beyond the end of your array. Remember that if you make an array of N elements, you can only predictably access elements 0 to N-1.

On line 16 you are pointing beyond the end of your array. Remember that if you make an array of N elements, you can only predictably access elements 0 to N-1.
Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: Backward String

Posted 03 March 2013 - 11:42 PM

View Post4N63L-Z3R0, on 03 March 2013 - 07:04 PM, said:

So. I just don't know. I can't get it to even run. Every time I try tweaking this program, I just get more issues. Here are my error codes as is:

1>Backward_String_N.obj : error LNK2001: unresolved external symbol "void __cdecl makeBackward(char *)" (?makeBackward@@YAXPAD@Z)
1>c:\users\owner\documents\visual studio 2010\Projects\Backward_String_N\Debug\Backward_String_N.exe : fatal error LNK1120: 1 unresolved externals


And here is my program:

// Backward_String_N.cpp : Defines the entry point for the console application.

// This program belongs to NM.
#include "stdafx.h"
#include <iostream>
using namespace std;

// Function prototype.
void makeBackward (char *);

int main()
{
	const int size = 51;
	char word[size];
	char *wo;
	wo = &word[size];

	// Get string.
	cout << "Enter a word, name, or phrase: ";
	cin.getline(word, size);

	// Function call.
	makeBackward(wo);

	return 0;
}

// Function header.
void makeBakward(char *wo)
{
	int c;
	
	cout << "Here is what you entered, but backward: ";
	for (c = 0; c >= 0; c--)
	{
		cout << wo[c];
	}
}


Any help is very much appreciated.


It's often a good idea to start off with a little easier related problem ...

For example ... using a C++ string and take the length

#include < string >

void printReverse( const string& s )
{
   for( int i = (int)s.length()-1; i >= 0; --i )
      cout << s[i];
}

// in main ...

string line;
cout << "Your prompt: " << flush;
getline( cin, line );
// now pass line to function ...



Was This Post Helpful? 1
  • +
  • -

#6 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Backward String

Posted 05 March 2013 - 08:29 AM

I was thinking about doing that too, finding the length number and such, but wasn't sure how to go about it. Seeing your code helped that make much more sense to me, thank you!

But I do have another question...
Everything works well and all, but the assignment is required for us to send a pointer of the string to the function. When I write this code without a pointer, it works just fine (again, thank you for that!) Though when I try to write it with a pointer and then write
for( int i = (int)s.length()-1;

this one piece of code, it constantly is telling me that my int needs a class type. I tweak with it a lot to try and fix it but it stays there.

It seems no matter how I write the pointer in there, as long as it's a pointer, it is telling me that it needs a class type.

(That snippet of code is from your example, not my actual program, but it's the same idea.)

This post has been edited by 4N63L-Z3R0: 05 March 2013 - 08:36 AM

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: Backward String

Posted 05 March 2013 - 08:36 AM

Show us your new code complete with the function definition.

If I were to guess, you were doing something like:
void PrintReverse(const char * s)
{
    for(int i = (int)s.length() - 1; i >= 0; i--)
        cout << s[i];
}


But I'm only guessing. It's better if you show us.
Was This Post Helpful? 0
  • +
  • -

#8 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Backward String

Posted 05 March 2013 - 08:39 AM

// Backward_String_N.cpp : Defines the entry point for the console application.

// This program belongs to NM.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

// Function prototype.
void printReverse (string *);

int main()
{
	string word;
	string *x;
	x = &word;
	
	// Get string.
	cout << "Enter a word, name, or phrase: ";
	getline(cin, word);

	// Function call.
	printReverse(x);

	return 0;
}

// Function header.
void printReverse(string *x)
{
	int c;

	cout << "Here is what you entered, but backward: ";
	for (c = x.length()-1; c >= 0; --c)
	{
		cout << x[c];
	}
	cout << "\n\n";
}

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: Backward String

Posted 05 March 2013 - 08:44 AM

You'll have to dereference the pointer when you use it.

For example:
struct Foo
{
    int Bar;
};

void PrintFooBar(Foo * pfoo)
{
    cout << pfoo->Bar;
}

int main()
{
    Foo foo = { 23 };
    PrintFoo(&foo);
    return 0;
}



Was This Post Helpful? 0
  • +
  • -

#10 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Backward String

Posted 05 March 2013 - 09:03 AM

I am having trouble understanding how to apply something like that to my program.
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Backward String

Posted 05 March 2013 - 09:12 AM

Change your function prototype.
// why the hell are you passing a pointer?
void printReverse (string *);
// instead, just pass a reference
void printReverse (const string &);



Looks like you really need to work on both pointers and references.
Was This Post Helpful? 1
  • +
  • -

#12 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Backward String

Posted 05 March 2013 - 09:50 AM

Yeah pointers are new to me so we are practicing with them. I tried using pointers because it is part of the assignment and I am still testing with them.

Thanks! I got wonderful help for this assignment. :bigsmile:

I just want to verify. Here is my final code:

// Backward_String_N.cpp : Defines the entry point for the console application.

// This program belongs to NM.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

// Function prototype.
void printReverse (const string &);

int main()
{
	string word;
	const string *x;
	x = &word;
	
	// Get string.
	cout << "Enter a word, name, or phrase: ";
	getline(cin, word);

	// Function call.
	printReverse(word); // I will admit, it would only work when I made the call to this. I had another program where this happened, too. Is this still correct? The program seems to run like normal the way it is.

	return 0;
}

// Function header.
void printReverse(const string &x)
{
	int c;

	cout << "Here is what you entered, but backward: ";
	for (c = x.length()-1; c >= 0; --c)
	{
		cout << x[c];
	}
	cout << "\n\n";
}

Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: Backward String

Posted 05 March 2013 - 10:01 AM

So with the code above, you have failed part of the assignment if the requirement was for you to pass a pointer to a string to your function. Above you are passing a reference to a string.

This post has been edited by Skydiver: 05 March 2013 - 10:01 AM

Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Backward String

Posted 05 March 2013 - 10:04 AM

	string word;
	// you use neither of these, kill them now
	// const string *x;
	// x = &word;
	
	// Get string.
	cout << "Enter a word, name, or phrase: ";
	getline(cin, word);

	// this is the correct way to call
	// in C++, the & on the protype is transparent here
	printReverse(word);

	return 0;
}



Now, if you really want those pointers, be consistent:
void printReverse (string *);

int main() {
	string word;
	
	cout << "Enter a word, name, or phrase: ";
	getline(cin, word);

	// you are passing the address, so the & here gets that for you
	// yes, it's confusing, the & in the prototype is for reference by
	// but here it's for address of
	printReverse(&word);

	return 0;
}

// Function header.
void printReverse(string *x) {
	cout << "Here is what you entered, but backward: ";
	for (int c = x->length()-1; c >= 0; --c) {
		// note the pointer -> above
		// or, if you like
		cout << (*x)[c];
	}
	cout << "\n\n";
}



For the char * option, there's some really neat recursive code for that. Keep that in mind when you cover recursion.
Was This Post Helpful? 2
  • +
  • -

#15 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: Backward String

Posted 05 March 2013 - 10:12 AM

Maybe your 'exercise' is to learn about the problems with using C type char strings and using cin.getline :)

And to ensure a large enough buffer ...

// your includes ...
#include <cstring> // re. strlen
// if you really need to pass a pointer ...
void print_reversed( const char* s )
{
   for( int i = strlen(s)-1; i >= 0; --i ) cout << s[i];
}

// in main 
char buffer[16];
for( ; ; )
{
   cout << "Enter string of 15 char's or less: " << flush;
   if( cin.getline( buffer, sizeof(buffer) ) )
   {
      if( strlen(buffer) == 0 ) return 0;
      cout << "Reversed: ";
      print_reversed( buffer );
      cout << endl;
   }
   else
   {
      cin.clear();
      cin.sync();
      cout << "Too large ..." << endl;
   }
}


This post has been edited by David W: 05 March 2013 - 10:15 AM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2