dandymcgee Posted December 9, 2007 Share Posted December 9, 2007 (edited) I'm having trouble creating a struct with a varying name... It's sort of hard to explain as I don't know much C++. Read the comments in the code. This is how I am currently trying to do it: CODE#include <iostream> using namespace std; void display(string accountname); struct database{ string acctname; int acctnum; int level; int exp; }; int main() { string currentaccount = "dandymcgee"; display(currentaccount); cin.get(); } void display(string accountname) { //"accountname" should be the input that was given when the function was called //the variable currentaccount was given, which is equal to "dandymcgee" // //I want to create a new entry in database called "dandymcgee" not "accountname" // database accountname; // "accountname" is a variable which i want to be replaced with "dandymcgee" accountname.acctname = "dandymcgee"; accountname.acctnum = 1; accountname.level = 28; accountname.exp = 14029; // Display the final result... cout<<"========================================"<< endl; cout<<" Account Name: "<< accountname.acctname << endl; cout<<" Account Number: "<< accountname.acctnum << endl; cout<<" Level: "<< accountname.level << endl; cout<<" Experience: "<< accountname.exp << endl; cout<<"========================================"<< endl; } Upon trying to compile this source code I get the error: ""declaration of 'database accountname' shadows a parameter"" This is because I'm telling it to store the string that was sent to the function in the variable "accountname", and then saying to make a new entry in database called "accountname"... but i want to create a new entry in database called "<data from string 'accountname' here>" (a.k.a. dandymcgee). Thanks for your help. Edited January 14, 2008 by dandymcgee - Dan [Website] Link to comment Share on other sites More sharing options...
GaryFrost Posted December 9, 2007 Share Posted December 9, 2007 and what do you have as the parameter name? void display(string accountname) SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference. Link to comment Share on other sites More sharing options...
-Ultima- Posted December 9, 2007 Share Posted December 9, 2007 (edited) You can't redeclare a variable with the same name in the same scope. In particular, you're declaring a new database variable with the same name as the string parameter -- both named accountname. Simple solution: rename one of the variables. Redeclaring the variable won't get you the result you want (which, I presume, would be to store the passed argument -- "dannymcgee" -- into the database struct). You need to explicitly set the struct's internal acctname variable to the passed name. void display(string name) { //"accountname" should be the input that was given when the function was called //the variable currentaccount was given, which is equal to "dandymcgee" // //I want to create a new entry in database called "dandymcgee" not "accountname" // database accountname; // "accountname" is a variable which i want to be replaced with "dandymcgee" accountname.acctname = name; accountname.acctnum = 1; accountname.level = 28; accountname.exp = 14029; // Display the final result... cout<<"========================================"<< endl; cout<<" Account Name: "<< accountname.acctname << endl; cout<<" Account Number: "<< accountname.acctnum << endl; cout<<" Level: "<< accountname.level << endl; cout<<" Experience: "<< accountname.exp << endl; cout<<"========================================"<< endl; } Edited December 9, 2007 by -Ultima- [ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ] Link to comment Share on other sites More sharing options...
dandymcgee Posted December 9, 2007 Author Share Posted December 9, 2007 (edited) That only changes the name that is displayed on the console screen... I need it to change the name of the entry it is adding to the database struct. Here is the overall goal: CODE#include <iostream> using namespace std; struct database{ string acctname; int acctnum; int level; int exp; }; void enteracct(string name, int acctnum, int level, int exp); void display(string name); int main() { string currentaccount; enteracct("dandymcgee", 1, 25, 4500); enteracct("secondacct", 2, 28, 4900); enteracct("thirdacct", 3, 30, 5000); currentaccount = "dandymcgee"; display(currentaccount); cin.get(); currentaccount = "secondacct"; display(currentaccount); cin.get(); currentaccount = "thirdacct"; display(currentaccount); cin.get(); } void enteracct(string name, int acctnum, int level, int exp) { // i want to make a new entry in the database struct // that is called whatever the data in string name is // ex. - enteracct("dandymcgee", 1, 25, 4500); // i want to create an entry called dandymcgee // ex. - enteracct("secondacct", 2, 28, 4900); // i want to create an entry called secondacct // ex. - enteracct("thirdacct", 3, 30, 5000); // i want to create an entry called thirdacct database name; name.acctname = name; name.acctnum = acctnum; name.level = level; name.exp = exp; } void display(string name) { // i want the same thing to happen here... // whatever parameter i give to this function // should be the database entry that it reads from // and displays on the screen cout<<"========================================"<< endl; cout<<" Account Name: "<< name.acctname << endl; cout<<" Account Number: "<< name.acctnum << endl; cout<<" Level: "<< name.level << endl; cout<<" Experience: "<< name.exp << endl; cout<<"========================================"<< endl; } I know... you can't use the same variable name in the param as you do to create the database entry. So how do i create a new database entry with the name of whatever the value of string name is? Thanks for your help. Edited January 14, 2008 by dandymcgee - Dan [Website] Link to comment Share on other sites More sharing options...
Valik Posted December 9, 2007 Share Posted December 9, 2007 You need to learn about scope and arrays. And classes although they are beyond you since you don't understand more basic things. All your enteracct() function does is creates a new variable of type database, populates it, then returns. You pass a string to display which is only half of what you need, you need to pass a valid database array which you search by the string that's passed in. In fact, the more I look over your code, the more I can't help but think that you need to go back and start all over with whatever material you're trying to learn C++ from. You're missing some very fundamental things. Link to comment Share on other sites More sharing options...
dandymcgee Posted December 9, 2007 Author Share Posted December 9, 2007 (edited) Thanks for the input Valik, I'm going to study C++ a some more before I try to get this working the way I want it to again. If I still need help after some additional reading and studying I'll be sure to post here and let you guys know. This version works almost perfectly... I don't think it's possible to do it the way I originally wanted to (if so I'm out of ideas on how to do it). I wanted to be able to prompt for an accountname, and then define a database variable as whatever was typed in so that all of the data would be stored under (ex. dandymcgee.lvl rather than aaa.lvl). Guess I'll have to live with this being my final working version. CODE#include <iostream> using namespace std; struct database{ std::string acctname; int acctnum; int level; int exp; }; database enteracct(string acctname, int acctnum, int level, int exp); void display(database passedname); int main() { string inacct; int innum; int inlvl; int inexp; database aaa; //database aab; //database aac; cout<<"Enter accountname: "; cin>> inacct; cout<<"Enter accountnum: "; cin>> innum; cout<<"Enter accountlvl: "; cin>> inlvl; cout<<"Enter accountexp: "; cin>> inexp; cin.get(); aaa = enteracct(inacct, innum, inlvl, inexp); //aab = enteracct("secondacct", 2, 28, 4900); //aac = enteracct("thirdacct", 3, 30, 5000); display(aaa); //display(aab); // display(aac); } database enteracct(string acctname, int acctnum, int level, int exp) { database name; name.acctname = acctname; name.acctnum = acctnum; name.level = level; name.exp = exp; return name; } void display(database passedname) { cout<<"========================================"<< endl; cout<<" Account Name: "<< passedname.acctname << endl; cout<<" Account Number: "<< passedname.acctnum << endl; cout<<" Level: "<< passedname.level << endl; cout<<" Experience: "<< passedname.exp << endl; cout<<"========================================"<< endl; cin.get(); } Edited January 14, 2008 by dandymcgee - Dan [Website] Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now