program that implements a small information system for a gas station

I need to write a program that implements a small information system for a gas station. with an array with Type of petrol, price and article number.The program needs to have a menu with Add new type of petrol and Search for an item by article number. The program needs to be with function and pointers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 #include <iostream>
#include <string>
#include <sstream>
using namespace std;
 struct gastation_t
{
  int tyofpetrol;   
  int price;
    int articnumber;  
} petrol[2];

void printpetrol(gastation_t typofpetrol);

int main(){
 int n;
for(n=0;n<2;n++){
  cout<<"Enter Type of Petrol 95 or 98: ";
  cin>>petrol[n].tyofpetrol;
   if(petrol[n].tyofpetrol=95 || petrol[n].tyofpetrol=98){
  cout<<"Enter price: ";
 cin>>petrol[n].price;
cout<<"Enter articnumber: ";
cin>>petrol[n].articnumber;
  }
  else{
    cout<<"You need to type 95 or 98 petrol";
  }
}
cout<<"\n You have entered these petrols: "<<endl;
for(n=0;n<2;n++)
  printpetrol(petrol[n]);
  return 0;
}
void printpetrol(gastation_t typofpetrol)
{
  cout<<"Type of petrol: "<<typofpetrol.tyofpetrol<<endl;
  cout<<"The price is: "<<typofpetrol.price<<endl;
  cout<<"The article number is: "<<typofpetrol.articnumber<<endl;
}

but something is wrong and i cant continue can oyu help me with the code i got error when i try to check is the number is 95 or 98
At line 19, you need to compare with == instead of =.
i know but when i try i got this error -
main.cpp:19:54: error: expression is not assignable
if(petrol[n].tyofpetrol=95 || petrol[n].tyofpetrol=98){
~~~~~~~~~~~~~~~~~~~~~~~~~~^
1 error generated.
> i know but when i try i got this error -
You know?

But can you read?

Your code STILL contains = where it should be ==.

i fix the problem with if statement but i don't know how to make the else statement to be if the number is not ==95 or 98 to ask again and again while the number is 95 or 98
Something like
1
2
3
4
do {
  cout<<"Enter Type of Petrol 95 or 98: ";
  cin>>petrol[n].tyofpetrol;
} while ( !(petrol[n].tyofpetrol==95 || petrol[n].tyofpetrol==98) );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

constexpr int NO_OF = 2;

 struct gastation_t
{
  int tyofpetrol = 0;   
  int price;
    int articnumber;  
} petrol[NO_OF];

void printpetrol(gastation_t typofpetrol);
gastation_t * find_by_articnumber( gastation_t * begin, gastation_t * end, int articnumber);

int main(){
    int n;
    for(n=0;n<NO_OF;n++){
        while (true) {
            cout<<"Enter Type of Petrol 95 or 98: ";
            cin>>petrol[n].tyofpetrol;
            if (petrol[n].tyofpetrol != 95 && petrol[n].tyofpetrol != 98){
                cout<<"You need to type 95 or 98 petrol\n";
                continue;
            }
            cout<<"Enter price: ";
            cin>>petrol[n].price;
            cout<<"Enter articnumber: ";
            cin>>petrol[n].articnumber;
            break;
        }
    }
    cout<<"\n You have entered these petrols: "<<endl;
    for(n=0;n<NO_OF;n++) printpetrol(petrol[n]);
    
    cout << "Searching an article by number.\n Enter article number: ";
    int number;
    cin >> number;
    gastation_t * article = find_by_articnumber( petrol, petrol+NO_OF, number);
    
    if ( article != nullptr) {
        cout << "Found: ";
        printpetrol( *article );
    }
    else cout << "Article not found!";
    
    return 0;
}

void printpetrol(gastation_t typofpetrol)
{
  cout<<"Type of petrol: "<<typofpetrol.tyofpetrol<<endl;
  cout<<"The price is: "<<typofpetrol.price<<endl;
  cout<<"The article number is: "<<typofpetrol.articnumber<<endl;
}

gastation_t * find_by_articnumber( gastation_t * begin, gastation_t * end, int articnumber)
{
    for (gastation_t * ptr = begin; ptr != end; ++ptr)
    {
        if (articnumber == ptr->articnumber)
            return ptr;
    }
    return nullptr;
}

I have fixed your program. The most stuff I added is the function find_by_articnumber, which works with help of pointers.

Feel free to ask if somethin is unclear to you.
Thank you soo much . Its working perfectly everything that i wanted thank you again much love for you <3
Thanks, but consider that find_by_articnumber() is in fact not really needed, because <algorithm> provides a function named find_if(...). So you could replace find_by_articnumber by:

 
gastation_t * article = std::find_if( petrol, petrol+NO_OF, [=](gastation_t gst){ return number == gst.articnumber;} );

find_if() takes an embedded lambda-function as its last argument.
Topic archived. No new replies allowed.