string variable used as a variable...

This is a discussion on string variable used as a variable... within the C++ Programming forums, part of the General Programming Boards category; is there any way to have a string used as the name of a variable? ie Code: vector<Names> Male(10); vector<Names> ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    string variable used as a variable...

    is there any way to have a string used as the name of a variable?

    ie

    Code:
    vector<Names> Male(10);
    vector<Names> Female(10);
    
    Male[3].name="John";
    
    string hold = "Male";
    
    cout<<hold[3].name
    not sure if this is possible, not even sure if it would be good programming to do so...mostly just wondering if that is possible...

    thx
    MSVC++~

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    No, you can't.

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    So you want to be able to parhaps get a string from the user then use a variable of that name? I don't think thats possible, you cannot 'change' the names of your variables while the program is running (can you?).

    Ie
    PHP Code:
    string name="paul";

    string "paul" "john"
    Is just illogical.

    ~ Paul

  4. #4
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >>you cannot 'change' the names of your variables while the program is running (can you?).

    No.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    no no I think you all misunderstood...

    What I meant was...

    You have several definitions of a struct

    Code:
    struct Gender
    {
         bool Nice;
    };
    
    Gender Male;
    Gender Female;
    
    string what_are_you;
    cout<<"Are you male or female?\n";
    cin>>what_are_you;
    
    what_are_you.Nice=true;
    I guess this would be used in case that the user would have to define what file he would want opened and such...

    I hope this makes sense...
    MSVC++~

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    no.

    but try it, it should not compile. .Nice is not a member of string, it is a member of struct Gender.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    You can use polymorphism and an object factory which creates objects based on an string.

    But that won't be very usefull in your situation. I just like to sound smart.

    gg

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    i realize that code is incorrect...and i really dont know anything about polywhateverish....

    let us try this once more...

    you have a program, someone needs to open up a save, you have no idea what the name of the saved file is...

    so you cant do

    Code:
    fstream File("save.txt");

    instead you will have to use a string and have it open up that way...

    Code:
    string name="code.txt";
    
    fstream File(name);
    I hope I cleared up my question....
    MSVC++~

  9. #9
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Thats a totally different question to the one you first asked, but
    PHP Code:
    string Name;
    string fileName;
    string ext ".txt";

    cin >> Name;
    fileName Name+ext;         // If you can do that with strings, I havent done this for so long. 
                                                // I know an ansi string you can.
    fstream File(fileName); 
    It uses the string and data for a variable, NOT as the name of a variable, which is what you had been trying to do previousley.

    ~ Paul

  10. #10
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    or u can cut out the middle-man

    Code:
     
    
    string name;
    string ext = ".txt";
    
    cout << "Enter name for file(no ext included!): ";
    cin >> name;
    
    name += ext;
    
    fstream file(name);
    also, a simple additon of

    Code:
    cout<< "Enter the extentions type in format: *.ext* and press enter: ";
    cin>>ext;
    will let u save as any file.

    So if u want a file called test as a doc file you would enter

    test

    when asked and

    .doc

    when asked.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    I'm not quite sure if it will let you do that. You can try it, though. If it doesn't work, you may want to try using the .c_str() fxn to convert it to a c string, because I believe ifstream takes a c string as the argument:

    for example:
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    int main()
    {
    	std::string test;
    	
    	std::cout << "Enter filename (with file type, ex. text.txt): ";
    	std::cin >> test;
    	
    	std::ifstream infile(test.c_str());
    	if(!infile) {
    		std::cerr << "Error opening " << test << std::endl;
    		exit(1);
    	} else {
    		std::cout << "File open." << std::endl;
    	}
    	
    	return 0;
    }
    I tried it without the .c_str() fxn, it wouldn't compile. it should be because ifstream takes c strings as arguments in this example.

    You can use the suggestions above to let the user have options, just remember, if you want to open the file, and you have the filename stored in a string to use the .c_str() fxn. hope this helps some.

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    Ah, well I guess my question changed through out the thread...

    but thanks for clearing it all up!
    MSVC++~

  13. #13
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >>I'm not quite sure if it will let you do that

    Heres a program i have that does it. Its a few months old so ignore the comments lol.

    Code:
    #include <iostream>
    #include <windows.h>
    #include <string>
    #include <stdlib.h>
    
    using namespace std;
    
    int find_files();
    
    /*	Define our STD's*/
    using std::string;
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    int main(int argc,char** argv)
    {
    
    	find_files();
    
    	system("pause");
    
    	return 0;
    	
    
    }
    
    int find_files()
    {
    	/*	Holds information found*/
    	WIN32_FIND_DATA wfd;
    
    	/*	Holds directory*/
    	string str,extension;
    
    	str = "";
    
    
    	/*	Counts files found*/
    	int i = 0;
    
    	/*	Prompt user for directory*/
    	cout<< "Enter Directory: ";
    	cin>>str;
    
    	cout<< "Enter the extentions type in format: *.ext* and press enter: ";
    	cin>>extension;
    
    	extension = "\\" + extension;
    
    	/*	Outout empty line*/
    	cout<<"\n";
    
    	/*	Indicates we want mp3 files*/
    	str += extension;
    
    	/*	Find first file*/	
    	HANDLE hFile =  FindFirstFile(str.c_str(),&wfd);
    
    	/*	Aborts on error*/
    	if(hFile ==INVALID_HANDLE_VALUE)
    	{
    		cout<<"No files found or invalid directory\n";
    		return 1;
    	}
    
    	do
    	{
    		/*	out files*/
    		cout<<wfd.cFileName<<endl;
    
    		/*	Increment counter*/
    		i++;	
    	}
    	
    	/*	Get the rest of the files*/
    	while(FindNextFile(hFile,&wfd));
    
    	/*	Output # of found files*/
    	cout<<"\nFound "<<i<<" Files.\n"<<endl;
    
    	/*	Clean up*/
    	FindClose(hFile);
    
    	
    	return 0;
    }

  14. #14
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Yes but your program does not use the fstream function, which is what alpha meant with
    ...because I believe ifstream takes a c string as the argument
    Thats why he meant he didn't think you were allowed to do it, and while I havent tested it I think he might be right on that one.

    ~ Paul

    Edit: I just tested, and alphas right, you do need to add .c_str() to it otherwise ifstream wont accept it.
    Last edited by nickname_changed; 03-29-2003 at 03:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

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