populating char array without using string

closed account (1Ck93TCk)
Hi All,

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.

I appreciate any help!

jmb
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).

closed account (1Ck93TCk)
Oh, thank you! Somehow in all of my reading I missed that.
closed account (1Ck93TCk)
I keep getting an error with this:

1
2
cout << "Enter an item name: ";
istream.getline(name, 20);


This is literally the only code I have so far apart from declaring variables. I assume there's something wrong with the way I'm doing this.

The error I get:
 
CS162/shoppingPrice2.cpp|19|error: expected unqualified-id before ‘.’ token|


line 19 refers to the getline line.

What am I doing wrong here?
What #include files are you including?

Show the declaration of istream.
Last edited on
Hello jmb,

I believe jlb was giving an example with istream.getline(C-string, size, optional delimiter)).

When I added your code to what I tried I did receive an error, although not what you did.

When I changed the code it worked well in VS2017.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
//#include <cstring> // <--- Not needed at this point. Maybe later?

int main()
{
	constexpr size_t MAXSIZE{ 20 };

	char name[MAXSIZE]{}; // <--- Defined and initialized.

	//std::cout << "\n Enter a string: ";
	//std::cin.getline(name, MAXSIZE);

	std::cout << "Enter an item name: ";
	std::cin.getline(name, MAXSIZE);

	std::cout << "\n\n " << name << std::endl;

	return 0;
}


Hope that helps,

Andy
closed account (1Ck93TCk)
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>

using namespace std;

const int 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 really do appreciate all of the help, guys.
closed account (1Ck93TCk)
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.

jmb
Andy, I'm a little confused about istream.getline and cin.getline.

What are you confused about? cin is an istream instance, istream is a standard C++ class.

With jlb's example above, he says cin.getline is for string and istream.getline is for c-strings.


Look again, nowhere in my first post did I mention cin.

closed account (1Ck93TCk)
Things are clearing up. I went back and looked and you're right, I made that up, you did not say anything about cin. Sorry about that!
Topic archived. No new replies allowed.