-3

I'm currently working on a small c++ project and I use it as practice for a final exam, so this could be a silly error. So... the thing is, I declared an array of structs for using it as a shopping list, were "articulo" (means article or product) has a name and a quantity field. Then declared that "articulo" struct like a list of them called "ListaArticulos" (means list of products) which has a maximum of 100 products. My question is, where am I messing this thing up? I mean, I tried to store values inside the struct and then saving them into the array and I keep getting the: expected primary expression before '.' token in line "cin >> articulo.prod;" and "cin >> articulo.cant;" .

#include <iostream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

typedef struct
{
    char prod[30];
    int cant;
}articulo;

typedef articulo ListaArticulos[100];

int nElem,opc;

int main()
{

    while( opc!=3 ){
        cout << "Bienvenido al la lista!." << "\n";
        cout << "\n[1]. Agregar articulos a la lista.";
        //cout << "\n[2]. Ver articulos actuales.";
        //cout << "\n[3]. Salir del programa." << "\n";
        cout << "\nElija una opción para continuar: ";

        cin >> opc;

        switch(opc)
        {
            default:
                cout << "\nCuantos articulos?: ";
                cin >> nElem;
                ListaArticulos LA;
                for(int i=0; i<nElem; i++){
                    cout << "Ingrese articulo " << i++ << ": ";
                    cin >> articulo.prod;
                    cout << "Ingrese cantidad: ";
                    cin >> articulo.cant;
                    LA[i] = articulo;
                }
            break;

        }
    }
return 0;
}

Any tips on this would be appreciate. Thank you all!

Sebtic
  • 1
  • 4
  • 2
    There are many errors in your code... – Vinícius Nov 16 '19 at 22:17
  • Possible duplicate of [Friend Function, expected Primary Expression before . token](https://stackoverflow.com/questions/15326648/friend-function-expected-primary-expression-before-token) – JaMiT Nov 16 '19 at 23:10
  • Other potential duplicates (I searched for "expected primary expression before token"): [File.cpp:148: error: expected primary-expression before ‘.’ token DIFFERENT SYMBOL](https://stackoverflow.com/questions/15736581/file-cpp148-error-expected-primary-expression-before-token-different-symb), [Expected primary-expression before '.' token](https://stackoverflow.com/questions/21817789/expected-primary-expression-before-token), and [Error: expected primary expression before '.' token](https://stackoverflow.com/questions/39668148/error-expected-primary-expression-before-token) – JaMiT Nov 16 '19 at 23:13

2 Answers2

0

articulo is a type. You want to use a variable of this type.

Replace the lines inside your for loop with these:

cout << "Ingrese articulo " << i+1 << ": ";
cin >> LA[i].prod;
cout << "Ingrese cantidad: ";
cin >> LA[i].cant;

and it will work.

I'd also like to point out that you made a typo with the i++ in cout. You already increment i in for(int i=0; i<nElem; i++), so you don't need to increment it here. If you want to print 1-indexed numbers with this, use i+1 as shown.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
0

articulo is a type, not an object. If you want to write to the element LA[i], then do so: std::cin >> LA[i].prod;

But I would really want to advise you to get a good book or tutorial on C++. This code is really bad.

JHBonarius
  • 10,824
  • 3
  • 22
  • 41
  • Thanks for the reply! i know the code, and i, are wrong, im learning the ropes from 0. Just wanterd someone who pointed me in the good direction. Thanks anyway for the help and the tip, appreciate it! – Sebtic Nov 16 '19 at 22:54
  • Forgot to ask, which c++ books do you recommend?. thank you again! – Sebtic Nov 16 '19 at 23:46
  • @Sebtic those questions are offtopic for Stack Overflow https://hackr.io/blog/10-best-c-cpp-books – JHBonarius Nov 17 '19 at 09:47
  • Got it!, sorry im really new to all this. Thanks again! – Sebtic Nov 17 '19 at 21:15