'cin.ignore() seems to 'eat' the first letter of my programs first string but I still need it

I need to read a unspecified number of products, each product vector has a name, a price and a number. Afterwards I need to display the information of all products (only if the products number is >0). I've noticed that the name of the first product misses its first letter, and I know the cause, but I don't know how to fix it. Some products have single word names while others don't.

(pret=price(float), stoc=number of products(int), nume = name(string))

#include<iostream>
#include<string>
#include<limits>

class Produs{
private:
    std::string nume;
    float pret;
    int stoc;
public:
    Produs(std::string nume=" ",float pret=0,int stoc=0):nume(nume),pret(pret),stoc(stoc){};

    void set_nume(std::string nume){
        this->nume=nume;
    }
    std::string get_nume(){
        return this->nume;
    }
    
    void set_pret(float pret){
        this->pret=pret;
    }
    float get_pret(){
        return this->pret;
    }
    
    void set_stoc(int stoc){
        this->stoc=stoc;
    }
    int get_stoc(){
        return this->stoc;
    }
    
    void display(){
        std::cout<<nume<<"\n";
        std::cout<<pret<<"\n";
        std::cout<<stoc<<"\n";
    }
};

class Cos_cumparaturi{
    
};

class Reducere{
    
};

class Magazin{
private:
   int index;
   int STOC;
   Produs p[10];
public:
   Magazin(int index=0,int STOC=0):index(index),STOC(STOC){};
   
   void read(std::string nume,float pret,int stoc){
       p[index].set_nume(nume);
       p[index].set_pret(pret);
       p[index].set_stoc(stoc);
       index++;
   }

   void display_all(){
       for(int b=0;b<index;++b){
           if(p[b].get_stoc()>0){
               p[b].display();
          }
       }
   }
};

int main(){
    Magazin m;
    std::string nume;
    float pret;
    int stoc;
    int i=0,c;
    
    for(int o=0;o<10;++o){
           
        std::cin.ignore();
        std::getline(std::cin, nume);
          
        {
            if(nume!="STOP"){   
          
                std::cin>>pret;
                std::cin>>stoc;
          
                m.read(nume,pret,stoc);
            }
            else{
                break;
            }
        }
    }
    
    std::cin>>c;
    
    switch(c){
        case 1:
            m.display_all();
            break;
    }
}

this is the input:

Papuci
45
8
Nvidia RTX 3090
8999
2
Dezinfectant
20
35
STOP
1

this is my output:

apuci
45
8
Nvidia RTX 3090
8999
2
Dezinfectant
20
35

The P from the first string is missing, so I can't pass the first automated test of the problem.

My functions work correctly, I only need to fix this cin.ignore() problem.



Solution 1:[1]

You are calling ignore() before you have read anything. You need to call it after you have read something that left behind something worth ignoring, like ignoring a line break after reading an integer.

In this case, move std::cin.ignore(); from before std::getline(std::cin, nume); to after std::cin>>stoc;.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Remy Lebeau