Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good evening dear colleagues,
I developed this program and I found out a problem. I created an array of objects, which contain a string inside each of them. Unfortunately I do not know how to write in those strings. What it shows is this: It allows me to write in this first object but not in the next ones! this is because the getline instruction was thought for files, not for the strings. I cannot also use cin or getchar because it stops at the first spacebar.
How can I settle this problem?
I can also use an array of char istead of a string, this is not the main point.

C++
#include<iostream>
#include<string>
using namespace std;

const unsigned int MAXPROD = 10;
class Prodotto{
    string descrizione;
    float prezzo;
    int numero_reparto;
    public:
        void registra(string , float, int);
    void visualizza();
    friend void elenco_per_reparto(int reparto, Prodotto prodotto[]);
    Prodotto(): prezzo(0.0), numero_reparto(0) { descrizione= "  ";}
};

void Prodotto:: registra( string s, float p, int n){
    descrizione = s;
    prezzo= p;
    numero_reparto = n;
}

void Prodotto::visualizza(){
    cout << descrizione << "," << prezzo << endl;
}

void elenco_per_reparto(int reparto, Prodotto prodotto[]){
    for(int i = 0;  i < MAXPROD; i++)
    if(reparto == prodotto[i].numero_reparto)
    prodotto[i].visualizza();
}

void inserisci_prodotti();
void lista_prodotti();

Prodotto prod[MAXPROD];

int main(){
    int i;
    inserisci_prodotti();
    lista_prodotti();
    cout << "digitare il numero del reparto: ";
    cin >> i;
    elenco_per_reparto(i, prod);
   
    return 0;
}

void inserisci_prodotti(){
    string descrizione_prodotti;
    float prezzo_prodotti;
    int np;
    char risp;
    for(int i = 0; i < MAXPROD;i++){
        cout << "il prodotto " << i + 1 << endl;
        do{
            cout << "descrizione prodotto: ";
            getline( cin, descrizione_prodotti); // THE PROBLEM IS HERE!!!!!!!( in english: goods description)
            cout << "prezzo del prodotto: ";
            cin >> prezzo_prodotti;
            cout << "numero del reparto: ";
            cin >> np; 
            prod[i].registra(descrizione_prodotti, prezzo_prodotti, np);
            cout << "conferma? s/n";
            cin >> risp;
        }while(risp != 's');

        }
    }

void lista_prodotti(){
    cout << "lista dei prodotti ";
    for( int i = 0; i < MAXPROD; i++){
    cout << "prodotto " << i + 1 << ":";
    prod[i].visualizza();
    }
}


What I have tried:

Good evening dear colleagues,
I developed this program and I found out a problem. I created an array of objects, which contain a string inside each of them. Unfortunately I do not know how to write in those strings. What it shows is this: It allows me to write in this first object but not in the next ones! this is because the getline instruction was thought for files, not for the strings. I cannot also use cin or getchar because it stops at the first spacebar.
How can I settle this problem?
Posted
Updated 10-Mar-22 21:21pm
v3

1 solution

You need to add cin.ignore(1000, '\n') after reading risp in the do/while loop. What is happening here is this - when you do
C++
char risp;
cin >> risp;
the system reads in a single character, but the input buffer consists of the character you entered and the newline char. So now when the next getline() is called at the start of the loop, the first thing it reads in is the newline char that is still in the input buffer from the last thing you typed in, and so an empty string is assigned to descrizione_prodotti. You can confirm this by entering a string when asked to confirm like "some more text" - the program will read in the 's' and move on to the next getline() and "ome more text" will be assigned to the product description.
 
Share this answer
 
Comments
Andrea Antonelli 11-Mar-22 4:06am    
fantastic answer dear k5054! it is working very well right now. I still have a question though, how can it be possible? the instruction cin.ignore(1000, '\n') should be referred to the last cin, which is cin >> risp; but the problem was in the string (to be exact, it was on the pointer of the getline)...how did you get the right solution)
k5054 11-Mar-22 10:50am    
"how did you get the right solution?"
Experience, I suppose - although I'm quite capable of making this same error myself, even now - although I'd be much more likely to do another getline() rather than an ignore in this case, and then examine the first character to see if it matched, probably using tolower() so that case didn't matter.
Andrea Antonelli 11-Mar-22 12:35pm    
I am impressed anyway. It looks like that ignore(...) is automatically reffered to the LAST operation on string..that's the only reasonable explanation, for which it can be written at the the end of the do-while loop...! this is making me confused..;;
k5054 11-Mar-22 15:21pm    
The ignore should happen just after reading in the single char for the confirmation response, not after the while statement. This is because if you do not answer si, you still need to remove the trailing newline before you call getline() at the top of the loop.

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