Sign in to follow this  
NUCLEAR RABBIT

Need Help With C++

Recommended Posts

NUCLEAR RABBIT    318
I tried making a simple scoring for when a level is completed. Im sure you all remember when you beat a mario level, your score,time,etc. is all added up and it looks like a clock is adding up really fast, Well, Thats what I was trying to do. I did it earlyer, but I forgot how I did it (i was just messing around with for loops). Can someone Please Help me! NOTE: The code works, but the score just adds, it doesnt show the numbers increasing like i was trying to make it do.
// Basic Score

#include <string>
#include <iostream>
#include <ctime>
#include <windows.h>
#include <cstdlib>


//Variables
int score = 500;
int bonus = 245;
int timeCompleted = 112;
int finalScore;

int levelComplete()
{
    std::cout << "::::::::::::::::::::::::::\n"
              << "::                      ::\n"
              << "::    Level Complete    ::\n"
              << "::                      ::\n"
              << "::::::::::::::::::::::::::\n\n"
              << "Score: " << score
              << "\nBonus Points: " << bonus
              << "\nTime: " << timeCompleted;
for(finalScore = 0; finalScore < score+bonus-timeCompleted/2; ++finalScore)
   { 
       ++finalScore;
   }
   std::cout << "Final Score : " << finalScore << " ";
}

int main()
{
    levelComplete();
    system("pause");
    return 0;
}




-- Thanks For any Help! [Edited by - NUCLEAR RABBIT on April 23, 2006 4:09:07 PM]

Share this post


Link to post
Share on other sites
yaustar    1022
1) Your for loop is incrementing finalscore twice, once in the loop and once at the end of the loop.
for(finalScore = 0; finalScore < score+bonus-timeCompleted/2; [b]++finalScore[/b])
{
[b]++finalScore[/b];
}







2) Your for loop doesn't have a cout call per loop. A for loop is only going to perform the operations between { and } per loop. Your cout call doesn't happen till after the for loop has finshed.

3) There also seems to be a flaw with your code logic as it is performing something different to what you want by the sounds of things.

4) Global variables should be avoided if possible.


/* DO NOT SCROLL IF YOU DO NOT WANT TO SEE A SOLUTION
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/


// Basic Score

#include <iostream>
#include <windows.h>

void levelComplete(int currentScore, int bonus, int timeLeft)
{
std::cout << "::::::::::::::::::::::::::\n"
<< ":: ::\n"
<< ":: Level Complete ::\n"
<< ":: ::\n"
<< "::::::::::::::::::::::::::\n\n"
<< "Score: " << currentScore
<< "\nBonus Points: " << bonus
<< "\nTime: " << timeLeft;

// Temp variable to hold the final score
// Make it equal to the current score + the bonus
int finalScore = currentScore + bonus;

// Depending on the amount of time left add 1 for each 'tick/sec'
for(int countdown = timeleft; countdown >= 0; --countdown)
{
++finalScore;
std::cout << "Final Score : " << finalScore << " ";
}
// Note that this isn't the most efficent way to do it, but the breakdown
// makes it easier to read and understand. Also I haven't tested this code ;)
}

int main()
{
levelComplete(500, 245, 112);
// Also, magic numbers (numbers that have no meaning) should be avoided
system("pause");
return 0;
}



[Edited by - yaustar on April 23, 2006 4:16:05 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this