I have writen a program that includes ALOT of code. I update this code and work on it everyday, and with so many lines, I get confussed.

a big chunk of my code goes like this:

cout << "\nNext:\n";
           cin >> string2;
           if (string2 == "end")
           {
                       cout<< "ok, you are now finished entering #'s\n\n";
                       stoppoint = 1;
                       GetResults();
                       }
                       else 
                       { }
                       
                           //next     
                              
                      cout << "\nNext:\n";
           cin >> string3;
           if (string3 == "end")
           {
                       cout<< "ok, you are now finished entering #'s\n\n";
                       stoppoint = 2;
                       GetResults();
                       }
                       else 
                       {  }

and this continues for a long time, each just simply stating a new string.
I want to make a header file, and place this code inside. then, instead of having all this in my big code, I can just type a line or 2 in my void start section like "insert header file here"

So can this be done? many thanks.

don't. Head files only contains class declarations, defines, typedefs, and function prototypes.

Have you notices that your program contains a lot of duplicate code? You can use a loop and arrrays to chop down the size of your program to just a few lines

string answer;
int stopped = 0;
vector<string> aList;
for(int i = 0; stopped == 0 && i < 10; i++)
{
    cout << "Enter string #" << i+1 << "\n";
    getline(cin, answer);
    if( answer == "done")
    {
        stopped = i+1;
        cout<< "ok, you are now finished entering #'s\n\n";
    }
    else
    {
        aList.push_back(answer);
    }
}

don't. Head files only contains class declarations, defines, typedefs, and function prototypes.

Have you notices that your program contains a lot of duplicate code? You can use a loop and arrrays to chop down the size of your program to just a few lines

string answer;
int stopped = 0;
vector<string> aList;
for(int i = 0; stopped == 0 && i < 10; i++)
{
    cout << "Enter string #" << i+1 << "\n";
    getline(cin, answer);
    if( answer == "done")
    {
        stopped = i+1;
        cout<< "ok, you are now finished entering #'s\n\n";
    }
    else
    {
        aList.push_back(answer);
    }
}

thanks.
but also, I need that code there to be able to track what you have typed. for example, first I type 34, then 23. string1=34, and string2=23. It doesnt look like your code would do that.
oh, and my complier sayes "vector undeclared"

>>and my complier sayes "vector undeclared"
you have to include <vector> header file

You don't have strings named string1, string2, string3, ... What you have that I posted is an array of strings, so if you want to reference the first string you would use aList[0], the second string is aList[1] etc. Makes programming life a LOT simpler if you learn arrays.

>>and my complier sayes "vector undeclared"
you have to include <vector> header file

You don't have strings named string1, string2, string3, ... What you have that I posted is an array of strings, so if you want to reference the first string you would use aList[0], the second string is aList[1] etc. Makes programming life a LOT simpler if you learn arrays.

ok. how would i refer to the first # entered in code form? and also, when I am displaying them, how would i say "if 7 numbers were not entered, do not try to display past 7"?

The vector class keeps track of how many numbers you put into the array. So if you entered only 5 numbers, then aList.size() will be 5. For example, the following code will display the values of all the numbers in the vector regardless of its size.

for(int i = 0; i < aList.size(); i++)
{
   cout << aList[i] << "\n";
}

You put prototypes of the functions in the headers, then place the code in individual C/Cpp files that you add to the project(assuming you have an IDE) which are compiled as object files that get linked.
This also saves massive amounts of time compiling large projects, in which you only updated one C file; since, it only needs to recompile that C file then!

This article has been dead for over six months. Start a new discussion instead.