C-String Input Problem

Howdy fellas,

I've got a project for class, and part of it involves inputting
c-string information into a struct.

Here is my code for inputting the information:

1
2
3
4
5
6
7
8
9
10
11
12

void addAnEntry(invType & inv){
	cout << endl << "Enter ID: ";
	cin.getline(inv.item[inv.totalItems].ID, 8);
	cout << endl << "Enter Item Name: ";
	cin.getline(inv.item[inv.totalItems].name, 20);
	cout << endl << "Enter Producer: ";
	cin.getline(inv.item[inv.totalItems].producer, 20);
	cout << endl << "Enter Quantity: ";
	cin >> inv.item[inv.totalItems].quantity;
	inv.totalItems++;
	cin.get(); }


And here is my code for an invType, and itemType.

1
2
3
4
5
6
7
8
9
10
11
struct itemType {
public:
	char ID[8];
	char name[20];
	char producer[20];
	int quantity; };

struct invType{
public:
	itemType item[100];
	int totalItems; }; 


I run into a problem when trying to run addAnEntry, the command prompt
does the following:


Enter ID:
Enter Item Name: 'Eggs'

Enter Producer: 'Eggland's Best'

Enter Quantity: '20'


Which isn't what I wanted. (By the way, the stuff in '' is my input.)
For some reason it doesn't take an input for ID, and also inserts an extra
line in between things that I didn't want it to insert.

Does anyone have any idea what is going on here?
I have produced the following code to test your issue:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

using namespace std;

struct itemType {
public:
	char ID[8];
	char name[20];
	char producer[20];
	int quantity;
};

struct invType {
public:
	itemType item[100];
	int totalItems;
};

void addAnEntry(invType & inv){
	cout << endl << "Enter ID: ";
	cin.getline(inv.item[inv.totalItems].ID, 8);
	cout << endl << "Enter Item Name: ";
	cin.getline(inv.item[inv.totalItems].name, 20);
	cout << endl << "Enter Producer: ";
	cin.getline(inv.item[inv.totalItems].producer, 20);
	cout << endl << "Enter Quantity: ";
	cin >> inv.item[inv.totalItems].quantity;
	inv.totalItems++;
	cin.get(); }

void displayOutput(itemType i) {
	cout << "Output:" << endl;
	cout << "ID: " << i.ID << endl;
	cout << "Name: " << i.name << endl;
	cout << "Producer: " << i.producer << endl;
	cout << "Qty: " << i.quantity << endl;
}

int main(int argc, char* argv[]) {
		invType i;
		i.totalItems = 0;
		addAnEntry(i);
		displayOutput(i.item[0]);
		return 0;
}


My output looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
Enter ID: 1

Enter Item Name: Name

Enter Producer: Producer

Enter Quantity: 1  
Output:
ID: 1
Name: Name
Producer: Producer
Qty: 1


I am able to enter an ID just fine, and I only see lines that I added. I initially had some issues using my debugger. But running it outside the debugger allowed it to work fine. How is this different from what you are experiencing?
Last edited on
Topic archived. No new replies allowed.