I have been looking at referencing (& symbol) and i cant seem to figure it out, in my code i want to get the plrname and scname from MAINPROGRAM so i can save the data but how do i do it, can someone please show me what im doing wrong.
void MAINPROGRAM( ); //Prototype should have same parameters as declaration
usingnamespace std;
int main()
{
string choice;
cout << "New" << endl;
cout << "Load" << endl;
cin >> choice;
if(choice == "New" || choice == "new")
{
MAINPROGRAM();
}
elseif(choice == "Load" || choice == "load")
{
}
}
void MAINPROGRAM()
{
string scname; //Don't need the address of variable you're declaring. That makes no sense.
string plrname;
if(cin.peek() == '\n')
{
cin.ignore();
}
cout << "Welcome to Shipping Supplies" << endl;
cout << "Please enter the name of your shipping company" << endl;
getline(cin, scname);
cout << "\n";
cout << "Ok so the name of your company is " << scname << endl;
cout << "\n";
cout << "And what is your name?" << endl;
getline(cin, plrname);
cout << "Ok " << plrname << " welcome to the game! lets get started" << endl;
cout << "\n";
}
void SAVEGAME()
{
string plrname; //Make these 2 variables. Otherwise you have nowhere to load plrname and filename in from file
string scname;
ofstream file("SS.txt");
file << plrname << endl;
file << scname << endl;
//Call the game function using plrname and scname as parameters
}
ok i sort of get it now, but my question is what if i have like 12 different things i want to reference? the parameters would get too crouded so is there an alternative or something??
also i got some errors:
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|9|error: variable or field 'SAVEGAME' declared void|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|9|error: 'string' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|9|error: 'string' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp||In function 'void MAINPROGRAM()':|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|43|error: 'SAVEGAME' was not declared in this scope|
||=== Build finished: 4 errors, 0 warnings ===|
References... they're aliases (alternative names, if you will) to a variable. You "feed" them the variable they're referencing at declaration.
1 2
int a; // regular variable
int &ra = a; // reference to `a'
You cannot change what variable ra is referencing, it will be a forever.
Now, when you use references as function parameters, it means that the variable you call the function with can be changed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
void regular_function(int i)
{
i=5; // the outside `i' doesn't change, because `i' is a local copy
}
void smart_function(int &i)
{
i=5; // the outside `i' changes to 5, because `i' is a reference
}
int main()
{
int var = 10;
smart_function(var);
std::cout << var << std::endl;
}
The errors are coming from the compiler not know what a string is. Put your declare namespace std before your SAVEGAME prototype. And to get around having too many variables, if they're all the same type, then put them in an array or a vector and pass that.
Arrays are easier, but you can't change the size of them after you make them. Vectors are a memory hog, but can be resized upon will.