Jump to content

adding the top score..

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
joie2

joie2

    Newbie

  • Members
  • PipPip
  • 17 posts
here's my code but i wanted to add in the case 2 the top score..from the time a player would play the score would be recorded and if it will be higher than the other it will be display as top 1.


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

int main()
{
 string n;
 char e,f,g,h,i,j,k,l,p;
 int o,nscore=0;
 cout<<"Select Option:New Game(1)\nTop Score(2)\nQuit(3)\n";
 cin>>o;
 switch (o)
 {
 case 1:
  cout<<"Enter Name:";
  cin>>n;
  cout<<"***Easy Mode***\n";
  
    cout<<"1.What is the name of our school director?\na.Ernalyn b. Ms. Dean\n";
    cin>>e;
    if (e!='b'){
     nscore = nscore +10;
     cout<<"Correct!Score:"<<nscore;}
    else cout<<"Wrong!";
     
    cout<<"\n2.Who is the worst guard?\na.Marko b.Marco\n";
    cin>>f;
    if (f!='a'){
     nscore = nscore +10;
     cout<<"Correct!Score:"<<nscore;}
    else
     cout<<"Wrong";
    cout<<"\n3.What object do we use in brushing our teeth?\na.toothbrush b.hairbrush\n";
    cin>>g;
    if (g!='b'){
     nscore = nscore +10;
     cout<<"Correct!Score:"<<nscore;}
    else
     cout<<"Wrong\n";

   cout<<"\n***Normal Mode***\n";
    cout<<"1.What is the national anthem of the Phil?\na.Bayang Magiliw b. Lupang Hinirang\n";
    cin>>h;
    if (h!='a'){
     nscore = nscore +20;
     cout<<"Correct!Score:"<<nscore;}
    else
     cout<<"Wrong";
    cout<<"\n2.Who is the first president of the Phil?\na.Emilio Aguinaldo b. Rogin Villaruz\n";
    cin>>i;
    if (i!='b'){
     nscore = nscore +20;
     cout<<"Correct!Score:"<<nscore;}
    else
     cout<<"Wrong";
    cout<<"\n3.What is the shortcut key for shutting down computer?\na.Shift A b. Alt F4\n";
    cin>>j;
    if (j!='a'){
     nscore = nscore +20;
     cout<<"Correct!Score:"<<nscore;}
    else
     cout<<"Wrong\n";
 
   cout<<"\n***Hard Mode***\n";
    cout<<"1.How many percent of the people thinks that the planet earth and mars may collide?\na.No one believes b. 90%\n";
    cin>>k;
    if (k!='b'){
     nscore = nscore +30;
     cout<<"Correct!Score:"<<nscore;}
    else
     cout<<"Wrong";
    cout<<"\n2.Who is the world's richest man?\na.Bell Gates b. Bill Gates\n";
    cin>>l;
    if (l!='a'){
     nscore = nscore +30;
     cout<<"Correct!Score:"<<nscore;}
    else
     cout<<"Wrong";
    cout<<"\n3.Who is the greatest actor of all time?\na.Fernando Poe Jr. b. George Cloney\n";
    cin>>p;
    if (p!='a'){
     nscore = nscore +30;
     cout<<"Correct!Score:"<<nscore;}
    else
     cout<<"Wrong\n";
 break;
 case 2:
  cout<<"Top Score";
  break;
 case 3:
  cout<<"Quit";
  break;
 default:
  cout<<"Error";
 }
return 0;
}

Edited by WingedPanther, 08 December 2009 - 04:19 AM.
add


#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Oy vey. Please use code tags when posting code...click "Edit Post" near the bottom of your post, highlight your code, and click on the # button. That'll make everything easier to read. All you need to do is keep track of the highest score seen so far; just keep a variable called top_score and initialize it to 0. On each iteration, you compare the current score to top_score. If the current score is greater, then reassign top_score to the current score. Otherwise leave it alone.
sudo rm -rf /

#3
theonejb

theonejb

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
Put the code inbetween CODE tags.

#4
speculatius

speculatius

    Newbie

  • Members
  • PipPip
  • 25 posts
Hello,

when your program ends, all data stored in your variables will be lost. So you need to write top score into file and read it on every start of your program.

Algorithm:
1) if file topscore.txt doesnt exist, create it and write there score 0
2) read score
3) your game-logic
4) if new score is greater then topscore, remove file and create new one.
5) end

Below are links to reference manual for manipulating files. See examples for methods open, close and operators << and >>.

ofstream - C++ Reference

ifstream - C++ Reference