Welcome to Dream.In.Code
Become an Expert!

Join 149,513 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,358 people online right now. Registration is fast and FREE... Join Now!




how to show high score input

 
Reply to this topicStart new topic

how to show high score input

Leines
16 Aug, 2007 - 11:23 PM
Post #1

New D.I.C Head
*

Joined: 11 Jun, 2007
Posts: 7


My Contributions
CODE

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

struct recordType
{
    string name;
    int score;
};

void GetInput(int &input, int &size);
void GetData(recordType * &data, int &size);
void PrintHighest(recordType * &data, int &size);
void Search(recordType * &data, int &size);

void main(void)
{
    
    int input=0, size=0, temp=0;
    recordType *data;

    
    while(input!=4)
    {
        GetInput(input, size);

        switch(input)
        {
        case 1:        GetData(data, size);
                    break;
        case 2:        PrintHighest(data, size);
                    break;
        case 3:        Search(data, size);
                    break;
        case 4:     break;

        default:    return;
        }
    }
}

void GetInput(int &input, int &size)
{
    cout << "[Score Borad] " << size << " records" << endl;
    cout << "1. Enter data" << endl;
    cout << "2. Highest Score" << endl;
    cout << "3. Search" << endl;
    cout << "4. Exit" << endl;
    

    for(bool check_input=false; check_input==false; )
    {
        cout << "Choice: ";
        cin >> input;

        if(!cin)
        {
            cin.clear();
            cin.ignore(100, '\n');
            cout << "Invalid choice." << endl;
        }
        else if(input<=(-1) || input>=7)
        {
            cout << "Invalid choice." << endl;
        }
        else
        {
            check_input=true;
        }
    }
}

void GetData(recordType * &data, int &size)
{
    if(size!=0)
    {
        // Copy current data into temporary data
        // Delete current data
        recordType *temp_data = new recordType[size];
        for(int i=0; i<size; i++)
        {
            temp_data[i].name = data[i].name;
            temp_data[i].score = data[i].score;
        }
        delete [] data;

        // Copy temporary data into new data
        // Delete temporary data
        data = new recordType[++size];
        for(int i=0; i<(size-1); i++)
        {
            data[i].name = temp_data[i].name;
            data[i].score = temp_data[i].score;
        }
        delete [] temp_data;
    }
    else
    {
        data = new recordType[++size];
    }

    cout << "Enter Name: ";
    cin >> data[size-1].name;

    for(bool check_score=false; check_score==false; )
    {
        cout << "Enter Score: ";
        cin >> data[size-1].score;

        if(!cin)
        {
            cin.clear();
            cin.ignore(100, '\n');
            cout << "Numeric input required." << endl;
        }
        
        else
        {
            check_score=true;
        }
    }
    cout << endl;
}

void PrintHighest(recordType * &data, int &size)
{
    int temp_highest=0, firstPlace=0;
    if(size==0)
    {
        cout << "Please enter data before getting highest score." << endl << endl;
    }
    else
    {
        for(int i=0; i<size; i++)
        {
            // If temp_highest is smaller, change it bigger
            if(data[i].score>=temp_highest)
            {
                temp_highest = data[i].score;
                firstPlace=i;
            }
        }

        cout << "The highest score is " << data[firstPlace].score;
        cout << " by " << data[firstPlace].name << "." << endl << endl;
    }
}


void Search(recordType * &data, int &size)
{
    string getTerm;
    unsigned int count;
    cout << "Enter search term: ";
    cin >> getTerm;

    cout << "Search results:" << endl;

    for(int i=0; i<size; i++)
    {
        if(data[i].name.find(getTerm)!= string::npos)
            cout << data[i].name << " (" << data[i].score << ")" << endl << endl;
    }
}

hi i need help as i wan my high score to just show out by itself but not by typing out. Do you have any tutorials on high sore input? It will be a great help to me .Thanks.
User is offlineProfile CardPM
+Quote Post

Topher84
RE: How To Show High Score Input
17 Aug, 2007 - 10:00 AM
Post #2

D.I.C Head
Group Icon

Joined: 4 Jun, 2007
Posts: 232



Thanked: 1 times
Dream Kudos: 25
My Contributions
Is it outputting garbage? if so you need to derefence your pointer when you display it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 07:49PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month