Getting multiple inputs from user

Hello All,

I'm writing a rectangle program and need a little help.

I need the user to enter the names, lengths and widths of four(4) rectangles. What is the best and easiest way for the user to enter all of this information?

I could ask the user for each piece of info separately, but that is clunky and time consuming. Any ideas would be greatly appreciated.
Last edited on
1
2
3
4
5
char RectNme [48];
int Length, Width;

cout << "Rect :";
cin >> RectNme >> Length >> Width;


Rect :  Square 138 28


Do this inside a loop and in this case RecNme="Square", Length=138 and Width=28;
Thanks soooo much. That helps a lot!
OK. So I'm still working on this Rectangle program and, once again, need a little help.

I pass the lengths entered by the user into the setLength function. When I run the program, it only lets me enter the first length, not all 4 lengths.

Any ideas? Code posted below. Thanks in advance.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void setName(string);
int setLength(int[]);
void setWidth(int);

int main()
{
	const int num = 4;
	int length[num], width[num], area[num];
	string name[num];

	cout << "Enter the names of 4 rectangles:\n";
	for (int count = 0; count < num; count++)
	{
		cout << "Rectangle " << count + 1 << ": ";
		cin  >> name[count];
	}
	setLength(length);
		cout << "Enter the widths of 4 rectangles:\n";
	for (int count = 0; count < num; count++)
	{
		cout << "Rectangle " << count + 1 << ": ";
		cin  >> width[count];
	}
	/*for (int count = 0; count < num; count++)
	cout << name[count] << "\n";
	for (int count = 0; count < num; count++)
	cout << length[count] << "\n";
	for (int count = 0; count < num; count++)
	cout << width[count] << "\n";*/
	for (int count = 0; count < num; count++)
	{
		area[count] = length[count] * width[count];
		cout << name[count] << " Area: ";
		cout << area[count] << endl;
	}
	
	system("pause");
	return 0;
}

/***************************************************************
 *                        setLength                            *
 * This function stores the names of the 4 rectangles entered  *
 * by the user in an array for later use.                      *
 ***************************************************************/
int setLength(int len[])
{
	int num = 4;
	cout << "Enter the lengths of 4 rectangles:\n";
	for (int count = 0; count < num; count++)
	{
		cout << "Rectangle " << count + 1 << ": ";
		cin  >> len[count];
		return len[count];
	}
}
Last edited on
Well guys, I figured it out all by myself!! Yay!!

Thanks for looking anyway.
1
2
3
4
5
6
7
8
9
10
11
12
int setLength(int len[])
{
int count;
	int num = 4;
	cout << "Enter the lengths of 4 rectangles:\n";
	for (count = 0; count < num; count++)
	{
		cout << "Rectangle " << count + 1 << ": ";
		cin  >> len[count];
	}
return len[count];
}

This is what I will do. Sorry to give you the answer I should of provide you advice as it was a minor error but see if you can find any other ways to fix it. Good Luck!
Thank you. That is exactly what I did.
Topic archived. No new replies allowed.