0

The program runs smoothly and I have no errors or warnings when its compiled its just when it gets the end result I just get a load of random letters and numbers no matter what I put in.

Here is the code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
     int hold;
     int n;
     int * result = new int;
     int * price = new int;
     std::string items[6];

        for (n=0; n<6; n++)
        {
            cout << "Item#" << n+1 << ": ";
            cin >> items[n];
        }
        cout <<  "\nYou Entered: ";
        for (int n=0; n<6; n++)
            cout << items[n] << ", ";

    for (n=0; n<6; n++)
    {
        if (items[n] == "ab"){
        price[n] = 2650;
        }

        else if (items[n] == "ae"){
        price[n] = 1925;
        }

        else if (items[n] == "ie"){
        price[n] = 3850;
        }

        else if (items[n] == "bt"){
        price[n] = 3000;
        }

        else if (items[n] == "pd"){
        price[n] = 2850;
        }

        else if (items[n] == "ga"){
        price[n] = 2600;
        }

    }

    for (n=0; n<6; n++)
    {
     result += price[n];
    }

    cout << "\nTotal gold for this build: " << result;
    cin >> hold;
    return 0;
}
pb2q
  • 58,613
  • 19
  • 146
  • 147
user1742497
  • 145
  • 1
  • 1
  • 4

5 Answers5

3
int * price = new int;

and

int * result = new int;

allocate a single int respectively. You probably meant new int[6].

But then again, you should be using std::vector instead.

I'm disappointed really that you took no advice from - https://stackoverflow.com/a/12868164/673730 - if you had, you wouldn't have this problem now. This is not a good way to learn.

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Okay I changed `int * result = new int;` to `int result;` and now I get a number as my result but its still the wrong number it doesnt add up any of the values I have entered in the if statements even if I've used "ae" 6 times for example. – user1742497 Oct 13 '12 at 15:18
  • @user1742497: The real culprit is `result += price[n]`. But there are many other flaws in this code, including those which Luchian already showed. – Zeta Oct 13 '12 at 15:19
1

With this declaration: int * price = new int; you only allocate space for a single int, but you go on to use price as an array of int.

To declare an array, use: int *price = new int[5];

As for result, you declare that as a pointer to int also, but you later use it as an int: result += price[n];. No need to result to be a pointer. Also note that you need to initialize your variables explicitly: set result to zero before you begin using it.

pb2q
  • 58,613
  • 19
  • 146
  • 147
1

just give some comments:

  1. new operater should be used with delete.
  2. "int *result" you declared is a point to int, so you should dereference this point to get the result you want.
  3. exceptions should be taken into consideration, what if the input letter is not in your given list?
sophos
  • 69
  • 2
0

Well, result is an int *. This kind of variable usually stores the address of another integer variable, which you get with new int in this specific case. However, with

 result += price[n];

you'll modify that address, which would lead to segmentation faults if you were to actually write/read from *result. This is also the reason why you output is strange:

cout << "\nTotal gold for this build: " << result;

This prints the adress stored in result, not the value. Make result an integer and it should work.

Please note that price should be changed too, see Luchian's answer.

Exercise

  1. Change your code so that there is no use of new.
  2. Your program could still fail. What is the initial value of result?
  3. What happens if the user provides a code which is not in your list?
Community
  • 1
  • 1
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • Thanks for the response I'm working on it I'm new to this stuff so I have to read through how to use something if I dont know what it is. – user1742497 Oct 13 '12 at 15:35
0

Change the line:

cout << "\nTotal gold for this build: " << result;

to

cout << "\nTotal gold for this build: " << *result;

Result is a pointer, so you need to dereference it, using the * operator;

Edit: Change the declaration of the price array to

int *price = new int[6];

The previous declaration declared a variable, not an array

Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58