Can someone help me debug 1 error please.

Alright, so my program is ready to go, but I have one error that pops up when compiling. It compiles successfully, but this error is causing my program to crash.

Heres the code:

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
#include "stdafx.h"
#include <iostream>
#include <istream>
#include <string>
#include <algorithm>
#include <cstdlib>

using namespace std;

bool alpha(const string& left, const string& right) //Function for sorting names alphabetically
{
	return (left[0] < right[0]); //Tests first letter of name
}


int main()
{
	const int numNames = 10;
	string player_names[numNames];  //Array for player names                              
	string player_scores[10]; //Array for player scores
	string nameIN;            //variable to hold name
	string scoreIN;           //variable to hold score

for( int i=0; i < 10; i++)  //For loop
{
	cout << "Please enter name and score for Player " << i+1 << "\n" << endl; //Ask user to input name and score
	cout << "Name: \n";
	cin >> nameIN;
	cout << "Score: \n";
	cin >> scoreIN;
	
	player_names[i] = nameIN+" "+scoreIN;   //To store name, then score
	player_scores[i] = scoreIN+" "+nameIN;  //To store score, then name
}


{
bool alpha(const string& left, const string& right);

sort(player_names, player_names + numNames, alpha);

	for( int i=0; i < numNames; alpha)
		{
		cout << player_names[i] << "\n";
		cout << endl;
		}
}


{
sort(player_scores, player_scores+10); //Sorting scores

	for( int i=9; i>=0; i--)
		{
		cout << endl;
		cout << player_scores[i] << "\n";
		}
}


system("pause");

return 0;
}


And the error its producing:

1
2
3
improving your code.cpp(49): warning C4551: function call missing argument list
//And is for line 42:
for( int i=0; i < numNames; alpha)


Thanks for any help!!!
Last edited on
Your for loop is trying to call a function with out passing it arguments. Why are you calling the function there by the way? It's potentially very clever, but do you understand it?
I understand what it does, however, I don't understand what the issue seems to be.

At the risk of sounding like an idiot, why is it potentially clever?

Do you mind clarifying what you mean by my function not passing any arguments? I'm fairly new to all this, but this is stumping me. Seems like it should work.

To be honest, I only called the function there to see if it would fix the error...
You need to give it a parameter list. For instance, you would not simply call cin.get, you would actually add a parameter list, even if it is empty: cin.get().

The problem in your code is that you call the alpha function but you don't give it any parameters. You need to call it like this: alpha("string on the left", "string on the right") because you specify in the function definition that it takes two string parameters. Obviously you'd replace the strings above with actual string objects, like alpha(LeftString, RightString) or however your program works.
Alright, let me check it out and I'll post back. Thanks a lot.

And, again, why would calling the function there be clever?
I said that it was potentially very clever because that section of the for loop is executed after the 'body'. Most people use it to increament or decreament a counter, but by putting a function there you could use it to do any number of things. That is if you actually understood what you are doing, and even though you say you do this line here:
To be honest, I only called the function there to see if it would fix the error...

Makes me think that you do not.
See Computergeek's post below.
Last edited on
Alright, so I guess I'm not understanding you correctly. But let me clarify a bit more for your sake.
Even when I don't call the Bool function before the sort and FOR loop, it gives me the same error for the FOR loop. Written as:

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

sort(player_names, player_names + numNames, alpha);

	for( int i=0; i < numNames; alpha)
		{
		cout << player_names[i] << "\n";
		cout << endl;
		}
}
See Computergeek's post below.

Sorry, I didn't realise he was using the std::sort function...I saw a line of code that wasn't indented correctly and I though it was a function definition...
Last edited on
@ L B: "sort(...)" can take three parameters, it's true that in this case it's a redundent but it wouldn't cause a compile error.

@ OP: I was refering to the call to "alpha(...)" inside your for loop. The third argument in your for loop, where you have "alpha" written with no parameters how did you manage to misunderstand me?
Alright, I understand now. I thought you were referring to either the function definition. Thanks for clearing that up.

I'll mark as solved if it works, otherwise, I'LL BE BOCK! lol

Thanks again.

**EDIT**

Alright, so the error is gone, but I must be doing something wrong because the program crashes and even though it compiles correctly, when I adjusted the argument, they have red lines under them like I did it wrong.

Heres the new code:

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

bool alpha(const string& left, const string& right);

sort(player_names, player_names + numNames, alpha);

	for( int i=0; i < numNames; alpha(left, right))
		{
		cout << player_names[i] << "\n";
		cout << endl;
		}
}


HELP PLEASE!!!
Last edited on
BUMP
The for loop should go
 
for (int i; i<numNames; i++/*or*/i--)
@ OP: Because "left" and "right" are not declared yet in that scope, they don't pass any real data to the function. Also now that I reread this what you're doing on Line 38 is useless as well, do you have much experiance using functions?
That solved it Mcqueen!!

Thanks a lot guys. I owe you a few.
Topic archived. No new replies allowed.