Help with structures

Hey I was wondering if anyone knows what I'm doing wrong here. I'm trying to write an input function for the different parts of my structure data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 struct product
{
   char itemname[30];
   char needdone[50];
   int purchase;
}
void input(product);
int main()
{
   product items[11];
   for(int i = 0; i<11; i++)
   {
      input(items[i]);
   }
   return 0;
}
input(product &items)
{
   cout << "Please enter the name of the item" << endl;
   cin.get(items.itemname);
   cin.ignore(100, '\n');
   cout << "Please enter a description" << endl;
   cin.get(items.needdone);
   cin.ignore(100, '\n');
   cout << "Please enter whether the product has been purchased 1 for yes 0 
   for no" << endl;
   cin >> items.purchase;
}
Last edited on
Maybe make your prototype match the implementation.
7 void input(product);
vs
17 input(product &items)
Post real code, not this butchered mess. It's missing all kinds of things (headers; semicolon after struct definition; ampersand after product in prototype; void before function definition; string literal has a unescaped literal newline in it).

And "what am I doing wrong here" is not a good problem statement.
What's the problem?
Last edited on
You forgot the newline after items.purchase.

Make sure to specify a maximum number of characters to read for your char[] strings. For simple char[] strings, you can use sizeof directly. That way you can change the size of the arrays in your structure without worrying about breaking code below.

The prototype for input() and the function’s actual type do not match. (The compiler thinks they are different functions.)

 
#include <limits> 
 
void input(product&);
1
2
3
4
5
6
7
8
9
void input(product& item)  // a single item
{
   cout << "Please enter the name of the item\n";
   cin.get(item.itemname,sizeof(item.itemname));
   cin.ignore(numeric_limits<streamsize>::max(),'\n');
   ...
   cin >> items.purchase;
   cin.ignore(numeric_limits<streamsize>::max(),'\n');
}


Computers are very strict, and (for example) don’t understand that you only meant a single “input” function — it thinks you meant two, and can’t find one of them.

Crank up your compiler warnings when you compile and try to eliminate them all. (Start with the first and work your way through.) The warnings and errors you get are the compiler’s way of telling you you made a mistake that needs to be fixed. Learning to read them helps tremendously.

Hope this helps.
Topic archived. No new replies allowed.