Small problem with looping program

Microsoft Windows vista home premium sp1...
February 1, 2010 at 19:47:03
Specs: Windows Vista
Well, Ive never tried to code anything on my own before. I actually just began trying to learn how to program in C++ yesterday, using online tutorials. Today, i decided to apply what i had learned and try to make a "game."
#include <iostream>
using namespace std;
main ()
{
     int EXP = 0 , NEEDEDEXP = 4, LEVEL = 1, POWER = ( LEVEL * 2 ), INPUTTEDNUMBER;
     cout<< " RUNESCAPE LEVELING SIMULATOR!\n";
     cout<< "Welcome. Lets see what level you can get to! If you forgot, use 1-5 and enter to harvest wood. enter anything not 1-5 to find out your current level\n";
     top:
         
     cin>> INPUTTEDNUMBER;
     if ( INPUTTEDNUMBER == ( 1,2,3,4,5 ) ) {
          EXP + POWER;
          if ( NEEDEDEXP <= EXP ) {
               NEEDEDEXP * 4;
               LEVEL + 1;
               goto top;
               }
               
          else {
               goto top;
               }
               }
     else {
          cout<< "YOU ARE LEVEL" << LEVEL <<endl;
          goto top;
          }
          }              

The main idea is to have an input that affects your characters EXP, and when EXP reaches a certain level (NEEDEDEXP), both your level and your NEEDEDEXP will increase. As your level increases, so does your ability to get EXP. However, when i input a 1 or 2, it tells me that i am level 1. If i input it again, it tells me the same thing. I was wondering if anyone could find the error in the code and tell me what i did wrong. Remember, 2 days experience, don't pretend that i know much of anything.

See More: Small problem with looping program

Report •

#1
February 1, 2010 at 20:52:49
The biggest problem I can find is that the math it's doing doesn't assign to anything.

NEEDEDEXP * 4;

Needs to be:

NEEDEDEXP = NEEDEDEXP * 4;

or

NEEDEDEXP *= 4;

I also removed the goto/label and replaced it with a while loop. Where you were using "goto top;" you can used "continue;" instead, to exit "iterate = false;" or "break;".

#include <iostream>
using namespace std;
int main ()
{
     int EXP = 0 , NEEDEDEXP = 4, LEVEL = 1;
     int POWER = ( LEVEL * 2 ), INPUTTEDNUMBER;
     cout << " RUNESCAPE LEVELING SIMULATOR!" << endl;
     cout << "Welcome. Lets see what level you can get to! "
          << "If you forgot, use 1-5 and enter to harvest"
          << "wood. enter anything not 1-5 to find out your"
          << "current level" << endl;
     bool iterate = true;
     while (iterate)
     {
         cin >> INPUTTEDNUMBER;
         if ( INPUTTEDNUMBER > 0 && INPUTTEDNUMBER < 6 ) {
              EXP += POWER;
              if ( NEEDEDEXP <= EXP ) {
                   NEEDEDEXP *= 4;
                   LEVEL++;
              }
         } else {
              cout<< "YOU ARE LEVEL" << LEVEL <<endl;
         }
     }
     return 0;
}       


Batch Variable how to


Report •

#2
February 1, 2010 at 21:26:31
just basic concepts, but you'll get the hang:
var assignment always involves placing the var. name on the left of the assignment (C, and most other langs it's "=", in pascal its ":=")
f/e: level+1
doesn't do anything (or not that i can tell). All your var. assignments need be like:
level=level+1
or (in C and some others:)
level+=1
or
level++
but C will let you do it the "traditional" way as well (level=level+1)

and i don't know what this does or is supposed to do or doesn't do:
if ( INPUTTEDNUMBER == ( 1,2,3,4,5 ) )
i thought it was someting i didn't know about in C and took it to mean: if number is 1 or 2 or 3 or 4 or 5...
but that didn't seem to work (i had to try it because i'm also very new at C, just breaking the ice so to speak)
So i'm clueless what you meant by that program-statement.

Also, the power level, which i would assume to be a multiplier for the EXP, never gets upgraded with level-up, but i'm not clear enough about your game concept to know what you intended.

good start though, didn't generate a single compiler error, unlike my first several feeb attempts which usually didn't get past line 2 before blowing up.

(oops, crossed in the mail, sorry Judago!)


Report •

#3
February 1, 2010 at 21:30:40
(oops, crossed in the mail, sorry Judago!)

No problem, it happens ;)


Batch Variable how to


Report •

Related Solutions

#4
February 2, 2010 at 12:04:28
I would like to thank you all, especially nbrane. While i kept it as a GOTO (im still figuring out while loops), i was using
NEEDED EXP * 4 = NEEDEDEXP ;
instead of
NEEDEDEXP = NEEDEDEXP * 4; .
Thanks for the tip.

Report •

Ask Question