?
Solved

Simple database help

Posted on 2003-03-31
14
Medium Priority
?
197 Views
Last Modified: 2010-04-01
I've looked at a number of database programs and i found one that seems to work best but i'm having troubles understanding why some of the variables are used. Below is single finction from the program.  the variables that are confusing me are i, s, ss, ii, l, and w.  they apear in nearly all the functions are initialized as integers.  I could really use the help becuase i'm trying to make my own little database project.  


void AddEntry(){
     int     b = 0; // length of integer string
     i = 0; s = 0; ss = 0; ii = 0; l = 0; w  = 0;
     system("cls");

     cout<<"\n\t\t\tAdd Entry"<<endl;
     cout<<"\t\t\t==========="<<endl;

     cout<<"\n\n\tPhone #: ";
     cin>>number;

     while(b != 7){

          if(number[b] == '\0' && b != 7){cout<<"\n\n\tnumber must be at least 7 digits long...\n";AddEntry();}
          b++;}
         
     CreateFile();//creates the number file to search for
     
     ifstream find(file);

     find>>fnumber; //finds number

     while(number[l] != '\0'){//while number is not equal to end loop
          if(number[l] != fnumber[l]){ l = 0; break;}//if any char doesn't match then break
          l++;
     }
     
     find.close();

     if(number[l] == fnumber[l] && number[l] == '\0'){

     DeleteFile();//deletes the file that was created while searching for number


     cout<<"\n\n\tThis number is already on file.  Would you like to try again?\n";
     cout<<"\tIf not press n. To continue enter any letter.\n";
     cin>>no;
     if(no == 'n'){main();}else{     AddEntry();}
     
     }else{
                   
     cout<<"\n\n\tDate of Entry (NOTE: no longer than 15 characters): ";
     cin>>date;
     
     cout<<"\n\tEntry Name (NOTE: no longer than 50 charcters): ";
     cin.ignore(1);
     cin.getline(name,50);

     cout<<"\n\tAddress (NOTE:no longer than 50 characters): ";
     cin.getline(address,50);

     cout<<"\n\tCity-State-Zip (NOTE no longer than 100 characters): ";
     cin.getline(csz,100);
     
    cout<<"\n\n\tEnter Notes (notes may only be 50 characters long for each line)\n\t press(q) to quit"<<endl;
    cout<<"\n\tYou have 6 lines to use..."<<endl;
    cout<<"*************************************************************"<<endl;
     
     line = 0;

    while(line != 1){line++;//Problem with ignore(1): eats the first couple of letters in the text.
     
     cin.getline(line1,50);    
    if(line1[0] == q[0] || line1[0] == Q[0]){break;}
    cin.getline(line2,50);
    if(line2[0] == q[0] || line2[0] == Q[0]){break;}
    cin.getline(line3,50);
    if(line3[0] == q[0] || line3[0] == Q[0]){break;}
    cin.getline(line4,50);
    if(line4[0] == q[0] || line4[0] == Q[0]){break;}
    cin.getline(line5,50);
    if(line5[0] == q[0] || line5[0] == Q[0]){break;}
    cin.getline(line6,50);
    if(line6[0] == q[0] || line6[0] == Q[0]){break;}
    }
    cout<<"*************************************************************"<<endl;

    ofstream save(file);
    save<<number<<endl;
    save<<date<<endl;
     save<<name<<endl;
    save<<address<<endl;
     save<<csz<<endl;
    save<<line1<<endl;
    save<<line2<<endl;
    save<<line3<<endl;
    save<<line4<<endl;
    save<<line5<<endl;
    save<<line6<<endl;
    save.close();

    cout<<"\n\n\tALL INFOMATION HAS BEEN SAVED...."<<endl;
    getchar();

}        
     main();
}
0
Comment
Question by:dblo02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 8242940
It is rather difficult to say exactly what a, s, ss, ii, l, and w are used for due to the fact that you only posted this function and to the fact that not all of them are used inside this function.

However, these global variables appear as if they are used by the program as parameters for its various functions, instead of passing the needed values to the functions.

Secondly, I do not like the way this program is structured. I say this because none of the calls made to this function will terminate because main is called at the end instead of allowing the function to return normally. As far as I am concerned this is a subtle way in which memory leak can be generated. Each function call does not finish execution until main returns, at which point all of the remaining instances of this function will return. Until that time they waste memory, even if the amount is rather small. Like memory leaks, this is also a bad idea because you could, at least theoreticaly, crash the computer if the program made enough function calls that did not terminate, although the chances of this happening are close to zero.

>> i = 0; s = 0; ss = 0; ii = 0; l = 0; w  = 0;

Just a matter of style, it is easier to say,

i = s = ss = ii = l = w = 0;

Exceter
0
 

Author Comment

by:dblo02
ID: 8243083
Ok well here is the whole program.  yeah the prgram is defioantley not perfect but my main concern were those variables beacuse they seemed to make little sense  however when i delted them the entries wuld not save or something.  again if i did not mention it before this is a simple address book.



#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


void AddEntry();  //Function to add new entries
void ViewEntry(); //Function that outputs a selected entry
void EditEntry(); //Function that Edits a selected entry
void Edit();    
void DeleteEntry();//Function that Deletes a selected entry
void PrintEntry();//Function that outputs an entry in a text file for printing
void CreateFile();//Function that creates the file for entries
void DeleteFile();//Function that deletes created files

//-------------------------global variables--------------------------------
char del[5] = "del "; //delelet variable
char file[20];              //file variable
int ss = 0;                  //variables for directory entry
int s = 0;
int i = 0;
int ii = 0;
int w = 0, l = 0;

char command[20];
char fnumber[20];      //find number variable
char number[20];      //Phone number
char date[15];            //Holds Date inforamtion
char name[50];            //Hold name information
char address[50];      //Holds address information
char csz[100];            //Holds secondaru address information
char q[] = "q";            //Quit letter
char Q[] = "Q";            //uppercase letter to quit      
int  line = 0;
char line1[50] = "                                                 ";      //comment line
char line2[50] = "                                                 ";
char line3[50] = "                                                 ";
char line4[50] = "                                                 ";
char line5[50] = "                                                 ";
char line6[50] = "                                                 ";
char no; //Holds user choice of wanting to quit or not


//------------------------Main Function--------------------------------------
int main(){

      char select; //user task choice
      system("cls");//clears screen
      cout<<"\t\t\t  Address Book"<<endl;
      cout<<"\t\t\t==============="<<endl;

      cout<<"\n\n\t<1> Add Entry"<<endl;
      cout<<"\n\t<2> Edit Entry"<<endl;
      cout<<"\n\t<3> View Entry"<<endl;
      cout<<"\n\t<4> Delete Entry"<<endl;
      cout<<"\n\t<5> Print Entry"<<endl;
      cout<<"\n\t<0> Exit"<<endl;

      cout<<"\n\n\tEnter selection: ";
      cin>>select;

      if(select == '0'){exit(1);}  // If else staements that leads user to the function they want to go to
      else{
      if(select == '1'){AddEntry();}
      else{
      if(select == '2'){EditEntry();}
      else{
      if(select == '3'){ViewEntry();}
      else{
      if(select == '4'){DeleteEntry();}
      else{
      if(select == '5'){PrintEntry();}
      else{
      if(select == '0'){exit(1);      }
      else{      cout <<"You entered and invalid choice please try again\n\n"; //Error checking loop back to main menu
            main();}}}}}}}
      return 0;
}


//--------------------------Program Functions--------------------------------------

void AddEntry(){
      int      b = 0; // length of integer string
      i = 0; s = 0; ss = 0; ii = 0; l = 0; w  = 0;
      system("cls");

      cout<<"\n\t\t\tAdd Entry"<<endl;
      cout<<"\t\t\t==========="<<endl;

      cout<<"\n\n\tPhone #: ";
      cin>>number;

      while(b != 7){

            if(number[b] == '\0' && b != 7){cout<<"\n\n\tnumber must be at least 7 digits long...\n";AddEntry();}
            b++;}
            
      CreateFile();//creates the number file to search for
      
      ifstream find(file);

      find>>fnumber; //finds number

      while(number[l] != '\0'){//while number is not equal to end loop
            if(number[l] != fnumber[l]){ l = 0; break;}//if any char doesn't match then break
            l++;
      }
      
      find.close();

      if(number[l] == fnumber[l] && number[l] == '\0'){

      DeleteFile();//deletes the file that was created while searching for number


      cout<<"\n\n\tThis number is already on file.  Would you like to try again?\n";
      cout<<"\tIf not press n. To continue enter any letter.\n";
      cin>>no;
      if(no == 'n'){main();}else{      AddEntry();}
      
      }else{
                        
      cout<<"\n\n\tDate of Entry (NOTE: no longer than 15 characters): ";
      cin>>date;
      
      cout<<"\n\tEntry Name (NOTE: no longer than 50 charcters): ";
      cin.ignore(1);
      cin.getline(name,50);

      cout<<"\n\tAddress (NOTE:no longer than 50 characters): ";
      cin.getline(address,50);

      cout<<"\n\tCity-State-Zip (NOTE no longer than 100 characters): ";
      cin.getline(csz,100);
      
    cout<<"\n\n\tEnter Notes (notes may only be 50 characters long for each line)\n\t press(q) to quit"<<endl;
    cout<<"\n\tYou have 6 lines to use..."<<endl;
    cout<<"*************************************************************"<<endl;
      
      line = 0;

    while(line != 1){line++;//Problem with ignore(1): eats the first couple of letters in the text.
      
      cin.getline(line1,50);      
    if(line1[0] == q[0] || line1[0] == Q[0]){break;}
    cin.getline(line2,50);
    if(line2[0] == q[0] || line2[0] == Q[0]){break;}
    cin.getline(line3,50);
    if(line3[0] == q[0] || line3[0] == Q[0]){break;}
    cin.getline(line4,50);
    if(line4[0] == q[0] || line4[0] == Q[0]){break;}
    cin.getline(line5,50);
    if(line5[0] == q[0] || line5[0] == Q[0]){break;}
    cin.getline(line6,50);
    if(line6[0] == q[0] || line6[0] == Q[0]){break;}
    }
    cout<<"*************************************************************"<<endl;

    ofstream save(file);
    save<<number<<endl;
    save<<date<<endl;
      save<<name<<endl;
    save<<address<<endl;
      save<<csz<<endl;
    save<<line1<<endl;
    save<<line2<<endl;
    save<<line3<<endl;
    save<<line4<<endl;
    save<<line5<<endl;
    save<<line6<<endl;
    save.close();

    cout<<"\n\n\tALL INFOMATION HAS BEEN SAVED...."<<endl;
    getchar();

}        
      main();
}


void ViewEntry(){
            
      i = 0; s = 0; ss = 0; ii = 0; l = 0; w  = 0;

      system("cls");

      cout<<"\n\t\t\tView Entry"<<endl;
      cout<<"\t\t\t============"<<endl;

      cout<<"\n\n\tEnter Phone #: ";
      cin>>number;

      CreateFile();

      ifstream find(file);

      while(w != 1000){//loop until 1000 or E is found...

      find>>fnumber;
      
      while(number[l] != '\0'){//while number is not equal to end loop

            if(number[l] != fnumber[l]){ l = 0; break;}//if all char don't match then break out of second loop
            l++;
      }//end of second loop
      
      if(number[l] == fnumber[l] && number[l] == '\0'){break;}//if all char matches then break out of first loop
      w++;

      }//end of first loop      

      find.close();

      
      if(number[l] == fnumber[l] && number[l] == '\0'){//if number is found the process these
            //functions..


      CreateFile();

      ifstream get(file);
      get>>number;
      get>>date;
      get.ignore(1);
      get.getline(name,50);
      get.getline(address,50);
      get.getline(csz,100);
      
      get.getline(line1,50);
      get.getline(line2,50);
      get.getline(line3,50);
      get.getline(line4,50);
      get.getline(line5,50);
      get.getline(line6,50);
      get.close();

      cout<<"\n\n\tDate: "<<date<<endl;
      cout<<"\n\tFull Name: "<<name<<endl;
      cout<<"\n\tAddress: "<<address<<endl;
      cout<<"\n\tCity-State-Zip: "<<csz<<endl;      
      cout<<"\n\t***************************************************************"<<endl;
      cout<<"\n\t"<<line1<<endl;
      cout<<"\t"<<line2<<endl;
      cout<<"\t"<<line3<<endl;
      cout<<"\t"<<line4<<endl;
      cout<<"\t"<<line5<<endl;
      cout<<"\t"<<line6<<endl;

      cout<<"\n\n\tPress any key to return..."<<endl;
      getchar();
      main();

      }else{

      DeleteFile();

      cout<<"\n\n\tThis number is not on file.  Would you like to try again?\n";
      cout<<"\tIf not press n. To continue enter any letter.\n";
      cin>>no;
      if(no == 'n'){main();}else{      ViewEntry();}
      }

}


void EditEntry(){

      i = 0; s = 0; ss = 0; ii = 0; l = 0; w = 0;

      system("cls");


      cout<<"\n\t\t\tEdit Entry"<<endl;
      cout<<"\t\t\t==========="<<endl;

      cout<<"\n\n\tEnter Phone #: ";
      cin>>number;

      CreateFile();

      ifstream find(file);

      while(w != 1000){//loop until 1000 or Entry is found...

      find>>fnumber;
      
      while(number[l] != '\0'){//while number is not equal to end loop

            if(number[l] != fnumber[l]){ l = 0; break;}//if all char don't match then break out of second loop
            l++;
      }//end of second loop
      
      if(number[l] == fnumber[l] && number[l] == '\0'){break;}//if all char matches then break out of first loop
      w++;

      }//end of first loop      
      find.close();
      
      if(number[l] == fnumber[l] && number[l] == '\0'){//if number is found the process these
            //functions..
      
      CreateFile();


      Edit();//if all is good then process function..
      
      }else{

      DeleteFile();

      cout<<"\n\n\tThis number is not on file.  Would you like to try again?\n";
      cout<<"\tIf not press n. To continue enter any letter.\n";
      cin>>no;
      if(no == 'n'){main();}else{      EditEntry();}}
            
}


void Edit(){

      char edit;
      
      system("cls");

      ifstream get(file);
      get>>number;
      get>>date;
      get.ignore(1);
      get.getline(name,50);
      get.getline(address,50);
      get.getline(csz,100);
      get.ignore(1);
      get.getline(line1,50);
      get.getline(line2,50);
      get.getline(line3,50);
      get.getline(line4,50);
      get.getline(line5,50);
      get.getline(line6,50);
      get.close();

      cout<<"\n\n\t<A> Date: "<<date<<endl;
      cout<<"\n\t<B> Entry Name: "<<name<<endl;
      cout<<"\n\t<C> Address: "<<address<<endl;
      cout<<"\n\t<D> City-State-Zip: "<<csz<<endl;      
      cout<<"\n\t***************************************************************"<<endl;
      cout<<"\n\t<E> "<<line1<<endl;
      cout<<"\t<F> "<<line2<<endl;
      cout<<"\t<G> "<<line3<<endl;
      cout<<"\t<H> "<<line4<<endl;
      cout<<"\t<I> "<<line5<<endl;
      cout<<"\t<J> "<<line6<<endl;
      

      cout<<"\n\tEnter a line to edit or <0> to exit: ";      
      cin>>edit;
      
      system("cls");
      cout<<"\n\t\t\tEdit number"<<endl;
      cout<<"\t\t\t============"<<endl;

      if(edit == 'a'){cout<<"\n\n\tEdit Date: <"            <<date<<"> ";            cin>>date;}else{
      if(edit == 'b'){cout<<"\n\n\tEdit Name: <"            <<name<<"> ";            cin.ignore(1);cin.getline(name,50);}else{
      if(edit == 'c'){cout<<"\n\n\tEdit Address: <"      <<address<<"> ";      cin.ignore(1);cin.getline(address,50);}else{
      if(edit == 'd'){cout<<"\n\n\tEdit City-State-Zip: <"<<csz<<"> ";      cin.ignore(1);cin.getline(csz,100);}else{      
      if(edit == 'e'){cout<<"\n\n\tEdit Line1: <"            <<line1<<"> ";            cin.ignore(1);cin.getline(line1,50);}else{
      if(edit == 'f'){cout<<"\n\n\tEdit Line2: <"            <<line2<<"> ";            cin.ignore(1);cin.getline(line2,50);}else{
      if(edit == 'g'){cout<<"\n\n\tEdit Line3: <"            <<line3<<"> ";            cin.ignore(1);cin.getline(line3,50);}else{
      if(edit == 'h'){cout<<"\n\n\tEdit Line4: <"            <<line4<<"> ";            cin.ignore(1);cin.getline(line4,50);}else{
      if(edit == 'i'){cout<<"\n\n\tEdit Line5: <"            <<line5<<"> ";            cin.ignore(1);cin.getline(line5,50);}else{
      if(edit == 'j'){cout<<"\n\n\tEdit Line6: <"            <<line6<<"> ";            cin.ignore(1);cin.getline(line6,50);}else{
      if(edit == '0'){main();}else{      Edit();}}}}}}}}}}}

      ofstream save(file);
    save<<number<<endl;
    save<<date<<endl;
      save<<name<<endl;
    save<<address<<endl;
    save<<csz<<endl;    
    save<<line1<<endl;
    save<<line2<<endl;
    save<<line3<<endl;
    save<<line4<<endl;
    save<<line5<<endl;
    save<<line6<<endl;
    save.close();

      Edit();
}

void DeleteEntry(){

      i = 0; s = 0; ss = 0; ii = 0; l = 0; w = 0;
      
      system("cls");

      cout<<"\n\t\t\tDelete number"<<endl;
      cout<<"\t\t\t============="<<endl;

      cout<<"\n\n\tEnter number #: ";
      cin>>number;

      CreateFile();

      ifstream find(file);

      while(w != 1000){//loop until 1000 or Entry is found...

      find>>fnumber;
      
      while(number[l] != '\0'){//while number is not equal to end loop

            if(number[l] != fnumber[l]){ l = 0; break;}//if all char don't match then break out of second loop
            l++;
      }//end of second loop
      
      if(number[l] == fnumber[l] && number[l] == '\0'){break;}//if all char matches then break out of first loop
      w++;

      }//end of first loop      
      find.close();
      
      if(number[l] == fnumber[l] && number[l] == '\0'){//if number is found the process these
            //functions..
      
      DeleteFile();

      cout<<"\n\n\tThis number has successfully been deleted..."<<endl;
      getchar();
      main();

      }else{
      
      DeleteFile();

      cout<<"\n\n\tThis number is not on file.  Would you like to try again?\n";
      cout<<"\tIf not press n. To continue enter any letter.\n";
      cin>>no;
      if(no == 'n'){main();}else{      DeleteEntry();}}

}


void PrintEntry(){      

      i = 0; s = 0; ss = 0; ii = 0; l = 0; w = 0;

      system("cls");

      cout<<"\n\t\t\tPrint number"<<endl;
      cout<<"\t\t\t============"<<endl;

      cout<<"\n\n\tEnter number #: ";
      cin>>number;
      

      CreateFile();

      ifstream find(file);

      while(w != 1000){//loop until 1000 or Entry is found...

      find>>fnumber;
      
      while(number[l] != '\0'){//while number is not equal to end loop

            if(number[l] != fnumber[l]){ l = 0; break;}//if all char don't match then break out of second loop
            l++;
      }//end of second loop
      
      if(number[l] == fnumber[l] && number[l] == '\0'){break;}//if all char matches then break out of first loop
      w++;

      }//end of first loop      
      find.close();
      
      if(number[l] == fnumber[l] && number[l] == '\0'){//if number is found then process these

//this gets all the number information needed for printing...            

      ifstream get(file);
      get>>number;
      get>>date;            
      get.ignore(1);
      get.getline(name,50);
      get.getline(address,50);
      get.getline(csz,100);      
      get.close();

//outputing all information to a print file for printing...

      ofstream save("number.txt");
      save<<"\n\n\n\n\tName:  "<<name<<endl;
      save<<"\tAddress:  "<<address<<endl;
      save<<"\tCit, State, Zipcode:  "<<csz<<endl;      
      save<<"\tnumber #: "<<number<<endl;
      save.close();

      system("number.txt");      
      main();

      }else{

      DeleteFile();

      cout<<"\n\n\tThis number is not on file.  Would you like to try again?\n";
      cout<<"\tIf not press n. To continue enter any letter.\n";
      cin>>no;
      if(no == 'n'){main();}else{      PrintEntry();}}

}

//===============These are special functions that create and delete files=================
void CreateFile(){

      strcpy(file,number);
      strcat(file,".txt");

      
}

void DeleteFile(){

      strcpy(command, del);

      strcat(command,file);

      system(command);
}
0
 
LVL 10

Expert Comment

by:substand
ID: 8243159
it looks like they are just used as indexes to arrays, and counters.

there is no real reason they need to be global though, as this just wastes memory.

unless they are constantly used, i suppose then it might save time since they don't have to be created and destroyed.


l is used as an index for array manipulation
w is used for a counter

ii, ss, s, and i are not used and are therefore useless.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Exceter
ID: 8243180
>> ...yeah the prgram is defioantley not perfect...

It certainly isn't. :-)

Well, it appears that the variables are used for the follwing purposes,

ss - Nothing
ii - Nothing
s  - Nothing
i  - Nothing
w  - Used as a loop counter
l  - Used as an index counter to loop through strings, like the user entered phone number for example.

Exceter
0
 
LVL 10

Expert Comment

by:substand
ID: 8243283
hehe, exceter, we posted close to each other and you said the same thing i did =)
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8249280
>> hehe, exceter, we posted close to each other and you said the same thing i did =)

That's nothing, I've had posts that were less than a minute apart from another expert's and both of us had almost the same content as well. :-)

Exceter
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8252068
If you ever wonder what a variable does, then comment it out and rebuild.  The compiler will show you each line in which the variable is used.

-- Dan
0
 

Author Comment

by:dblo02
ID: 8258323
Ok so i went back and as i thought before ss ii s and i are used for nothing.  but the other two ( w , l ) are
so far what i've learned from feedback is:

w  - Used as a loop counter
l  - Used as an index counter to loop through strings, like the user entered phone number for example.

ok so now the question is what is a loop counter?  does it limit the number of times it loops or what?

and what is an index counter?

i'm trying to figure out a simple way of explaining it.

0
 
LVL 8

Expert Comment

by:Exceter
ID: 8258454
An index counter is simply an offset that determines the position in the array. For example,

char name[] = "Exceter";

This is an array of chars. In C/C++, with the exception of classes, strings are stored in arrays which are represented by a pointer to the first element in the array. Therefore, name is a pointer to the first element in the array. Each element in the array can be accessed via an index value which is an offset from the first element in the array. So, if you wanted to access the second element in the array you could say,

cout << name[1] << endl; // You use an index value of 1 instead of 2 because array indexes are numbered starting at 0 instead of 1

Since the index is simply an offset from the first element in the array that name[1] is equivalent to,

*( name + 1 )

Therefore, the following is legal as well,

cout << 1[name] << endl;

This is so because the addition of 1 to name and name to 1 produces the same result. After all, what is the difference between the following two expressions?

cout << *( name + 1 ) << endl;
cout << *( 1 + name ) << endl;

Therefore, it also follows that the following statements are also equivalent,

cout << name << endl;
cout << &name[0] << endl;
cout << &0[name] << endl;

In short, an idex is simply an offset from the first element in the array.

A loop counter is simply a variable that has a value added to it each time a loop iterates. For example,

for( int x = 0; x < 5; x++ )
    cout << x << endl;

In this instance the counter variable x gets 1 added to it after each iteration of the loop. Usually this value is one but that is not required. For example,

for( int x = 0; x < 10; x += 2 )
    cout << x << endl;

You don't necessarily have to use addition here either because the expression inside the for loop is evaluated you can pretty much do whatever you want here. However, that is getting a little outside the realm of a counter. :-)

>> while(w != 1000)

Anyway, your program uses w to keep track of how many time this while loop has looped. w it set to 0 at the top of the function and as long as it does not equal 1000 the while loop will continue to iterate. Since w is incremented at the bottom of this loop it will iterate 1000 times. Notice that since it started counting from 0 the following would do the same thing,

while( w < 1000 )
{
    ...
    w++;
}

In general, a loop counter is used to keep track of how many times the loop has iterated.

Exceter
0
 
LVL 8

Accepted Solution

by:
Exceter earned 400 total points
ID: 8258492
>> ok so now the question is what is a loop counter?  does it limit the number of times it loops or what?

As a general rule a loop counter does limit the number of times a loop will iterate but there can be exceptions. For example,

int array = { 5,4,3,2,1,0 };

for( int x = 0; array[x] != 0; x++ )
    cout << array[x] << endl;

You see that the loop counter keeps track of how many times the loop iterates but it does not have anything to do with the loop's break condition. If the loop's break condition was not met by the data in the array the loop would continue looping. This, of course, would give you problems because this loop uses x as an index value. So if there wasn't a zero in the array it would continue looping  and the index would overset the bounds of the array. However, in general, a loop counter does limit the number of times the loop iterates.

>> and what is an index counter?

Simply put, an index counter keeps track of the position in the array. As in the following example,

char s[] = "string";

for( int x = 0; x < strlen(s); x++ )
    cout << s[x] << endl;

This will display the string one character at a time. Notice that x is used as an index that keeps track of which element in the char array to display. An index counter simply keeps track of the current position in the array.

Exceter
0
 
LVL 10

Expert Comment

by:substand
ID: 8258706
couldn't have said it better myself.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8258738
:-)
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9566571
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: Exceter

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question