1

I'm having trouble with an example that I am compiling from my C++ All-In-One For Dummies, Second Edition. What it should do is display 10 lines of code containing CR(somenumbers)NQ; However every time I run I get 10 variable addresses. I tried to search the web for this issue but it's pretty particular. I'm running Linux openSUSE 12.1 and using Code::Blocks (GCC). I'm beginning to think there might be a library issue with the included append function. Either that or I'm completely blind and its really obvious.

#include <iostream>
#include <sstream>
#include <cstdlib>

using namespace std;


string *getSecertCode()
{
    string *code = new string;
    code->append("CR");
    int randomNumber = rand();
    ostringstream converter;
    converter << randomNumber;

        code->append(converter.str());
    code->append("NQ");

    return code;
}

int main()
{
    string *newcode;
    int index;
    for (index =0; index < 10; index++)
    {
        newcode = getSecertCode();
        cout << newcode << endl;
    }
    return 0;
}
Jonathan Spooner
  • 7,682
  • 2
  • 34
  • 41
Wylie Coyote SG.
  • 1,009
  • 6
  • 22
  • 37
  • Unless this was an example demonstrating bad C++ on purpose, consider [a different textbook](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Cubbi Dec 06 '11 at 21:15
  • It unfortunately was an bad textbook example. It also didn't include for use of rand(). The rest of the book has been relatively spot on though. But thanks for the suggestion. – Wylie Coyote SG. Dec 06 '11 at 21:37

4 Answers4

1

The problem seems to be

cout << newcode << endl;

since "newcode" is a string* and not a string.

Try

cout << *newcode << endl;

instead.

And as you say you are a beginner: don't forget to delete the memory you are allocating (new!).

cli_hlt
  • 7,072
  • 2
  • 26
  • 22
  • That fixed it! Looks like It was me just being blind. I looked over this code for at least 10 mins looking for a typo. Sorry I didnt catch that. So I'm new to stackoverflow. DO I remove this question now that its been resolved? (especially because it was my fault?) – Wylie Coyote SG. Dec 06 '11 at 21:35
  • No problem, these things happen. Don't remove the question, let others have the chance to find an answer if they have a similar problem. – cli_hlt Dec 06 '11 at 21:41
1

Avoid using pointers whenever you can. You don't need them here.

string getSecertCode()
{
  string code;
  code.append("CR");
  [...]

  code.append(converter.str());
  code.append("NQ");

  return code;
}

int main()
{
  string newcode;
  [...]
}
Draco Ater
  • 20,820
  • 8
  • 62
  • 86
0

the problem is your inderecting a pointer into cout.

change

cout << newcode << endl;

to

cout << *newcode << endl;

and you will see your values.

with pointers, if you want to look at the value of the pointer you have to dereference it as in

*pointer;

if you wish to look at the address of that value, just use the pointer.

johnathan
  • 2,315
  • 13
  • 20
0

Your main issue is probably:

cout << newcode << endl;

where you're printing the pointer (string*) instead of the string. You may print the string by dereferencing the variable:

cout << *newcode << endl;

Secondarily, you have a memory leak as your subfunction allocates strings that are never deleted.

Adam Holmberg
  • 7,245
  • 3
  • 30
  • 53