I'm working on a project that accepts grocery item names and prices and stores them in a char array. This is for school and we are absolutely not allowed to use string for any reason.
I don't know why, but I'm not sure where to start. I don't have any code to post, I'm just looking for hints on where to start. When I experiment with cin.getline I only get the first letter instead of the whole word. Is cin.getline only for strings? I thought it could be used with cstrings as well.
There are two different getline() functions, one for C-strings (istream.getline(C-string, size, optional delimiter)) and one for std::string getline(istream&, std::string&, char optionalDelimiter).
jlb, here are my includes and everything I've declared. Some of these variables haven't been used yet, but they will be used later, once I get past this issue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <iomanip> //this will get used later
#include <cstring>
usingnamespace std;
constint MAX_ITEMS = 100;
int main()
{
char productName[MAX_ITEMS];
double productPrice[MAX_ITEMS];
char name;
double price;
mbozzi,
"Show the declaration of istream"
Maybe this is part of the problem. I didn't know you had to declare istream, I thought it could be used the same way as cin.
Andy, I'm a little confused about istream.getline and cin.getline. With jlb's example above, he says cin.getline is for string and istream.getline is for c-strings. When I tried using cin.getline, I was only getting the first letter of the string. I'm going to compare mine to yours and see if there's something I'm missing.
I think I've got it but I had to use cin instead of istream. I'm unsure what I'm doing wrong with istream. If anyone has more thoughts on that I'd appreciate it. For now, it's enough that it works and I can move on.
Thanks everyone! I'll be back if I get super stuck again.