0

This is what I am suppose to do: Write a program that maintains a high score table of five entries, where each entry is made up of a player's name and score. The table should store the entries in order, from highest score to lowest. Your program should initialize the table using your name and 1000 for all five entries. Next, your program should display the entries and allow a player to add a new one. Your program should accept a name and a score for the potential new entry. If the score is greater than or equal to the lowest score in the table, your program should insert the new entry at the correct position, adjust the rest of the table accordingly, and display a message saying that the entry was inserted. Otherwise, your program should display a message telling the player that the score was too low for the entry to be added. Your program should continue to display the table and allow the player to add new entries as long as he or she wants. (Hint: Your program doesn't have to do a full sort of the entries when a new one is added. You can assume that the entries are already sorted; a new entry needs only to be inserted at the correct position with the rest of the table adjusted accordingly)

This is how far I have gotten and I believe that I have completely lost myself. I am getting these errors:

high_score_2.cpp: In function ‘int main()’:
high_score_2.cpp:28: error: ‘x’ was not declared in this scope
high_score_2.cpp: In function ‘void shift(score_struct*, std::string&, int, int&)’:
high_score_2.cpp:114: error: invalid initialization of reference of type ‘std::string&’ from expression of type ‘int’
high_score_2.cpp:100: error: in passing argument 2 of ‘void highScore(score_struct*, std::string&, int, int&)’
high_score_2.cpp:120: error: invalid initialization of non-const reference of type ‘std::string&’ from a temporary of type ‘int’
high_score_2.cpp:100: error: in passing argument 2 of ‘void highScore(score_struct*, std::string&, int, int&)’

I may be hitting the block wall and not seeing what I am missing. It could be something simple, but it also may not be any help would be appreciated.

#include<iostream>

#include<iomanip>

#include<string>

#include<sstream>



using namespace std;

const int SIZE = 5;//Constant for the size of the array



struct score_struct//Struct declaration

{

     string name;//string declaration in the struct class 
     int score;//int declaration in the struct class

};



void getInput(score_struct* scores, string& name, int& int_score);//getInput function declaration

void createList(score_struct* scores);//createList function declaration

void search(score_struct* scores, string& name, int x, int& int_score);//search function declaration

void highScore(score_struct* scores, string& name, int x, int& int_score);//sethigh function declaration

void shift(score_struct* scores, string& name, int x, int& int_score);



int main()

{

     score_struct* scores = new score_struct[SIZE];//creates a new array pointer

     string name;//variable used for new name insertion

     int int_score;//variable used for new score insertion

     createList(scores);//function call for createList

     getInput(scores, name, int_score);//function call for getInput

     search(scores, name, x, int_score);//function call for search 

     //highScore(scores, name, x, int_score);//function call for sethigh

     //shift(scores, name, x, int_score);//function call for shift

     return 0;

}



void getInput(score_struct* scores, string& name, int& int_score)//function definition for getInput

{

     string score;//variable used locally

     cout << endl << endl;//creates space when program runs

     cout << "Congratulations!!! Please enter your name: \n";//asks for input from user

     getline(cin,name);//gets information from keyboard

     //cout << name;//test to see if name is working properly

     cout << "Please enter your score: \n";//asks for score from user

     getline(cin,score);//gets information from keyboard

     //cout << score;//test to see is score is working properly

     stringstream ss (score);//used to copy the input from the string into the int

     if ( (ss >> int_score).fail() )//checks to see if the input being converted is a number

     {

          cout << "Your score was not correct, please try again!";//Informs the user they should try again

          getInput(scores,name,int_score);//function call to repeat the asking if the inproper information is entered

     }

}



void createList(score_struct* scores)//function definition for createList

{    
     int score = 1000;

     for ( int i=0; i<SIZE;i++ )
     {
          scores[i].name = "Your Name";
          scores[i].score = score;
     }



     cout << endl << endl;//creates space when program begins

     cout << "High Scores List: \n" << endl;//prints out the title

     for ( int i=0; i<SIZE; i++)//for loop to populate the array

          cout << scores[i].name << setw(15) << scores[i].score << endl;//used to print out the list

}



void search(score_struct* scores, string& name, int x, int& int_score)//function definition for search

{

     for ( int i=0; i<SIZE; i++ )//used to iterate across the array

     {

          if (int_score > scores[i].score)//used to check if int_score is greater tha scores[0].score

          {
               if ( int_score > scores[0].score)

	       {

                    cout << "You now have the highest score!\n" << endl;//informs the user they are now the highest scorer
                    shift(scores, name, x, int_score);

                    highScore(scores, name, x, int_score);//setHigh function call
                    //createList(scores);//createList function call
                    break;//used stop looping 
     	       }

          }

          else if ((int_score >scores[(i+1)].score) && (int_score<scores[i].score))//used to check if player has a sufficient score to list

          {

               cout << "Your score is: #" << (i+2) << endl;//used to inform player what there spot on the list is

               shift(scores, name, x, int_score);

               highScore(scores, name, x, int_score);//setHigh function call
	       createList(scores);//createList function call

               break;//used stop looping

          }

          else if (int_score < scores[(i+1)].score)//used to check if int_score is lower than an existing low score

          {

               cout << "Your score was to low! Good luck next time!\n";//Informs user they do not have a sufficiently high score

               break;//used stop looping

          }

     }

     

}



void highScore(score_struct* scores, string& name, int x, int& int_score)//function definition for setHigh

{

     //cout << name << " AND " << int_score;//used to test if passed to function
          scores[x].score = int_score;
	  scores[x].name = name;

}



void shift(score_struct* scores, string& name, int x, int& int_score)

{

    score_struct fill[SIZE]; //local object for shift function
    int place;
    for ( int i = 0; i < SIZE; i++ )
    {
          highScore(scores, x, name, int_score);//calls the highScore function
    }
    for ( int i = place; (i < SIZE); i++ )//this is where the shifting happens
    {
          if (i==0) 
          {
	       highScore(scores,(x+1), fill[(x+1)].score, fill[(x+1)].name); //

          } 

    } 

}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by triumphost
0

In main, you have not declared what type x is.. whether is is int, double or float.. I have declared it for u as int type as your other functions show that you want it as int.

invalid initialization of reference of type ‘std::string&’ from expression of type ‘int’

thats because here:

void shift(score_struct* scores, string& name, int x, int& int_score)
//declares that the function should return in this order.. scores, name, int x, score...

//Instead you have it returning score, int x, name, int_score.. when you should have switched around name and x...
highScore(scores, x, name, int_score);

as for the other errors which I have not fixed, your functions are returning more arguments than they are declared for.. or they are being passed more than the amount they should hold due to your declarations.

You can either declare x BEFORE function main() or inside main();
#include<iostream>

#include<iomanip>

#include<string.h>

#include<sstream>

#include <string>



using namespace std;

const int SIZE = 5;//Constant for the size of the array



struct score_struct//Struct declaration

{

     string name;//string declaration in the struct class 
     int score;//int declaration in the struct class

};



void getInput(score_struct* scores, string& name, int& int_score);//getInput function declaration

void createList(score_struct* scores);//createList function declaration

void search(score_struct* scores, string& name, int x, int& int_score);//search function declaration

void highScore(score_struct* scores, string& name, int x, int& int_score);//sethigh function declaration

void shift(score_struct* scores, string& name, int x, int& int_score);



int main()

{
    int x;

     score_struct* scores = new score_struct[SIZE];//creates a new array pointer

     string name;//variable used for new name insertion

     int int_score;//variable used for new score insertion

     createList(scores);//function call for createList

     getInput(scores, name, int_score);//function call for getInput

     search(scores, name, x, int_score);//function call for search 

     //highScore(scores, name, x, int_score);//function call for sethigh

     //shift(scores, name, x, int_score);//function call for shift

     return 0;

}



void getInput(score_struct* scores, string& name, int& int_score)//function definition for getInput

{

     string score;//variable used locally

     cout << endl << endl;//creates space when program runs

     cout << "Congratulations!!! Please enter your name: \n";//asks for input from user

     getline(cin,name);//gets information from keyboard

     //cout << name;//test to see if name is working properly

     cout << "Please enter your score: \n";//asks for score from user

     getline(cin,score);//gets information from keyboard

     //cout << score;//test to see is score is working properly

     stringstream ss (score);//used to copy the input from the string into the int

     if ( (ss >> int_score).fail() )//checks to see if the input being converted is a number

     {

          cout << "Your score was not correct, please try again!";//Informs the user they should try again

          getInput(scores,name,int_score);//function call to repeat the asking if the inproper information is entered

     }

}



void createList(score_struct* scores)//function definition for createList

{    
     int score = 1000;

     for ( int i=0; i<SIZE;i++ )
     {
          scores[i].name = "Your Name";
          scores[i].score = score;
     }



     cout << endl << endl;//creates space when program begins

     cout << "High Scores List: \n" << endl;//prints out the title

     for ( int i=0; i<SIZE; i++)//for loop to populate the array

          cout << scores[i].name << setw(15) << scores[i].score << endl;//used to print out the list

}



void search(score_struct* scores, string& name, int x, int& int_score)//function definition for search

{

     for ( int i=0; i<SIZE; i++ )//used to iterate across the array

     {

          if (int_score > scores[i].score)//used to check if int_score is greater tha scores[0].score

          {
               if ( int_score > scores[0].score)

	       {

                    cout << "You now have the highest score!\n" << endl;//informs the user they are now the highest scorer
                    shift(scores, name, x, int_score);

                    highScore(scores, name, x, int_score);//setHigh function call
                    //createList(scores);//createList function call
                    break;//used stop looping 
     	       }

          }

          else if ((int_score >scores[(i+1)].score) && (int_score<scores[i].score))//used to check if player has a sufficient score to list

          {

               cout << "Your score is: #" << (i+2) << endl;//used to inform player what there spot on the list is

               shift(scores, name, x, int_score);

               highScore(scores, name, x, int_score);//setHigh function call
	       createList(scores);//createList function call

               break;//used stop looping

          }

          else if (int_score < scores[(i+1)].score)//used to check if int_score is lower than an existing low score

          {

               cout << "Your score was to low! Good luck next time!\n";//Informs user they do not have a sufficiently high score

               break;//used stop looping

          }

     }

     

}



void highScore(score_struct* scores, string& name, int x, int& int_score)//function definition for setHigh

{

     //cout << name << " AND " << int_score;//used to test if passed to function
      scores[x].score = int_score;
	  scores[x].name = name;

}



void shift(score_struct* scores, string& name, int x, int& int_score)

{

    score_struct fill[SIZE]; //local object for shift function
    int place;
    for ( int i = 0; i < SIZE; i++ )
    {
          highScore(scores, name, x, int_score);//calls the highScore function
    }
    for ( int i = place; (i < SIZE); i++ )//this is where the shifting happens
    {
          if (i==0) 
          {
	       highScore(scores,(x+1), fill[(x+1)].name, fill[(x+1)].score); //

          } 

    } 

}

Edited by triumphost: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.