Help with my programme

Hi,

I am completely new to c++ and I am tyring to build my programm but i get the error C2109: subscript requires array or pointer type on lines 136 and 230 .Please can someone help me :)

// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>

using namespace std;

#include<stdlib.h>
#define NLINES 11


#define HSIZE 50
#define VSIZE 35







void Welcome(); //this is a function declaration a.k.a. prototype
void Goodbye();
void ReadFromFile();
void SaveToFile();
void InsertLine();
void SwapTwoLines();
void DisplayBuffer();
void DeleteLine();
void RenameStudent();
int Menu();











char Register [NLINES] = {0};
char szLineNames[NLINES][HSIZE] = {0};



void main()
{
bool bExit =false;

Welcome();

do

{

switch (Menu())

{

case 1:ReadFromFile();break;

case 2:DisplayBuffer();break;

case 3:InsertLine();break;

case 4:DeleteLine();break;

case 5:SwapTwoLines ();break;

case 6:RenameStudent();break;

case 7:SaveToFile();break;

case 8:bExit=true ;break;

}


}

while (bExit!=true); //Also can do false

Goodbye();

system("pause");


}


void Welcome()
{
cout << "Welcome to our Line Editor program!"<< endl;
}






void Goodbye()
{
cout << "Goodbye, thanks for using the line editor programme" << endl;
}






void SaveToFile()
{
ofstream fout;
int i=0;

fout.open("QUOTES.txt");


if(fout.is_open())
{
for(i=0; i<NLINES; i++)
{
if(strlen(szLineNames[i]) >0)
fout << setw(20) << szLineNames[i] << ": " << setw(5) << Register[i][0] <<endl;
}

fout.close();
}


system("pause");


}


void ReadFromFile()
{
ifstream fin;
char buffer[100]={0};
char buffer1[100]={0};
int iCounter = 0;

fin.open("QUOTES.txt");


if(fin.is_open())
{
while(!fin.eof())
{
//fin.getline(buffer,50,':').getline(buffer1,100);
fin.getline(szLineNames[iCounter],sizeof(szLineNames[iCounter]),':').getline(buffer1,100);
cout << szLineNames[iCounter] << endl;

iCounter++;


}
fin.close();
}
else
cerr << "unable to open file QUOTES.txt" << endl;



}


void InsertLine() //DO InsertLine
{
char BufferArray[NLINES][HSIZE]={0}; // INITIALISE BufferArray 25 by 50 with 0
int iAfter=0; // INITIALISE iAfter with 0
int iCounter=0;

cout << "After which line would you like to insert? "; // PROMPT "After which line would you like to insert? "
cin >> iAfter; // INPUT into iAfter

fflush(stdin); //Removes any rememberence from the stream buffer - only required in windows environment

for(iCounter = iAfter; iCounter < NLINES; iCounter++) // FOR iCounter from iAfter to NLINES
{ // LOOP
strcpy(BufferArray[iCounter+1], szLineNames[iCounter]); // COPY szLineNames[iCounter] to BufferArray[iCounter]
} // END LOOP

cout << "Please enter another line name: "; // PROMPT "Please enter another line name: "
cin.getline (szLineNames[iAfter],sizeof(szLineNames[iAfter])); // INPUT into szLineNames[iAfter+1]
for(iCounter = iAfter+1; iCounter < NLINES; iCounter++) // FOR iCounter from iAfter to NLINES-1
{ // LOOP
strcpy(szLineNames[iCounter],BufferArray[iCounter]); // COPY BufferArray[iCounter] to szLineNames[iCounter+1]
} // END LOOP
} //END DO InsertLine



void SwapTwoLines() //DOSwapTwoLines
{



int iLineNumber1=0, iLineNumber2=0; //SET LineNumber1to 0 , LineNumber2 to 0, Buffer to 0
char szBuffer[HSIZE]={0};


cout << "Please input the two line numbers to be swapped:"; //PROMPT "Please input the two line numbers to be swapped:"

cin >> iLineNumber1 >> iLineNumber2; //INPUT LineNumber1and LineNumber2

iLineNumber1--;
iLineNumber2--;

strcpy(szBuffer, szLineNames[iLineNumber1]); //COPY szLineNames[LineNumber1] to Buffer

strcpy(szLineNames[iLineNumber1], szLineNames[iLineNumber2]);//COPY szLineNames[LineNumber2] to szLineNames[LineNumber1]

strcpy(szLineNames[iLineNumber2], szBuffer); //COPY Buffer to szLineNames[LineNumber2]

} //ENDDOSwapTwoLines




void DisplayBuffer()
{
for(int i=0; i<NLINES; i++)

{
if(strlen(szLineNames[i]) >0)
cout << setw(20) << szLineNames[i] << ": " << setw(5) << Register[i][0] << endl;
}

}

void DeleteLine() // Do Delete line
{
int iLineNumber = 0; // INITIALISE iLineNumber with 0
cout << "Please enter a line number you would like to delete "; // PROMPT "Please enter a line number you would like to delete "

cin >> iLineNumber; // INPUT into iLineNumber

strcpy(szLineNames[iLineNumber-1],""); // COPY "" to szLineNames


}



void RenameLine() //DO RenameLine
{


char szToBeReplaced[HSIZE] = {0}; //INITIALISE szToBeReplaced with 0
char szReplacement[HSIZE] = {0}; //INITIALISE szReplacement with 0
int i=0;
int iTargetLine = 0;
int iLength = 0; //INITIALISE i with 0 //INITIALISE iTargetLine with 0
char * pszPosition = 0; //INITIALISE Position with 0
char szRemainder [HSIZE] = {0}; //INITIALISE Remainder with 0
cout << "Please enter a name to be changed"; // PROMPT "Please enter a name to be changed"

cin.getline(szToBeReplaced,sizeof(szToBeReplaced)); // INPUT into szToBeReplaced

cout << "Please enter the new name"; // PROMPT "Please enter the new name"

cin.getline(szReplacement,sizeof(szReplacement)); // INPUT into szReplacement

for (i=0; i<NLINES;i++) //FOR I from 0 to NLINES

{ //LOOP
if(pszPosition = strstr(szLineNames[i],szToBeReplaced)) // IF szToBeReplaced is found in szLineNames[i]
{ //THEN
// SAVE the position of szToBeReplaced within szLineNames[i] as Position
iTargetLine = i; // COPY I to iTargetLine (first occurrence of)
break; // EXIT from the loop
} //END IF
}
//ENDLOOP
iLength = strlen(szToBeReplaced); //CALCULATE the length of szToBeReplaced
//SAVE it to iLength

strcpy(szRemainder, pszPosition+iLength); //COPY Position+iLength to szRemainder
strcpy(pszPosition, szReplacement); //COPY szReplacement to Position
strcat(szLineNames[iTargetLine], szRemainder); //CONCATENATE szRemainder to szLineNames[iTargetLine]

//ENDDO RenameStudent


}



int Menu()
{
int iMenuSelection = 0;
do
{
cout << "Line Editor Programme Options" << endl << endl;
cout << "1: Load File" << endl;
cout << "2: Display Buffer" << endl;
cout << "3: Insert Line" << endl;
cout << "4: Delete Line" << endl;
cout << "5: Swap Two Lines" << endl;
cout << "6: Rename Line (String Sub)" << endl;
cout << "7: Save File" << endl;
cout << "8: Exit from Prgramme" << endl << endl;
cout << "Please enter option 1-8: ";
cin >> iMenuSelection;
fflush(stdin);
}
while (!(iMenuSelection >0 && iMenuSelection <10));

return iMenuSelection;

return 0;

}
can you just post the codes around those lines? Or just the codes where you think problem may lie? nobody will look through this...you have not even used the code tags to format your post.
This is getting SERIOUSLY annoying, 90% of new posts don't use the [ code ] tags and it's impossible to read. Edit your post and use [ code ] and [/code] tags at the start and end of your code so that it's readable. Otherwise no one is going to take the time to look through that sloppy unindented code.
Last edited on
Hi , many thanks for your response, I am completely new to all this, the errrs are around these parts of the code

void SaveToFile()
{
ofstream fout;
int i=0;

fout.open("QUOTES.txt");


if(fout.is_open())
{
for(i=0; i<NLINES; i++)
{
if(strlen(szLineNames[i]) >0)
fout << setw(20) << szLineNames[i] << ": " << setw(5) << Register[i][0] <<endl;
}

fout.close();
}

and

void DisplayBuffer()
{
for(int i=0; i<NLINES; i++)

{
if(strlen(szLineNames[i]) >0)
cout << setw(20) << szLineNames[i] << ": " << setw(5) << Register[i][0] << endl;
}

}

When I say new I mean new - how do I use code tages on posts ? Sorry to be a nightmare !

Regards

Hayley
USE CODE TAGS!!! Like this...
[ code ] (without the spaces)
int main()
{
//Code here....
}
[/code]
Last edited on
You say use code tags, its not like I am doing this on purpose, I have explained that I am new, I was advised to use this forum for help and advise as I am a complete beginner, all I wanted was help. I cannot help being new can I, the whole reason I am on here is to get better.


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

void SaveToFile()
  {
      ofstream fout;
      int i=0;

       fout.open("QUOTES.txt");


      if(fout.is_open())
           {
               for(i=0; i<NLINES; i++)

                    {
                          if(strlen(szLineNames[i]) >0)
                         fout << setw(20) << szLineNames[i] << ": " << setw(5) << Register[i][0] <<endl;
                    }

                fout.close();
           }
  }


1
2
3
4
5
6
7
8
9
10
11
12
13

void DisplayBuffer()

          {
                for(int i=0; i<NLINES; i++)

                     {
                        if(strlen(szLineNames[i]) >0)
                        cout << setw(20) << szLineNames[i] << ": " << setw(5) << Register[i][0] << endl;
                     }

          }
Just a tip: You included <string> but didn't actually use the string type. I think using it could make life easier for you.
Also, I think the problem is with Register. I only see you initialized it with char Register [NLINES] = {0}; and accessed it in your functions (where do you fill it with something you can actually read?).
Many thanks, I will have a look and amend :)
I understand that hb84 but I asked you to use code tags and you replied with more code and no code tags. Also you have to understand that people come to this message board on their free time to help others (and get help) and CONSTANTLY every day people are making new posts without using code tags.

It is very, VERY hard to read code like that and if these new guys would just read 2-3 posts before asking for help they would see that it's necessary to use code tags when requesting help. I didn't use all caps because I was mad, I just said it before and you obviously didn't notice so I wanted to be more clear. (;
Topic archived. No new replies allowed.