# Need Help With C++

This topic is 4326 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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; ++finalScore)   {        ++finalScore;   }

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]