2

I've got this code, and for some reason the cin.get does not assign a value to the p.name[i]. I don't know why, but getline does not work aswell. I've tried to just cin.get(temp,30), to read a temp, but this dont work in the for cycle.

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

typedef struct echip
{
    char name[30];
    double price;
};

void read(echip* p, int n)
{

    for (int i = 0; i < n; i++)
    {
        cout << "Name: ";

        cin.get((p + i)->name, 30);
        cin.ignore();

        cout << (p + i)->name;

        cout << "Price: "; cin >> (p+i)->price;
        cout << (p+i)->price;
    }
}

int main()
{
    echip* k;
    int n;

    cout << "Number: "; cin >> n;
    k = new echip[n];

    read(k, n);

    delete[] k;
    return 0;
}
Pilv
  • 63
  • 6
  • 1
    Why C-strings, if you have the `` header included? Your code cannot be compiled by others, I think. `citire()` is used, but not shown. You don't even call the read function that you show. – sweenish May 14 '20 at 20:44
  • It can make sense if you want a fixed length struct – Erwan Daniel May 14 '20 at 20:45
  • I think `citire` == `read` - but not sure. – Adrian Mole May 14 '20 at 20:46
  • @ErwanDaniel so what should I do? write echip* [10]? – Pilv May 14 '20 at 20:46
  • I'm sorry, I didn't rename the function in the main. – Pilv May 14 '20 at 20:47
  • 1
    Avoid doing `(p + i)->name` when `p[i].name` is way easier to read. You can also kick ahead the `p` pointer each iteration and just do `p->name` as well. Even better: Use `std::vector` and `for (auto& echip : echips)` on a `const std::vector& echips` argument. – tadman May 14 '20 at 20:52
  • You need `cin.ignore();` *before* the `get`, or the newline from the previous `cin` is still in the input buffer (and it stays in the input stream for other reads). – Adrian Mole May 14 '20 at 20:52
  • https://stackoverflow.com/questions/21567291/ – Remy Lebeau May 14 '20 at 20:57

2 Answers2

1

Place the cin.ignore() before the cin.get():

Live demo

#include<limits>
//...

for (int i = 0; i < n; i++)
{
    cout << "Name: ";
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); //here  
    cin.get((p + i)->name, 30);           

    cout << (p + i)->name;
    cout << "Price: "; 

    cin >> (p+i)->price;
    cout << (p+i)->price;
}

cin.get(), unlike cin with operator >>, will not ignore newline characters.

Some other minor issues I think is relevant to mention:

anastaciu
  • 20,013
  • 7
  • 23
  • 43
  • 1
    I know all these. I used typdef because I thought that this way I can solve it. – Pilv May 14 '20 at 21:22
  • @Pilv, oh, I see, it's nice to see you tried to solve it before asking, it's actually part of the site's guidelines, anyway, if you don't mind I would keep the minor issues section so that future readers can learn from this post and for overall completion of the answer. – anastaciu May 14 '20 at 21:26
0

Do cin.ignore() after numeric input.

cout << "Number: "; cin >> n; cin.ignore();

and

cout << "Price: "; cin >> (p+i)->price; cin.ignore();

Don't do cin.ignore() after get or getline.

Better still don't mix up get/getline and >>, then you don't have to do ignore at all.

john
  • 71,156
  • 4
  • 49
  • 68
  • @Pilv But really if you want to read lines, use getline, don't use >>. Read a line and then if you want a number convert the line to a number. – john May 14 '20 at 21:00