Determining price via column and row number.

Hi friends, I just want to know how to make the variable 'price' deleted after its last run. I have tried the delete price; but the value of price seems to stuck at first trial result. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    char things [4][13]={{'A','B','C','D','E','F','G','H','I','J','K','L','M'},{'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'},
{'a','b','c','d','e','f','g','h','i','j','k','l','m'},{'n','o','p','q','r','s','t','u','v','w','x','y','z'}};
    char input[20];
    int i, j, k, l;
    int *price;
    price=new int [6];
    cout<<"Name of thing ? ";
    cin.getline(input,sizeof(input));
    for (i=0; i<=20; i++){
        for (k=0; k<=3; k++){
                for (l=0; l<=12; l++){
                    if(input[i]==things[k][l]){
                    *price=k+l+100;
                    }
            }
        }
        }
    cout<<*price;
    delete price;
    getch();
}
I believe it's supposed to be delete[]price;
I'm not sure what you're trying to do.

In your code price is a pointer to memory containing an array of 6 int, but you treat it as if it's a pointer to memory containing space for a single int.

You assign to *price multiple times without doing anything with the value (except overwriting it) after it's stored.

If you use new with [] you must use delete with [] as CJC0116 pointed out.
@cire @CJC0017 I want to do this thing: If I type BANANA, the variable price will show a particular score, assume it is X. Then after running the app, simply the value of variable price is deleted (return to blank or 0), so if we run the app for the next time the variable price will show another value, not X again. What I experience now is the value of price is equal to X after first run--not depending on the word typed on second run, third run, etc.
Last edited on
That doesn't have anything to do with price not being reset, but you not processing what is input correctly.

Let's see. BANANA. How many letters is that?

How many are you looping through?

The value of *price is very likely not being determined by anything you actually input.
If you want the program to repeat then you'll need to include an infinite loop that only ends if you type something like "done". Also, you shouldn't be using dynamic memory at all; it's unnecessary. Just make price an integer. Also, just write cin.getline(input,20); on line 15. and line 16 is wrong. What you need to do is something like for(i=0;i<strlen(input);i++). To use strlen, which returns the length of the array, include the cstring header file. Finally, correct me if I'm wrong, but the initial price is 100, and each letter in 'input' adds to that price, right? If so, you have to initialize price to 100 and add price to itself in the code. Here's a complete code. If you have any problems, ask for clarification:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <conio.h>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char things [4][13]={{'A','B','C','D','E','F','G','H','I','J','K','L','M'},{'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'},
{'a','b','c','d','e','f','g','h','i','j','k','l','m'},{'n','o','p','q','r','s','t','u','v','w','x','y','z'}};
    char input[20];
    int i, j, k, l;
    int price=100;
    while(true){
        cout<<"Name of thing ? ";
        cin.getline(input,20);
        if(strlen(input)==4&&input[0]=='d'&&input[1]=='o'&&input[2]=='n'&&input[3]=='e')
            break;
        else{
            for (i=0; i<strlen(input); i++){
                for (k=0; k<=3; k++){
                    for (l=0; l<=12; l++){
                        if(input[i]==things[k][l]){
                            price=price+k+l;
                        }
                    }
                }
            }
        }
        cout<<price<<endl;
        price=100;
    }
}
Last edited on
@CJC0117

:-D Thanks for your clear explanation! I've corrected it by your way. Also, for @cire my main intention is to make the value of price=0 in each running, so i think that the value of X is unimportant.

changes including:
int price =0; price+=k*l;

I still use getch() ; for convenience, to show my assistant how my program works. Still try to use cin.get();
Last edited on
Topic archived. No new replies allowed.