Overflowing Array

When I have input over 15 characters the result is: it display every cout and writes the "Either" cout.

How can I fix it?

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	const int SIZE=16;
	char f1[SIZE], f2[SIZE], f3[SIZE], l1[SIZE], l2[SIZE], l3[SIZE];
	double t1, t2, t3;

	cout << "This program will put the best three runner\n"
		    "in order of their finishing time."
		    "\nPlease enter runners' details.\n\n"
		    "***WARNING***: Last names and first names will be displayed with\n"
			"the first 15 characters only and miliseconds will be displayed with\n"
			"three decimal points only.\n\n";
	
	cout << "Runner Number 1 Details\n"
			"-----------------------\n"
			"Last Name              : ";
	cin.getline(l1, SIZE);
	cout << "\nFirst Name             : ";
	cin.getline(f1, SIZE);
	cout << "\nFinish Time(In Seconds): ";
	cin >> t1;
		
	cout << "Runner Number 1 Details\n"
			"-----------------------\n"
			"Last Name              : ";
	cin.ignore();
	cin.getline(l2, SIZE);
	cout << "\nFirst Name             : ";
	cin.getline(f2, SIZE);
	cout << "\nFinish Time(In Seconds): ";
	cin >> t2;

	cout << "Runner Number 1 Details\n"
			"-----------------------\n"
			"Last Name              : ";
	cin.ignore();
	cin.getline(l3, SIZE);
	cout << "\nFirst Name             : ";
	cin.getline(f3, SIZE);
	cout << "\nFinish Time(In Seconds): ";
	cin >> t3;
	
	
	if (t1==t2||t1==t3||t2==t3||t1<=0||t2<=0||t3<=0)
	{
		cout << "\n\nEither you have entered the same finishing time for different runners\n"
				"or one or more finishing time is smaller than or equal to 0.\n"
				"Please restart the program and enter correct finishing times.\n\n";
				
	system("pause");
	return 0;
	}
	else
	{
		cout << "\n\nHere are the runners displayed according to their finishing time:\n\n";

		if (t1<t2&&t1<t3)
		{
			if (t2<t3)
			{
				cout << "1. " << l1 << ", " << f1 << "    " << fixed << setprecision(3) << t1 << endl;
				cout << "2. " << l2 << ", " << f2 << "    " << fixed << setprecision(3) << t2 << endl;
				cout << "3. " << l3 << ", " << f3 << "    " << fixed << setprecision(3) << t3 << "\n\n";
			}
			else
			{
				cout << "1. " << l1 << ", " << f1 << "    " << fixed << setprecision(3) << t1 << endl;
				cout << "2. " << l3 << ", " << f3 << "    " << fixed << setprecision(3) << t3 << endl;
				cout << "3. " << l2 << ", " << f2 << "    " << fixed << setprecision(3) << t2 << "\n\n";
				
			}
		}
		else if (t2<t1&&t2<t3)
		{
			if (t1<t3)
			{
				cout << "1. " << l2 << ", " << f2 << "    " << fixed << setprecision(3) << t2 << endl;
				cout << "2. " << l1 << ", " << f1 << "    " << fixed << setprecision(3) << t1 << endl;
				cout << "3. " << l3 << ", " << f3 << "    " << fixed << setprecision(3) << t3 << "\n\n";
			}
			else
			{
				cout << "1. " << l2 << ", " << f2 << "    " << fixed << setprecision(3) << t2 << endl;
				cout << "2. " << l3 << ", " << f3 << "    " << fixed << setprecision(3) << t3 << endl;
				cout << "3. " << l1 << ", " << f1 << "    " << fixed << setprecision(3) << t1 << "\n\n";
			}
		}
		else
		{
			if (t2<t1)
			{
				cout << "1. " << l3 << ", " << f3 << "    " << fixed << setprecision(3) << t3 << endl;
				cout << "2. " << l2 << ", " << f2 << "    " << fixed << setprecision(3) << t2 << endl;
				cout << "3. " << l1 << ", " << f1 << "    " << fixed << setprecision(3) << t1 << "\n\n";
			}
			else
			{
				cout << "1. " << l3 << ", " << f3 << "    " << fixed << setprecision(3) << t3 << endl;
				cout << "2. " << l1 << ", " << f1 << "    " << fixed << setprecision(3) << t1 << endl;
				cout << "3. " << l2 << ", " << f2 << "    " << fixed << setprecision(3) << t2 << "\n\n";
			}
		}

		}
	system("pause");
		return 0;
}


I hope someone can help fast. This is due tomorrow.
I have tried different cin.ignore methods or get methods, but everytime I did put an input over 15 characters, I failed.

"Be sure the names do not overflow the arrays".
That is what I need to succeed.

Thank you.

There's no solution. If you use arrays of chars to get your input, you'll always be limited to some fixed number of characters. Use std::string to avoid that problem.
Then let's say the input is 40 characters (And SIZE is defined as 99).
How can I just get the 15 characters of those input without seeing an error at the end?
All I want to is display 15 characters without an error.

I can't use string at this point with my knowledge.
Then you just need to do input[15]=0; and proceed as normal. But note that merely increasing the size of the array only moves the problem. It doesn't eliminate it.
Can you give me an example with input?
I was checking about it, but couldn't find where will I put it.
Thanks.

What I will do at this point is give one as above where it works unless name is shorter than the array (except I will put setw in the cout below) and give another where only 15 characters will be displayed even if it is longer than 15 characters and that is if you give me an example.

Can't I write a code to get the number of characters in the input and the ndisplay that the name has more than 15 characters long, restart the program and enter only 15 character?
That wouldn't look good though.
Last edited on
1
2
3
4
5
6
7
8
char tempArray[200];
char l1[16];

cout << "Please enter a name: ";
cin >> tempArray;

for (int i = 0; i < 16; i ++)
   l1[i] = tempArray[i];


If you need a null terminated string, change the for loop to less than 15 and set l1[15] to the null character '\0'
Last edited on
Thank you so much for this.
There is still an error, but my eyes are closing, so I gotta go to sleep.
I can't see anything or do anything right at the moment.
Nobody would believe that I took 2 java and 2 c++ classes.
If you don't practice, you forget it.
I even forgot languages.

Anyway, thank you so much.
Your suggestion opened a new door in my program and helped me a lot.

Have a good night.
Topic archived. No new replies allowed.