Thread: Trying to make a string identifier

  1. #16
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Ok i compiled your code and got rid of the errors, take a look
    Code:
    #include <iostream>
    #include <string>
    #include <time.h>
    #include <cstdlib>
    #include <stdlib.h>
    using namespace std;
    
    //==================================================  ===========
    
    int PlayerCardA=0;//PLAYER CARD 1
    int PlayerCardB=0;//PLAYER CARD 2
    int PlayerTotal=0;//PLAYER TOTAL
    int ComputerCardA=0;//COMPUTER CARD 1
    int ComputerCardB=0;//COMPUTER CARD 2
    int ComputerTotal=0;//COMPUTER TOTAL
    int Bank=1000;//BANK
    int xWager=0;//WAGER
    int xNewCard=0;//NEW CARD
    bool xSync=true; //SYNC TRUE?
    bool xHit_Stay=false; //HIT OR STAY?
    
    //==================================================  ===========
    
    void PlayerCards(int& A, int& B);//PLAYER CARDS
    void ComputerCards(int& A, int& B);//COMPUTER CARDS
    int Wager(int& A);//WAGER
    int NewCard();//NEW CARD
    void Winner(bool& A, bool& B, bool& C);//WINNER
    int Display(int A, int B, int C, int D);//DISPLAY CARDS
    bool Hit_Stay();//HIT OR STAY
    
    
    //==================================================  ===========
    
    int main(void)
    {
    
    	srand(time(NULL));  
    
    	cout<<"Welcome to Black Jack. You have $1000 in the bank.\n\n";
    	cout<<"------------------------------------------------------";
    	cout<<"\nPress enter to continue...";
    	cin.get();
    	system("cls");
    	do
    	{
    		xWager = Wager(xWager); //CALL WAGER FUNCTION
    		PlayerCards(PlayerCardA,PlayerCardB);//GENERATE PLAYER CARDS
    		ComputerCards(ComputerCardA,ComputerCardB);//GENERATE COMPUTER CARDS
    		Display(PlayerCardA, PlayerCardB, ComputerCardA, ComputerCardB);//DISPLAY TOTALS
    	}
    	while(xSync == true);
    
    	return(0);
    }
    
    //==================================================  ===========
    
    int Wager(int& A)
    {
    
    	cout<<"Please enter your wager:$"; //REQUEST WAGER
    	cin>>xWager; //GET WAGER
    	system("cls");
    
    	return(A);
    
    }
    
    //==================================================  ===========
    
    void PlayerCards(int& A, int& B)
    {
    
    	A = 2 + (rand() % (14 - 2 + 1));
    	B = 2 + (rand() % (14 - 2 + 1));		
    
    }
    
    //==================================================  ===========
    
    void ComputerCards(int& A, int& B)
    {
    
    	A = 2 + (rand() % (14 - 2 + 1));
    	B = 2 + (rand() % (14 - 2 + 1));		
    
    }
    
    //==================================================  ===========
    
    int Display(int A, int B, int C, int D)
    {
    
    	string CardA = "boo";
    	string CardB = "bee";
    	string CardC = "baa";
    	string CardD = "bum";
    
    	//=====================
    
    	int E = A+B; // PLAYER TOTAL = PLAYERTOTALA+PLAYERTOTALB
    	int F = C+D; // COMPUTER TOTAL = COMPUTERCARDA+COMPUTERCARDB
    	
    	cout<<"You drew a "<< CardA << " and a "<< CardB <<"  for a total of " << E <<".\n";
    	cout<<"The computer drew a "<< CardC << " and a "<< CardD <<" for a total of "<< F <<".\n";
    
    	cout<<"\nPress enter to continue...";
    	cin.get();
    
    	system("cls");
    	return 0;
    }
    all i did was fix your include of iostream.h to iostream, and i changed the prototype and the function.

  2. #17
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    is there a way i can convert ints into strings?

  3. #18
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Quote Originally Posted by ILoveVectors
    all i did was fix your include of iostream.h to iostream, and i changed the prototype and the function.
    As I was saying....

  4. #19
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    string mystring = "99999";
    char * mychararray;
    
    mychararray =  mystring.data();
    
    long mystring2numbers = atoi( mychararray );
    this should work if you can follow, there may be an easier way,
    i dont know for sure, havent done much work with this.

    if you are having a hard time following this, search on google
    for atoi and string.data

  5. #20
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    what about the other way around?

    like

    int x = 4
    string y;

    make y = x

  6. #21
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    int mynumber = 4;
    char mychar = itoa(mynumber);
    string mystring = mychar;

    i believe thats correct

  7. #22
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Bah beaten.

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    ILoveVectors, your methods of converting are bad. The data() function doesn't necessarily return a null-terminated character array, and therefore will cause your code to not work as expected, and possibly crash. Use c_str() instead if you want to get to a character array. Or use a stringstream. Also, your second method uses the non-standard itoa function which usually returns a character array, not a single character. Even if it returned a single character, that's not very helpful for two digit numbers. Use a stringstream.

    See this FAQ (39.1 & 39.2) for stringstream examples, or for a simpler examples I believe there is a FAQ on this website.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM