Click here to Skip to main content
16,018,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
  1  #include<iostream>
  2  #include<fstream>
  3  using namespace std;
  4  class cars{
  5          public:
  6          string make;
  7          string model;
  8          int year;
  9          bool transmission;
 10          int price;
 11          void display()
 12          {
 13                  cout<<"Make "<<make<<endl;
 14                  cout<<"Model "<<model<<endl;
 15                  cout<<"year "<<year<<endl;
 16                  if(transmission)
 17                  cout<<"transmission "<<"Mannual"<<endl;
 18                  else
 19                  cout<<"transmission "<<"Automatic"<<endl;
 20                  cout<<"price "<<price<<endl;
 21          }
 22          
 23  };
 24  int main()
 25  {
 26          cars arr[3];
 27          ofstream filin;
 28          for(int i=0;i<3;i++)
 29          {
 30                  cout<<"Enter the details of car "<< i+1<<endl;
 31                  cout<<"enter make of car"<<endl;
 32                  cin>>arr[i].make;
 33                  cout<<"enter model of car"<<endl;
 34                  cin>>arr[i].model;
 35                  cout<<"enter year of car"<<endl;
 36                  cin>>arr[i].year;
 37                  cout<<"enter transmission of car"<<endl;
 38                  cout<<"1. Mannual"<<endl;
 39                  cout<<"2. Automatic"<<endl;
 40                  int t;
 41                  cin>>t;
 42                  if(t==1)arr[i].transmission=true;
 43                  else arr[i].transmission=false;
 44                  cout<<"enter price of car"<<endl;
 45                  cin>>arr[i].price;
 46          }
 47          cout<<"1. See car details"<<endl;
 48          cout<<"2. Update car details"<<endl;
 49          cout<<"3. print all car details"<<endl;
 50          cout<<"4. Save cars"<<endl;
 51          int q;
 52          cin>>q;
 53          if(q==1)
 54          {
 55                  cout<<"Enter number of car to search 1 or 2 or 3"<<endl;
 56                  int model1;
 57                  cin>>model1;
 58                  if(model1<3)
 59                  arr[model1-1].display();// 0 based indexing
 60                  else 
 61                  cout<<"Wrong number"<<endl;
 62          }
 63          if(q==2)
 64          {
 65                  cout<<"Enter number of car to update 1 or 2 or 3"<<endl;
 66                  int model1;
 67                  cin>>model1;
 68                  model1--; // 0 based indexing
 69                  cout<<"enter make of car"<<endl;
 70                  cin>>arr[model1].make;
 71                  cout<<"enter model of car"<<endl;
 72                  cin>>arr[model1].model;
 73                  cout<<"enter year of car"<<endl;
 74                  cin>>arr[i].year;
 75                  cout<<"enter transmission of car"<<endl;
 76                  cout<<"1. Mannual"<<endl;
 77                  cout<<"2. Automatic"<<endl;
 78                  int t;
 79                  cin>>t;
 80                  if(t==1)arr[i].transmission=true;
 81                  else arr[i].transmission=false;
 82                  cout<<"enter price of car"<<endl;
 83                  cin>>arr[i].price;
 84          }
 85          cout<<"1. See car details"<<endl;
 86          cout<<"2. Update car details"<<endl;
 87          cout<<"3. print all car details"<<endl;
 88          cout<<"4. Save cars"<<endl;
 89       
 90          cin>>q;
 91          if(q==1)
 92          {
 93                  cout<<"Enter number of car to search 1 or 2 or 3"<<endl;
 94                  int model1;
 95                  cin>>model1;
 96                  if(model1<3)
 97                  arr[model1-1].display();// 0 based indexing
 98                  else 
 99                  cout<<"Wrong number"<<endl;
100          }
101          if(q==2)
102          {
103                  cout<<"Enter number of car to update 1 or 2 or 3"<<endl;
104                  int model1;
105                  cin>>model1;
106                  model1--; // 0 based indexing
107                  cout<<"enter make of car"<<endl;
108                  cin>>arr[model1].make;
109                  cout<<"enter model of car"<<endl;
110                  cin>>arr[model1].model;
111                  cout<<"enter year of car"<<endl;
112                  cin>>arr[model1].year;
113                  cout<<"enter transmission of car"<<endl;
114                  cout<<"1. Mannual"<<endl;
115                  cout<<"2. Automatic"<<endl;
116                  int t;
117                  cin>>t;
118                  if(t==1)arr[model1].transmission=true;
119                  else arr[model1].transmission=false;
120                  cout<<"enter price of car"<<endl;
121                  cin>>arr[model1].price;
122          }
123          if(q==3)
124          {
125                  for(int i=0;i<3;i++)
126                  {
127                          arr[i].display();
128                  }
129          }
130          else
131          {
132                  filin.open("text.txt", ios::app);
133                  for(int i=0;i<3;i++)
134                  {
135                          filin.write((char*)&arr[i], sizeof(arr[i]));// writing into file
136                  }
137                  filin.close();  
138          }
139  }


What I have tried:

it says
identifier "i" is undefined


cin>>arr[i].year;
              cout<<"enter transmission of car"<<endl;
              cout<<"1. Mannual"<<endl;
Posted
Updated 31-Aug-22 6:40am
v2
Comments
Greg Utas 31-Aug-22 12:41pm    
It looks like it should be model1 instead of i.
BernardIE5317 8-Sep-22 6:23am    
May I suggest in future as case may need be specifying the line number of the offending code . It is quite kind of these fine gentlemen to search through your code to locate same w/o such aid . May I also suggest in future as case may need be providing code which has been minimized . Doing so aids the kind reviewers here but also provides oneself w/ opportunity of discovery of error and solution . - Cheerios

1 solution

In lines 80 to 83 you are trying to refer to arr with the index variable i (arr[i]). But i does not exist at that point, its scope extends only between lines 28 and 46.
 
Share this answer
 
Comments
KarstenK 31-Aug-22 13:05pm    
You are right and it is standard. But some compilers are "allowing" this.
But using it is silly.
CPallini 31-Aug-22 16:14pm    
Really? What compiler?
KarstenK 1-Sep-22 5:17am    
I remember that Visual Studio was allowing it in earlier versions.
Richard MacCutchan 1-Sep-22 5:26am    
1. Visual Studio is not a compiler.
2. No version of the Microsoft C/C++ compilers since the mid 1980s, or any version of gcc/g++, or other C compilers, that I have worked on has ever allowed it.
Richard MacCutchan 1-Sep-22 3:09am    
I doubt that. The rules of variable scope in C and C++ are very clear and must be adhered to.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900