Trying to make a string identifier

This is a discussion on Trying to make a string identifier within the C++ Programming forums, part of the General Programming Boards category; Code: #define string mystring [] the error says " c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(113) : error C2065: 'CardD' : undeclared identifier ...

  1. #1
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114

    Trying to make a string identifier

    Code:
    #define string mystring []
    the error says " c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(113) : error C2065: 'CardD' : undeclared identifier
    Error executing cl.exe. "


    CardD is being initialized as

    Code:
    string CardD = "Test";

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    just make sure you have <string> include as a header, then just simply type in the main()

    Code:
    string CardD = "Test";
    now a variable of string type, named CardD will hold the information "Test"

    Code:
    cout << CardD << endl;
    will say your information to the console held by the string CardD

  3. #3
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    here is my function:

    Code:
    int Display(int A, int B, int C, int D, int& E, int& F)
    {
    
    	string CardA = "boo";
    	string CardB = "bee";
    	string CardC = "baa";
    	string CardD = "bum";
    
    	//=====================
    
    	E = A+B; // PLAYER TOTAL = PLAYERTOTALA+PLAYERTOTALB
    	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);
    
    }
    Here is my error message after adding:

    Code:
    #include <string>
    Code:
    --------------------Configuration: BlackJack - Win32 Debug--------------------
    Compiling...
    Source_BlackJack.cpp
    c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(102) : error C2065: 'string' : undeclared identifier
    c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(102) : error C2146: syntax error : missing ';' before identifier 'CardA'
    c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(102) : error C2065: 'CardA' : undeclared identifier
    c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(102) : error C2440: '=' : cannot convert from 'char [4]' to 'int'
            This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(103) : error C2146: syntax error : missing ';' before identifier 'CardB'
    c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(103) : error C2065: 'CardB' : undeclared identifier
    c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(103) : error C2440: '=' : cannot convert from 'char [4]' to 'int'
            This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(104) : error C2146: syntax error : missing ';' before identifier 'CardC'
    c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(104) : error C2065: 'CardC' : undeclared identifier
    c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(104) : error C2440: '=' : cannot convert from 'char [4]' to 'int'
            This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(105) : error C2146: syntax error : missing ';' before identifier 'CardD'
    c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(105) : error C2065: 'CardD' : undeclared identifier
    c:\documents and settings\owner\desktop\comp prog\c++\blackjack\source_blackjack.cpp(105) : error C2440: '=' : cannot convert from 'char [4]' to 'int'
            This conversion requires a reinterpret_cast, a C-style cast or function-style cast

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Can you post the rest of the code, i think some if not all the errors are due to the code before it.

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Class string is enclosed in namespace std. Either include "using namespace std;" or use "std::string" instead of just "string".

  6. #6
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    Code:
    #include <iostream.h>
    #include <string>
    #include <time.h>
    #include <cstdlib>
    #include <stdlib.h>
    
    
    //=============================================================
    
    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, int& E, int& F);//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,PlayerTotal,ComputerTotal);//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, int& E, int& F)
    {
    
    	string CardA = "boo";
    	string CardB = "bee";
    	string CardC = "baa";
    	string CardD = "bum";
    
    	//=====================
    
    	E = A+B; // PLAYER TOTAL = PLAYERTOTALA+PLAYERTOTALB
    	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);
    
    }

  7. #7
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    Quote Originally Posted by LuckY
    Class string is enclosed in namespace std. Either include "using namespace std;" or use "std::string" instead of just "string".
    when ever i add using name space std; i get an error saying std is not a name space. I just switched from Dev c++ to VS 6.0

  8. #8
    Banned
    Join Date
    Jun 2005
    Posts
    594
    yea he is right sorry i just assumed you had

    Code:
    using namespace std;
    somewhere in your code

  9. #9
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    Ok im not getting the std does not exist anymore, now im at this:

    Code:
    --------------------Configuration: BlackJack - Win32 Debug--------------------
    Compiling...
    Source_BlackJack.cpp
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\BlackJack\Source_BlackJack.cpp(114) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std
    ::allocator<char> >' (or there is no acceptable conversion)
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\BlackJack\Source_BlackJack.cpp(115) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std
    ::allocator<char> >' (or there is no acceptable conversion)
    Error executing cl.exe.
    here are lines 114 and 115:

    Code:
    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";

  10. #10
    Banned
    Join Date
    Jun 2005
    Posts
    594
    i think is has to do with E and F, why do you need to pass the address? can you just pass the value into the int in your function just like you do with a b c and d ?

  11. #11
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    this function also updates those variables as well.

  12. #12
    Banned
    Join Date
    Jun 2005
    Posts
    594
    yea i noticed that for a and b, but e and f, you do this
    Code:
    E = A+B; // PLAYER TOTAL = PLAYERTOTALA+PLAYERTOTALB
    F = C+D; // COMPUTER TOTAL = COMPUTERCARDA+COMPUTERCARDB
    why not remove the int &e and the int&f from your prototype and the function and just do

    Code:
    int E = A+B; // PLAYER TOTAL = PLAYERTOTALA+PLAYERTOTALB
    int F = C+D; // COMPUTER TOTAL = COMPUTERCARDA+COMPUTERCARDB
    i say this because in your prototype it looks liek you are sending the same information your just calculating inside here, which i think is causing the problem.

  13. #13
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    Yuck.

    Ok, first off:
    Code:
    #define string mystring []
    Bad. Bad. Bad.
    I'm not really sure why you did that? Anyway, just get rid of it.

    Second, your include section should be like so:

    Code:
    #include <iostream>
    #include <string>
    // other includes
    
    using namespace std;
    Then you can declare the string variables perfectly fine.

    Code:
    string myString="blah";

  14. #14
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    updated code:

    Code:
    int Display(int A, int B, int C, int D, int& E, int& F)
    {
    
    	string CardA;
    	string CardB;
    	string CardC;
    	string CardD;
    
    	if (A == 11){
    		CardA = "Jack";
    	}
    	else if (A == 12){
    		CardA = "Queen";
    	}
    	else if (A == 13){
    		CardA = "King";
    	}
    	else if (A == 14){
    		CardA = "Ace";
    	}
    
    	//====================
    
    	if (B == 11){
    		CardB = "Jack";
    	}
    	else if (B == 12){
    		CardB = "Queen";
    	}
    	else if (B == 13){
    		CardB = "King";
    	}
    	else if (B == 14){
    		CardB = "Ace";
    	}
    
    	//=====================
    
    	if (C == 11){
    		CardC = "Jack";
    	}
    	else if (C == 12){
    		CardC = "Queen";
    	}
    	else if (C == 13){
    		CardC = "King";
    	}
    	else if (C == 14){
    		CardC = "Ace";
    	}
    
    	//====================
    
    	if (D == 11){
    		CardD = "Jack";
    	}
    	else if (D == 12){
    		CardD = "Queen";
    	}
    	else if (D == 13){
    		CardD = "King";
    	}
    	else if (D == 14){
    		CardD = "Ace";
    	}
    
    
    	//=====================
    
    	E = A+B; // PLAYER TOTAL = PLAYERTOTALA+PLAYERTOTALB
    	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);
    
    }

  15. #15
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    Quote Originally Posted by ILoveVectors
    yea i noticed that for a and b, but e and f, you do this
    Code:
    E = A+B; // PLAYER TOTAL = PLAYERTOTALA+PLAYERTOTALB
    F = C+D; // COMPUTER TOTAL = COMPUTERCARDA+COMPUTERCARDB
    why not remove the int &e and the int&f from your prototype and the function and just do

    Code:
    int E = A+B; // PLAYER TOTAL = PLAYERTOTALA+PLAYERTOTALB
    int F = C+D; // COMPUTER TOTAL = COMPUTERCARDA+COMPUTERCARDB
    i say this because in your prototype it looks liek you are sending the same information your just calculating inside here, which i think is causing the problem.
    ok i have done that, but im still getting the same two errors.

Page 1 of 2 12 LastLast
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, 07: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, 04:58 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21