954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

counter in a for loop for int type variable

Hi there guys I am looking for a way on how I can increment an object from a structure which is an integer type. the string types I have Increment well but the int types do not. it says int[int] is invalid for array subscript. The red font on my code indicates the line I am having trouble incrementing with. it won't allow me to put [i] beside the object like this level.year[i] and amount.units[i]

My goal is to arrange a record for a file stream like this:

student no[1] // all
firstname[1] // of these
lastname[1] // increment well because they are of
course[1] // type strings.
year[1] // year cannot be incremented because it is int
units[1] // units cannot also be incremented because it is int


student no[2]
firstname[2]
lastname[2]
course[2]
year[2] //the value inputted here will be the ones outputted to the text file because it's the last that was entered
units[2] //the value inputted here will also be the ones outputted to the text file because it's the last that was entered

that was just an example. they store two different set of files but the year and units remain the same. Any ways on how I can increment them also? Here's my code:

#include<iostream>
#include<string>
#include<windows.h>
#include<fstream>
#include <iomanip>
#include<cctype>
#include<cstdlib>

using namespace std;

struct TF
{int units, punits, misc;};

struct studre
{
       string Sno;
       string Fname;
       string Mname;
       string Sname;
       string course;
       int year;
       int fees;
      
      
};

void ClearScreen();
void input();
int main()
{   
    int total;
    
    studre level;
    int x;
    studre record;
    TF amount;
    amount.punits=800;
    amount.misc=2000;
    double tuition;
    ofstream output;

    while(true)
    {
    cout<<"Enter number of students that will enroll (Max. of 5 students only)"<<endl;
    cin>>x;

try
{
if(x<1||x>5)
{
throw x;
}

}
catch(int a)
{
ClearScreen();
cout<<"Invalid input. Please try again."<<endl;
continue;
}





for (int i=0;i<x;i++)

    {
        cin.ignore();
        cout<<endl;
        cout<<"Student Number:"<<"["<<i+1<<"]:"<<endl;
        getline(cin,record.Sno);
         
        cout<<"Enter First Name:"<<"["<<i+1<<"]:"<<endl;
        getline(cin,record.Fname);
        
        cout<<"Enter Middle Name:"<<"["<<i+1<<"]:"<<endl;
        getline(cin,record.Mname);
       
        cout<<"Enter Last Name:"<<"["<<i+1<<"]:"<<endl;
        getline(cin,record.Sname);
       
        cout<<"Enter Course:"<<"["<<i+1<<"]:"<<endl;
        getline(cin,record.course); 
       
        cout<<"Enter Year:"<<endl;
        cin>>level.year;
        {
        cout<<"Enter Units:"<<endl;
        cin>>amount.units;
        total=amount.units*x;
        }
        ClearScreen();
        }

for (int i=0;i<x;i++)
{
output.open("record.txt", ios::app);

output<<"Student Number:"<<record.Sno[i]<<endl;
output<<"First Name:"<<record.Fname[i]<<endl;
output<<"Middle Name:"<<record.Mname[i]<<endl;
output<<"Last Name:"<<record.Sname[i]<<endl;
output<<"Course:"<<record.course[i]<<endl;
output<<"Enter Year:"<<level.year[i]<<endl; //[i] here is invalid
output<<"Enter Units:"<<amount.units[i]<<endl; //[i] here is invalid
output<<endl;
output.close();
}
system ("pause");
return 0;
    
}


}

void ClearScreen()
  {
  HANDLE hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD count;
  DWORD cellCount;
  COORD homeCoords = { 0, 0 };
  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi ))
  return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;
  SetConsoleCursorPosition( hStdOut, homeCoords );
  }
Lightning03
Junior Poster in Training
92 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

level.year is not an array. So you cannot give output<string types I have Increment well but the int types do not. it says int[int] is invalid for array subscript.
Increment is different( a++ or ++a,--a,a--).

Arbus
Practically a Master Poster
615 posts since Dec 2010
Reputation Points: 45
Solved Threads: 31
 

Mostly people would say it's bad practice to use exceptions like you are, but for such a simple program it won't matter.

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 
Mostly people would say it's bad practice to use exceptions like you are, but for such a simple program it won't matter.


FYI, most people would have no idea what you mean because if they use it wrong they obviously don't know any better. Without explanation you are just adding confusion.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: