14

I am having a problem with the program I am trying to code. It's just a Windows console program and I am very new to C++. It's only my 4th program.

The problem I am having is that when I run my program I have no errors but a lot of warnings that say "comparison with string literal results in unspecified behaviour" in the lines that I will highlight below.

When the program runs instead of adding the numbers I want it to it just gives me a random huge number no matter what I put in for my inputs.

Here is the code:

#include <iostream>

using namespace std;

int main()
{
     int hold;
     int i;
     int n;
     i = 6;
     int result;
     int * price;
     char items[100][100];

     if (items == 0)
        cout << "No items can be stored";
    else
    {
        for (n=0; n<i; n++)
        {
            cout << "Item#" << n << ": ";
            cin >> items[n];
        }
        cout <<  "\nYou Entered: \n";
        for (n=0; n<i; n++)
            cout << items[n] << ", ";

    }
    for (n=0; n<i; 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<i; n++)
    {
    result = result + price[n];
    }

    cout << "\nTotal gold for this build: " << result;
    cin >> hold;
    return 0;
}

Any help is appreciated. There is probably something big that I've done wrong. The names in the if statements are all currently placeholders and I'll be adding a lot more if statements when I can get it to work with the bare 6 which is what it needs to work.

Bart
  • 19,692
  • 7
  • 68
  • 77
user1742497
  • 145
  • 1
  • 1
  • 4
  • 1
    `if (items == 0)` that doesn't make any sense. You're not dynamically allocating `items`, it is a stack variable. That comparison will never be true. – Praetorian Oct 12 '12 at 23:10

4 Answers4

24

In C++ == only implemented internally for primitive types and array is not a primitive type, so comparing char[100] and string literal will only compare them as 2 char* or better to say as 2 pointers and since this 2 pointers can't be equal then items[n] == "ae" can never be true, instead of this you should either use std::string to hold string as:

std::string items[100];
// initialize items
if( items[n] == "ae" ) ...

or you should use strcmp to compare strings, but remeber strcmp return 0 for equal strings, so your code will be as:

char items[100][100];
// initialize items
if( strcmp(items[n], "ae") == 0 ) ...

And one extra note is if (items == 0) is useless, since items allocated on stack and not in the heap!

BigBoss
  • 6,904
  • 2
  • 23
  • 38
  • Thankyou very much I've managed to get the code to work but it still outputs the wrong result at the end and I cant figure out why. – user1742497 Oct 13 '12 at 14:27
7

First, int * price; is a dangling pointer - you never initialize it. You have to do:

int * price = new int[i];

Second, usually, i denotes an iterator index so I suggest you stick with that - so

for (i=0; i<n; i++) //some more refactoring needed

Third, you need to compare char arrays using strncmp in your case.

Fourth and most important - use std::string and std::vector instead. This is C++, not C.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
3

Just a little thing that got me stumbling for a bit, is the difference between single and double quotes, see: Single quotes vs. double quotes in C or C++

I was comparing the first character of a string with double quotes and not single quotes - which resulted in above's error message.

Community
  • 1
  • 1
alex
  • 1,277
  • 3
  • 14
  • 30
1

You're comparing pointers, not the actual strings. Use C++ string class instead of char* (or check how C strings work).

m0skit0
  • 25,268
  • 11
  • 79
  • 127