Hi working on this lab, and theres mistakes were i know im not writing the code correctly but i am trying, really am. If anyone couuld help me out with these errors, or notice were i am writing incorrectly can you post correct way. thank you..

Description:

Create a program that will display baseball players names(just last names) and averages. The most players you would have to process is 10. Input the players last name, at-bats, and hits. A players’ average is calculated by dividing the players’ hits by the number of at-bats. Averages are generally displayed to 3 decimal points. For any player, if the number of hits exceeds the number of at-bats, display an error message.

Your program should have 2 arrays. One to store the names, and another one to store the averages. You will need to include the string library in order to have string variables.


Errors:

11 `string' does not name a type
21 conflicting declaration 'char Name'
13 'Name' has a previous declaration as `std::string Name'
21 declaration of `char Name'
13 conflicts with previous declaration `std::string Name'
25 initializer fails to determine size of `Array_names'
25 invalid initializer
26 initializer fails to determine size of `Array_avg'
26 invalid initializer
33 no match for 'operator<<' in 'std::cin << Name'
35 no match for 'operator!=' in 'Name != QUIT'
38 \p no match for 'operator[]' in 'Array_names[Name]'
38 expected primary-expression before "format_name"
38 expected `;' before "format_name"
42 name lookup of `count' changed for new ISO `for' scoping
29 using obsolete binding at `count'
99 cannot convert `std::string' to `int' in return

#include <iostream>    
#include <iomanip>    
#include <string>
#include <cctype>  
using namespace std;

int main()
{


void string format_name(char Name);

string Name;

//local constants
int Average = 0;
int MAX = 10;
int At_Bat = 0;
int Hit = 0;
char QUIT = 'Q';
char Name;


//local variables
char Array_names[]=Name;
int Array_avg[]=Average;

/***********************************************************/
for (int count = 0; count <= 10 ; count ++)
{
    cout <<"\n\n\n\n";
    cout <<"Please enter player name, or Q to quit";
    cin << Name;
    
    while (Name != QUIT)
    {
          //call function to put name in array
          Array_names[Name] = string format_name;
          }
}
/*****************************************************************/
for (int i = 0; i <= count ; i++)
{
cout << "\n\n\n\n";
cout <<"Please Enter At bats";
cin >> At_Bat;
cout <<"Please Enter total hita";
cin >> Hit;

while (At_Bat >= Hit);
Array_avg[Average] = Hit/At_Bat;
}
/**********************************************************************
 *Program Name   : 
 *Author         : 
 *Date           : 
 *Course/Section : CSC 110
 *Program Description:  This function is designed to format a string
 *  to be 12 characters long.  If the string is longer than 12 chars
 *  then the string will be truncated.  If the string is shorter than 
 *  12 characters then it will be padded with spaces at the end.
 *
 *BEGIN - Format Name (input string)
 *   Init pad variable to # of chars the name is too long or too short
 *   IF (input string is too short)
 *      Pad the string with spaces
 *   ELSE //input string is too long
 *      Truncate the unwanted characters
 *   END IF
 *   Return the new formatted string
 *END - Format Name
 *********************************************************************/

{
string format_name(string Name);


	//local constants
	const int WIDTH = 12;

	//local variables
	int Pad = WIDTH - Name.length(); //# of chars too long or too short

    /***********begin function body*****************************/

    //if the name is shorter than the desired width
	if (Pad > 0)

		//pad the name with spaces so the length is correct
		for (int Count = 0; Count < Pad; Count++)
			Name = Name + " ";

	else //the name is longer than the desired width

		//truncate all characters after the desired width
		Name = Name.substr(0, WIDTH);

    //return the new string
	return Name;

}//end format name

}

Recommended Answers

All 9 Replies

are you the one who create this program or not?! I think this is a quiz that you will need to remove all the errors. Correct me if I'm wrong. :D

Line 11 is just plain weird. It has two types followed by a function name and parameter list. I thing what you did was declare main() as type void on line 11 initially with a function prototype for fomat_string() on line 9 or something. Then you cut main(){ and moved it to line 7 and moved the prototype for format_name() down to line 11. Weird, weird, weird. To fix it, what ever the blazes happened, move line 11 to line 6 and eliminate the type void before the type string.

Then you declared two variables of different type the same name, Name. One Name is declared as type string on line 13 and the other type char on line 21. Either eliminate one of the Names or change it to some other name.

Fix those and recompile. Who knows what will happen after that. Just tackle the first error message first. Better yet, get in the habit of doing frequent compiles as you write the program instead of trying to deal with a twisted knot after trying to write the whole thing and then compile.

Better yet, get in the habit of doing frequent compiles as you write the program instead of trying to deal with a twisted knot after trying to write the whole thing and then compile.

This is right! :cool:

are you the one who create this program or not?! I think this is a quiz that you will need to remove all the errors. Correct me if I'm wrong. :D

no its a lab. the errors are mine

Got rid of a lot of your errors :

#include <iostream>    
#include <iomanip>    
#include <string>
#include <cctype>  
using namespace std;

//function prototype goes outside main
string format_name(string Name);

int main()
{
	string Name = "";

	//local constants
	int Average = 0;
	const int MAX = 100;
	int At_Bat = 0;
	int Hit = 0;
	string QUIT = "Q";	


	//local variables	
	string Array_names[MAX] = {""};
	int Array_avg[MAX] = {0};


/***********************************************************/
int count = 0;
for (count = 0; count <= 10 ; count ++)
{
    cout <<"\n\n\n\n";
    cout <<"Please enter player name, or Q to quit";
    cin >> Name;	
    while (Name != QUIT)
    {
          //call function to put name in array
		Array_names[count] = format_name(Name);
          }
}
/*****************************************************************/
for (int i = 0; i < count ; i++)
{
cout << "\n\n\n\n";
cout <<"Please Enter At bats";
cin >> At_Bat;
cout <<"Please Enter total hita";
cin >> Hit;

while (At_Bat >= Hit);
Array_avg[Average] = Hit/At_Bat;
}


}


/**********************************************************************
 *Program Name   : 
 *Author         : 
 *Date           : 
 *Course/Section : CSC 110
 *Program Description:  This function is designed to format a string
 *  to be 12 characters long.  If the string is longer than 12 chars
 *  then the string will be truncated.  If the string is shorter than 
 *  12 characters then it will be padded with spaces at the end.
 *
 *BEGIN - Format Name (input string)
 *   Init pad variable to # of chars the name is too long or too short
 *   IF (input string is too short)
 *      Pad the string with spaces
 *   ELSE //input string is too long
 *      Truncate the unwanted characters
 *   END IF
 *   Return the new formatted string
 *END - Format Name
 *********************************************************************/

string format_name(string Name)
{
	//local constants
	const int WIDTH = 12;

	//local variables
	int Pad = WIDTH - Name.length(); //# of chars too long or too short

    /***********begin function body*****************************/

    //if the name is shorter than the desired width
	if (Pad > 0)

		//pad the name with spaces so the length is correct
		for (int Count = 0; Count < Pad; Count++)
			Name = Name + " ";

	else //the name is longer than the desired width

		//truncate all characters after the desired width
		Name = Name.substr(0, WIDTH);

    //return the new string
	return Name;

}//end format name

Thanks for the fixes appreciate it. Well when i was compiling it didnt get past the first step so i added this part and still wont compile... idk what i should show so ill show first what i changed then whole program. No errors showed up on my labtop so should compile.

int count = 0;
for (count = 0; count <= 10 ; count ++)
{
    cout <<"\n\n\n\n";
    cout <<"Please enter player name, or Q to quit";
    cin >> Name;
    while (Name != QUIT)
    {
          //call function to put name in array
		Array_names[count] = format_name(Name);
        
        }
        if (count >= 10)
        {
                  cin >> QUIT;
        }
        else (count < 10);
        {
             cout <<"Please enter player name, or Q to quit";
             }
        }
#include <iostream>    
#include <string>
#include <cctype>  
using namespace std;

//function prototype goes outside main
string format_name(string Name);

int main()
{
	string Name = "";

	//local constants
	int Average = 0;
	int At_Bat = 0;
	int Hit = 0;
	const int MAX = 100;
	string QUIT = "Q";	


	//local variables	
	string Array_names[MAX] = {""};
	int Array_avg[MAX] = {0};


/***********************************************************/
int count = 0;
for (count = 0; count <= 10 ; count ++)
{
    cout <<"\n\n\n\n";
    cout <<"Please enter player name, or Q to quit";
    cin >> Name;
    while (Name != QUIT)
    {
          //call function to put name in array
		Array_names[count] = format_name(Name);
        
        }
        if (count >= 10)
        {
                  cin >> QUIT;
        }
        else (count < 10);
        {
             cout <<"Please enter player name, or Q to quit";
             }
        }
/*****************************************************************/
for (int i = 0; i < count ; i++)
{
cout << "\n\n\n\n";
cout <<"Please Enter At bats";
cin >> At_Bat;
cout <<"Please Enter total hita";
cin >> Hit;

while (At_Bat >= Hit);
Array_avg[Average] = Hit/At_Bat;
}


}


/**********************************************************************
 *Program Name   : 
 *Author         : 
 *Date           : 
 *Course/Section : CSC 110
 *Program Description:  This function is designed to format a string
 *  to be 12 characters long.  If the string is longer than 12 chars
 *  then the string will be truncated.  If the string is shorter than 
 *  12 characters then it will be padded with spaces at the end.
 *
 *BEGIN - Format Name (input string)
 *   Init pad variable to # of chars the name is too long or too short
 *   IF (input string is too short)
 *      Pad the string with spaces
 *   ELSE //input string is too long
 *      Truncate the unwanted characters
 *   END IF
 *   Return the new formatted string
 *END - Format Name
 *********************************************************************/

string format_name(string Name)
{
	//local constants
	const int WIDTH = 12;

	//local variables
	int Pad = WIDTH - Name.length(); //# of chars too long or too short

    /***********begin function body*****************************/

    //if the name is shorter than the desired width
	if (Pad > 0)

		//pad the name with spaces so the length is correct
		for (int Count = 0; Count < Pad; Count++)
			Name = Name + " ";

	else //the name is longer than the desired width

		//truncate all characters after the desired width
		Name = Name.substr(0, WIDTH);

    //return the new string
	return Name;

}//end format name

It should be :

int count = 0;
for (count = 0; count <= 10 ; count ++)
{
    cout <<"\n\n\n\n";
    cout <<"Please enter player name, or Q to quit";
    cin >> Name;
    if(Name == QUIT) break; //exit the loop
  else{
          //call function to put name in array
	Array_names[count] = format_name(Name); 
     }
}

ok it almosts works, the program stops when i enter the first at_BAT AND then hits and wont loop to get the neext persons at bat and hits.

error:

 36  no matching function for call to `toupper(std::string&)' 

ok here is ther part that isnt working...

for (int i = 0; i < count ; i++)
{
cout << "\n\n\n\n";
cout <<"Please Enter At bats";
cin >> At_Bat;
cout <<"Please Enter total hita";
cin >> Hit;

while (At_Bat >= Hit);
Array_avg[Average] = Hit/At_Bat;

}


}

and this is whole code....

#include <iostream>    
#include <string>
#include <cctype>  
#include <iomanip>
using namespace std;

//function prototype goes outside main
string format_name(string Name);

int main()
{
    string Name = "";

    //local constants
    int Average = 0;
    int At_Bat = 0;
    int Hit = 0;
    const int MAX = 100;
    string QUIT = "Q";  


    //local variables   
    string Array_names[MAX] = {""};
    int Array_avg[MAX] = {0};




/*****************************************************************/
int count = 0;
for (count = 0; count <= 10 ; count ++)
{    
     cout <<"\n\n\n\n\n";    
     cout << setw(60) << "Please enter player name, or Q to quit";    
     cout << "\n";
     QUIT = toupper (QUIT);
     cin >> Name;   

     if(Name == QUIT) 
     break; //exit the loop 
      else{          

            //call function to put name in array    
            Array_names[count] = format_name(Name);      
            }  
}
/*********************************************************/

for (int i = 0; i < count ; i++)
{
cout << "\n\n\n\n";
cout <<"Please Enter At bats";
cin >> At_Bat;
cout <<"Please Enter total hita";
cin >> Hit;

while (At_Bat >= Hit);
Array_avg[Average] = Hit/At_Bat;

}


}


/**********************************************************************
 *Program Name   : 
 *Author         : 
 *Date           : 
 *Course/Section : CSC 110
 *Program Description:  This function is designed to format a string
 *  to be 12 characters long.  If the string is longer than 12 chars
 *  then the string will be truncated.  If the string is shorter than 
 *  12 characters then it will be padded with spaces at the end.
 *
 *BEGIN - Format Name (input string)
 *   Init pad variable to # of chars the name is too long or too short
 *   IF (input string is too short)
 *      Pad the string with spaces
 *   ELSE //input string is too long
 *      Truncate the unwanted characters
 *   END IF
 *   Return the new formatted string
 *END - Format Name
 *********************************************************************/

string format_name(string Name)
{
    //local constants
    const int WIDTH = 12;

    //local variables
    int Pad = WIDTH - Name.length(); //# of chars too long or too short

    /***********begin function body*****************************/

    //if the name is shorter than the desired width
    if (Pad > 0)

        //pad the name with spaces so the length is correct
        for (int Count = 0; Count < Pad; Count++)
            Name = Name + " ";

    else //the name is longer than the desired width

        //truncate all characters after the desired width
        Name = Name.substr(0, WIDTH);

    //return the new string
    return Name;

}//end format name

36 no matching function for call to `toupper(std::string&)'

The above means that the toupper() doesn't take a std::string (also known as an STL string object). toupper() takes a single char. The STL string class may have a toupper() function built in, but if so it's syntax would be something like this:

string s;
cin >> s;
s.toupper();

but my online reference, cppreference, doesn't indicate there is a toupper() method for the string class.

You can change each char in the std::string by passing it to toupper and then reassigning it back to the same index.

Alternatively, you can change QUIT to be a single char, 'Q', and pass the user input as a single char to toupper().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.