Really having a problem

n0n4m3
Unregistered
 
Post: #1
Code:
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

//Alla strukturer
struct sPlayer{
    int iMoney;
    int iHp;
    int iExp;
    int iAttack;
    int iWeapon_strenght;
    char cPlayer_weapon[];
};

//Alla funktioner
int fMenu(sPlayer& player);
int fShop(sPlayer& player);
int fBuy_weapon(char cWeapon_sale[], int iBuy_price[], int iChoice, int i, sPlayer& player);

int main()
{
    sPlayer player;
    player.iMoney = 200;
    player.iHp = 100;
    player.iExp = 0;
    player.iAttack = 10;
    player.iWeapon_strenght = 0;

    //GÃ¥r till menyn och stannar kvar där tills spelet avslutas
    fMenu(player);
    return 0;
}

//Huvudmenyn
int fMenu(sPlayer& player)
{
    int iChoice;
    
    do
    {
        //Huvudmenyn
        system("CLS");
        cout << "1. Forrest" << endl;
        cout << "2. Shop" << endl;
        cout << "3. Exit" << endl;
        cin >> iChoice;
        
        //Ditt val
        switch (iChoice)
        {
            case 1:
                break;
            
            case 2:
                fShop(player);
                break;
            
            case 3:
                break;
            
            default:
                cout << "Try again" << endl;
                break;
        }
    }while(iChoice != 3);  
}

//Shop
int fShop(sPlayer& player)
{
    char cWeapon_sale[4][30] = {"Knife", "Sword", "Bow", "Axe"};
    int iWeapon_strenght[4] = {10, 18, 28, 42};
    int iBuy_price[4] = {80, 100, 160, 220};
    int iChoice;
    int i;
    
    do
    {
        //Vapen-meny
        system("CLS");
        cout << "What weapon do you want to look at?" << endl;
        cout << "1. " << cWeapon_sale[0] << endl;
        cout << "2. " << cWeapon_sale[1] << endl;
        cout << "3. " << cWeapon_sale[2] << endl;
        cout << "4. " << cWeapon_sale[3] << endl;
        cout << "5. Leave shop" << endl;
        cin >> i;
        i -= 1;
        
        if(i >= 0 && i <= 4)
        {
            system("CLS");
            cout << "Weapon: " << cWeapon_sale[i] << endl;
            cout << "Strenght: " << iWeapon_strenght[i] << endl;
            cout << "Price: " << iBuy_price[i] << endl;
            cout << endl;
            cout << "1. Buy weapon" << endl;
            cout << "2. Sell weapon" << endl;
            cout << "3. View another weapon" << endl;
            cin >> iChoice;
            
            //Hanterar ditt val
            switch (iChoice)
            {
                case 1:
                    fBuy_weapon(cWeapon_sale[i], iBuy_price, iChoice, i, player);
                    break;
                
                case 2:
                    break;
                
                case 3:
                    break;
                    
                default:
                    cout << "Try again" << endl;
                    break;
            }
        }
        else if(i == 5)
        {
            return 0;
        }
        else
        {
            cout << "Try again" << endl;
            getch();
        }
    }while(i != 5);
}


//Köper weapon
int fBuy_weapon(char cWeapon_sale[], int iBuy_price[], int iChoice, int i, sPlayer& player)
{
    //Kollar om jag har pengar och att jag inte redan har ett likadant vapen
[b]    if(player.iMoney >= iBuy_price[i] && strcmp(cWeapon_sale[i], player.cPlayer_weapon)!=0)[/b]
    {
[b]        player.cPlayer_weapon = cWeapon_sale[i];[/b]
        player.iMoney -= iBuy_price[i];
        cout << "You bought " << cWeapon_sale[i] << " for " << iBuy_price[i] << "$" << endl;
    }
    else
    {
        cout << "You can't buy that weapon" << endl;
    }
}


i am really having problems with this code. im pretty new att c++ so i really need help. dont care aboute the comments, its swedish. if someone could compile this code and see the errors. i get like 5 errors.
please!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #2
It would be easier if you just post what the errors are.
Quote this message in a reply
n0n4m3
Unregistered
 
Post: #3
Code:
139 C:\Documents and Settings\Andreas\Mina dokument\Programmering\Egna grejer\Adventure\Game.cpp invalid conversion from `char' to `const char*'
139 C:\Documents and Settings\Andreas\Mina dokument\Programmering\Egna grejer\Adventure\Game.cpp   initializing argument 1 of `int strcmp(const char*, const char*)'
141 C:\Documents and Settings\Andreas\Mina dokument\Programmering\Egna grejer\Adventure\Game.cpp incompatible types in assignment of `char' to `char[0u]'
149 C:\Documents and Settings\Andreas\Mina dokument\Programmering\Egna grejer\Adventure\Game.cpp invalid conversion from `char*' to `int'

i made those lines bold
Quote this message in a reply
Member
Posts: 168
Joined: 2004.10
Post: #4
Are you using MingW with Dev-C++, or Visual-C++?

People here mostly use GCC, so MingW is the recommended choice.

From the errors, it seems that you are using variables that are supposed to be of one type for a certain argument, whilst you are using another.

My C's a bit rusty, so I might be wrong but I think that && means that the argument afterwards is supposed to be the true value of a boolean.
Quote this message in a reply
Moderator
Posts: 529
Joined: 2003.03
Post: #5
Hey! I don't have a CAnnoyed drive on my Macintosh. I'm envious.

"Yes, well, that's the sort of blinkered, Philistine pig-ignorance I've come to expect from you non-creative garbage."
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #6
You're having conversion errors. Sadly, it isn't just a two-line fix.

The first problem is that your call to strcmp is botched.
strcmp(cWeapon_sale[i], player.cPlayer_weapon)
If you check out cWeapon_sale, you'll notice that it is a char array. sale[i] is therefore a char. strcmp takes a const char *, which isn't a char. That is, you don't need to index into the array. strcmp (cWeapon_sale, player.cPlayer_weapon) will be a better test. Depending on your compiler settings, you may need to cast it to a const char*, if so just add (const char *) after the first parenthesis.

Next up, you're doing rougly the same error again - you're trying to assign a char to a char*. What you want to do is to copy the string, not just one character. So the first step to take is to go
player.cPlayer_weapon = cWeapon_sale;
...for the same reasons as above. Now, depending on what you want to do, this might or might not be good, since you're not copying the string. In this particular case, it might work and not crash, but I would not take any chances - copy the string with strcpy. Change that line to:
strcpy (player.cPlayer_weapon, cWeapon_sale);

Finally, you're not returning anything from that function...

Let me give you a tip - write a little code, and then compile. I mean, compile every two lines of code. I've done this for upwards of ten years, I still compile every five lines of code. Smile

Lycka till! Wink
Quote this message in a reply
n0n4m3
Unregistered
 
Post: #7
Tack=)

but now it look like this:

Code:
//Köper weapon
int fBuy_weapon(char cWeapon_sale[], int iBuy_price[], int iChoice, int i, sPlayer& player)
{
    //Kollar om jag har pengar och att jag inte redan har ett likadant vapen
    if(player.iMoney >= iBuy_price[i] && strcmp(cWeapon_sale, player.cPlayer_weapon)!=0)
    {
        player.cPlayer_weapon = cWeapon_sale;
        player.iMoney -= iBuy_price[i];
        cout << "You bought " << cWeapon_sale[i] << " for " << iBuy_price[i] << "$" << endl;
    }
    else
    {
        cout << "You can't buy that weapon" << endl;
    }
}

isnt that wrong? I get this error:
141 C:\Documents and Settings\Andreas\Mina dokument\Programmering\Egna grejer\Adventure\Game.cpp incompatible types in assignment of `char*' to `char[0u]'

whats wrong now=/
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #8
Ok, then you need to do that strcpy thing on that line. Just as I showed you in my earlier post. Smile
Quote this message in a reply
n0n4m3
Unregistered
 
Post: #9
yeah ive done that. now it works to compile=) but theres something weird anyway. here's my code now. I have added a new function, fStatus. With that you can se your HP, EXP, Weapon and money.

Code:
#include <iostream>
#include <conio.h>

using namespace std;

//Alla strukturer
struct sPlayer{
    int iMoney;
    int iHp;
    int iExp;
    int iAttack;
    int iWeapon_strenght;
    char cPlayer_weapon[];
};

//Alla funktioner
int fMenu(sPlayer& player);
int fShop(sPlayer& player);
int fBuy_weapon(char cWeapon_sale[], int iBuy_price[], int iChoice, int i, sPlayer& player);
void fStatus(sPlayer& player);

int main()
{
    //Ger variablerna sina värden
    sPlayer player;
    player.iMoney = 200;
    player.iHp = 100;
    player.iExp = 0;
    player.iAttack = 10;
    player.iWeapon_strenght = 0;

    //GÃ¥r till menyn och stannar kvar där tills spelet avslutas
    fMenu(player);
    return 0;
}

//Huvudmenyn
int fMenu(sPlayer& player)
{
    int iChoice;
    
    do
    {
        //Huvudmenyn
        system("CLS");
        cout << "1. Forrest" << endl;
        cout << "2. Shop" << endl;
        cout << "3. View satus" << endl;
        cout << "4. Exit" << endl;
        cin >> iChoice;
        
        //Ditt val
        switch (iChoice)
        {
            case 1:
                break;
            
            case 2:
                fShop(player);
                break;
            
            case 3:
                fStatus(player);
                break;
            
            default:
                cout << "Try again" << endl;
                break;
        }
    }while(iChoice != 4);  
}

//Shop
int fShop(sPlayer& player)
{
    char cWeapon_sale[4][30] = {"Knife", "Sword", "Bow", "Axe"};
    int iWeapon_strenght[4] = {10, 18, 28, 42};
    int iBuy_price[4] = {80, 100, 160, 220};
    int iChoice;
    int i;
    
    do
    {
        //Vapen-meny
        system("CLS");
        cout << "What weapon do you want to look at?" << endl;
        cout << "1. " << cWeapon_sale[0] << endl;
        cout << "2. " << cWeapon_sale[1] << endl;
        cout << "3. " << cWeapon_sale[2] << endl;
        cout << "4. " << cWeapon_sale[3] << endl;
        cout << "5. Leave shop" << endl;
        cin >> i;
        i -= 1;
        
        if(i >= 0 && i <= 5)
        {
            system("CLS");
            cout << "Weapon: " << cWeapon_sale[i] << endl;
            cout << "Strenght: " << iWeapon_strenght[i] << endl;
            cout << "Price: " << iBuy_price[i] << endl;
            cout << endl;
            cout << "You have: " << player.iMoney << "$" << endl;
            cout << "1. Buy weapon" << endl;
            cout << "2. Sell weapon" << endl;
            cout << "3. View another weapon" << endl;
            cin >> iChoice;
            
            //Hanterar ditt val
            switch (iChoice)
            {
                case 1:
                    fBuy_weapon(cWeapon_sale[i], iBuy_price, iChoice, i, player);
                    break;
                
                case 2:
                    break;
                
                case 3:
                    fShop(player);
                    break;
                    
                default:
                    cout << "Try again" << endl;
                    break;
            }
        }
        else if(i == 5)
        {
            return 0;
        }
        else
        {
            cout << "Try again" << endl;
            getch();
        }
    }while(i != 5);
}


//Köper weapon
int fBuy_weapon(char cWeapon_sale[], int iBuy_price[], int iChoice, int i, sPlayer& player)
{
    //Kollar om jag har pengar och att jag inte redan har ett likadant vapen
    if(player.iMoney >= iBuy_price[i] && strcmp(cWeapon_sale, player.cPlayer_weapon)!=0)
    {
        strcpy(player.cPlayer_weapon, cWeapon_sale);
        player.iMoney -= iBuy_price[i];
        cout << "You bought " << cWeapon_sale[i] << " for " << iBuy_price[i] << "$" << endl;
    }
    else
    {
        cout << "You can't buy that weapon" << endl;
    }
    getch();
}

void fStatus(sPlayer& player)
{
     cout << "Weapon: \t" << player.cPlayer_weapon << endl;
     cout << "Money: \t" << player.iMoney << endl;
     cout << "HP: \t" << player.iHp << endl;
     cout << "EXP: \t" << player.iExp << endl;
     getch();
}

when I buy a weapon, is it working nice. but when I want to leave the shop then the error come. instead of going to fMenu i get this:
Weapon: ´+-
Strengh: 129789439
Price: 10

You have: 120$
1. Buy weapon
2. Sell weapon
4. View another weapon

how does that come?
Quote this message in a reply
Moderator
Posts: 770
Joined: 2003.04
Post: #10
You have 4 weapons (4 cWeapon_sale, 4 iWeapon_strenght and 4 iBuy_price)
When you select Leave Shop by entering 5, you get i=5.
Then, you subtract 1, so i=4, but then you "cout << "Weapon: " << cWeapon_sale[i] << endl;", so you are out of the array bounds (0...3) and therefore random data is printed.
Quote this message in a reply
Post Reply