Solved

string data type

Posted on 2000-03-15
5
192 Views
Last Modified: 2010-04-02
All I want to do is convert a string type into an integer.  This integer is used to update a MySQL database.  I'm not sure if this is where I'm having the problem or not, but if I hard code integers in I don't have a problem with the function.  When I use the code here(variables), I get a segmentation fault.  The weird thing is, the database gets updated correctly.

Here's the code to the function.

/* Date:           03/09/2000
   Function Name:  deposit
   Parameters:     string,Person,Room
   Return:         (none)
   OS:             SunOS 5.7
   Compiler:       gcc 2.8.1
   Programmer:     Scott R. Dahl
   Description:    This procedure will first test to see if the room has the deposit event, then test to see if the
                   person actually has the amount of money they
                   want to deposit.  If they do, the money is subtracted from their current money, and added
                   to their bank account.
*/

#ifndef DEPOSIT_H
#define DEPOSIT_H

#include <mysql.h>


MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
char *db_host = "localhost";
char *db_username = "********";
char *db_password = "********";
char *db_database = "mud";
char myquery[200];

#include "/home/mud/classes/person/person.cpp"
#include "/home/mud/classes/room/room.cpp"
#include "/home/mud/functions/parser.h"

void deposit(string amt,Person per, Room room)
{
        int i = 0;
        int deposit_amt = 0;
        int has_amt = 0;
        char* dummy;
        char* xnew_amt;
        string new_amt;

        // Make sure the room is a bank
        if(!room.hasEvent("deposit"))
        {
                cout << "You can't deposit money here, go to a bank!" << endl;
                return;
        }

        // Capture the amount of money the person is currently holding
        has_amt = per.getMoney();

        // make sure the person has money
        if(has_amt == 0)
        {
                cout << "You don't have any coins to deposit!" << endl;
                return;
        }

        // put the string passed into lowercase
        while(amt[i])
        {
                amt[i] = tolower(amt[i]);
                i++;
        }

        // get the first word/number from the string passed
        new_amt = parser(amt);

        // If the user wants to deposit all their money, we should make the deposit
        // amount what they are currently holding
        if(new_amt == "all")
        {
                deposit_amt = has_amt;
        }
        // Otherwise we have to figure out how much they want to deposit.
        else
        {       // If the first set of characters passed is a number,
                // set it equal to the deposit amoun..
                if(isdigit(new_amt[0]))
                {
                        strcpy(xnew_amt, new_amt.c_str());
                        deposit_amt = int(strtod(xnew_amt,&dummy));
                }
                else
                {
                        cout << "You can't do that.  Type <help deposit> for more information." << endl;
                        return;
                }
        }

        // Make sure they are trying to deposit coins!
        if(amt != "coins" && amt != "coin")
        {
                cout << "You can only deposit coins!  Type <help deposit> for more information." << endl;
                return;
        }

        // Make sure they have the amount they want to deposit.
        if(has_amt < deposit_amt)
        {
                cout << "You don't have enough coins!" << endl;
                return;
        }


        // If we made it this far, it's time to make the deposit!
        per.addBankMoney(deposit_amt);
        //per.addBankMoney(5);

        // set the deposit_amt to negative so we can take it away from their
        // current holding money
        deposit_amt *= -1;
        per.addMoney(deposit_amt);
        //per.addMoney(-5);

        return;
}


#endif // DEPOSIT_H

And here's the code to the test proggy.
/* Date:           03/09/2000
   Function Name:  just a test driver for the deposit function
   Parameters:     (none)
   Return:         (none)
   OS:             SunOS 5.7
   Compiler:       gcc 2.8.1
   Programmer:     Scott R. Dahl
   Description:    This procedure will test the deposit function
*/

#ifndef DEPOSIT_CPP
#define DEPOSIT_CPP
#include <string>
#include "deposit.h"

void main()
{

        mysql_init(&mysql);
        mysql_options(&mysql,MYSQL_OPT_COMPRESS,0);
        mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP, "odbc");
   
        // Connect to the database
        if(!mysql_real_connect(&mysql,db_host,db_username,db_password,db_database,0,NULL,0))
        cerr << "Failed to connect to database: Error: " << mysql_error(&mysql) << endl;

        // Initialize the database
        else
        {
                Person per(1);
                Room room(99,99);

                cout << "person has: " << per.getMoney() << " in hand before deposit" << endl;
                cout << "person has: " << per.getBankMoney() << " in bank before deposit" << endl;
     
                deposit("5 coins",per,room);
                cout << "hello?" << endl;

                cout << "person has: " << per.getMoney() << " in hand after deposit" << endl;
                cout << "person has: " << per.getBankMoney() << " in bank after deposit" << endl;

        }
}

#endif // DEPOSIT_CPP

The program halts after the deposit function runs in its entirety, then I get a segmentation fault.  Like I said, if I hard code the integers, it runs smooth.  Don't forget that either way, the database gets updated correctly.

Thanks in advance for your help! :-)
0
Comment
Question by:scottd1978
5 Comments
 
LVL 1

Expert Comment

by:ntdragon
ID: 2622496
for transfering an string to int
use
#include <stdio.h>
atoi(char*str);
0
 

Expert Comment

by:jclayton
ID: 2623097
I think you're steeping on a local stack value within the deposit() method somehow.

Look at xnew_amt - the local variable is not initialised to anything prior to the strcpy() call.

This *might* make sense regards the method working UNTIL it tries to rewind the stack - at which point it goes bang.
0
 

Author Comment

by:scottd1978
ID: 2623994
Same thing happens here.  I tried atoi as well, and get the same thing.
0
 
LVL 1

Accepted Solution

by:
TKII earned 25 total points
ID: 2624441
Even if you rejected jclayton's answer but he is right. You must not copy with strcpy to xnew_amt. This is a pointer which points to "nowhere". You need a char array/vector there.

char xnew_amt[100];

strcpy(xnew_amt, new_amt.c_str());

Give it a try.


If your problem still exists it would be nice to see the definition of your person class, to see how AddMoney() and AddBankMoney() work.
0
 

Author Comment

by:scottd1978
ID: 2624521
DOH!  Thank you.  I still don't fully understand pointers.  
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now