Learning the basics

This is a little program that tells you how many levels in World of Warcraft you have left until you cap and it also says how far along you are in words. Pretty pointless program I know.
I need to hide "you are x level and have x to go" line when input is greater than 80 and make compiler read and run "enter your real level" and the "cin >> iLevel" as it did in the beginning of the program. Hoping to add a trigger to reset the program to the "enter your level" part after it has run its course.
ps. Would anyone recommend the book C++ without Fear by Brian Overland?
Sorry for the 3 in 1 post. Heres the code Im working with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
//Silly first program spawned from hello world program.

using namespace std;
//these is comments not read by compiler " // " also " /* */ "

int main()
{

// Declare intergers here  !important!
    int iLevel;
    int iDifference;


    cout << "Hello! This is my first program. Just play along.\nWhat Level is your WoW Character?"<< endl;
    cout << "\nENTER LEVEL:";
    cin >> iLevel;
    cin.ignore(); // still not sure what this does.



cout<< "\n"; //makes a break for formating maybe a better solution?



    iDifference = 80 - iLevel;

    cout<< "You are Level " << iLevel << " and have " << iDifference << " levels to go."<< endl;


//----------------------------------------------------------------------*
//----------------------------------------------------------------------*
    if ( iLevel < 30 )
  cout<<"You are low Level. Close this program and score some XP now!";



    if ( iLevel >= 31 && iLevel <= 59 ) //Fixed with && operators to create a range :)
  cout<<"You are of moderate level ";


    if ( iLevel >= 60 && iLevel <= 79 )
  cout<<"You are high level ";



    if (iLevel == 80)
  cout<< "Congrats! You are the highest level in wow. ";
//----------------------------------------------------------------------
    if ( iLevel >= 81 )//Takes care of input over level cap like levels 100 or 81
    {
  cout<<"80 is the highest level...\n";
  cout<<"\n";
  cout<<"ENTER YOUR REAL LEVEL:";
  cin >> iLevel;//something is wrong here
cin.ignore();
    }
//Need to hide "you are x level and have x to go" line when input is greater than 80 and make compiler read
//and run "enter your real level" and the "cin >> iLevel" as it did in the begining of the program.


//----------------------------------------------------------------------*
//----------------------------------------------------------------------*

cin.get(); //what about "return(0);"? whats the difference?


}
Last edited on
Here is another attempt. It performs more like I want it to but not in the right way. I want it to be able to handle the user breaking the rules by not giving a value between 1-80 even if they enter 10 wrong variables before a good one. Currently you only have one chance. New code..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
//Silly first program spawned from hello world program.

using namespace std;


int main()
{

    int iLevel;
    int iDifference;


    cout << "Hello! This is my first program.\nWhat Level is your WoW Character?"<< endl;
    cout << "\nENTER LEVEL:";
    cin >> iLevel;
    cin.ignore();

cout<< "\n"; //makes a break for formating maybe a better solution?

    iDifference = 80 - iLevel; //Mathy stuff




    cout<< "You are Level " << iLevel << " and have " << iDifference << " levels to go."<< endl;


    if ( iLevel < 30 )
  cout<<"You are low Level. Close this program and score some XP now!";

    if ( iLevel >= 31 && iLevel <= 59 )
  cout<<"You are of moderate level ";

    if ( iLevel >= 60 && iLevel <= 79 )
  cout<<"You are high level ";

    if (iLevel == 80)
  cout<< "Congrats! You are the highest level in wow. ";

//----------------------------------------------------------------------*
//----------------------------------------------------------------------*
   //Takes care of input over level cap like levels 100 or 81 or tries to :( The cap is 80
    if ( iLevel >= 81 )
    {
  cout<<"80 is the highest level..." << endl;
  cout<<"\n";
  cout<<"ENTER YOUR REAL LEVEL:";
  cin >> iLevel;
cin.ignore();

//------------------------------------------
    iDifference = 80 - iLevel;

   cout<< "You are Level " << iLevel << " and have " << iDifference << " levels to go."<< endl;



    if ( iLevel < 30 )
  cout<<"You are low Level. Close this program and score some XP now!";

    if ( iLevel >= 31 && iLevel <= 59 )
  cout<<"You are of moderate level ";

    if ( iLevel >= 60 && iLevel <= 79 )
  cout<<"You are high level ";

    if (iLevel == 80)
  cout<< "Congrats! You are the highest level in wow. ";
//------------------------------------------

    }


cin.get(); //what about "return(0);"? whats the difference?


}
cout<< "\n"; //makes a break for formating maybe a better solution?

you can use cout << endl; does the same thing pretty much, but better formatting you will learn later on is printf.
/n (newline) and endl; (end line) are fine for now.

you can use a sentinel loop for this for correct data.

1
2
3
4
5
6
7
8
9
10

do {

    cout << "Hello! This is my first program.\nWhat Level is your WoW Character?"<< endl;
    cout << "\nENTER LEVEL:";
    cin >> iLevel;
    cin.ignore();


} while (iLevel>80 || iLevel<=0) ;;


then you can add some error statements inside the loop to tell them to enter correct data....

1
2
3
4
5
6
7
8
9
10
11
12
do {

    cout << "Hello! This is my first program.\nWhat Level is your WoW Character?"<< endl;
    cout << "\nENTER LEVEL:";
    cin >> iLevel;
    cin.ignore();

    if (iLevel>80) {
        cout << "Level entered is to HIGH! try again..." << endl;
    }

} while (iLevel>80 || iLevel<=0) ;;



lets extend that if statement to include negative values...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
do {

    cout << "Hello! This is my first program.\nWhat Level is your WoW Character?"<< endl;
    cout << "\nENTER LEVEL:";
    cin >> iLevel;
    cin.ignore();

    if (iLevel>80) {
        cout << "Level entered is to HIGH! try again..." << endl;
    }
    else if (iLevel<1) {
        cout << "Level entered is too LOW! try again..." << endl;
    }

} while (iLevel>80 || iLevel<=0) ;;
Last edited on
you can use cout << endl; does the same thing pretty muchp

Usually you DO use '\n', only use std::endl if you want to flush the buffer as well (in this case, print the data to the screen immediately)

but better formatting you will learn later on is printf.

What? printf() is C, and is slightly harder to use anyway.

/n (newline) and endl; (end line) are fine for now.

FYI, newline is \n, but they are actually quite standard.

Also, I would add some sort of a check to make sure they entered an integer, something like:

1
2
3
4
5
6
7
//...
while(!(cin >> iLevel)) {
    cout << "Input a number.\n";
    cin.clear();
    cin >> iLevel;
}
//... 
Last edited on
/n was a typo...

you can use cout << endl; does the same thing pretty muchp


^^ I never said that, muchp isn't a word... it's spelled much

EDIT:

What? printf() is C, and is slightly harder to use anyway.


He was asking about formatting so I made a suggestion that he could look up, printf is simple to use once you spend 2 minutes RTFM.. But being that we are using console here and simply trying to add some blank lines, \n and endl are fine. FYI C++ is descendant of C, printf works fine in C++, Java, C# and 101 other languages.

FYI, newline is \n, but they are actually quite standard.


I meant to say fine for this... but w/e
Last edited on
^^ I never said that, muchp isn't a word... it's spelled much

2 line of your first reply. And yeah, it's spelled much.

He was asking about formatting so I made a suggestion that he could look up, printf is simple to use once you spend 2 minutes RTFM.. But being that we are using console here and simply trying to add some blank lines, \n and endl are fine. FYI C++ is descendant of C, printf works fine in C++, Java, C# and 101 other languages.

Indeed, but it is still C, and not C++. It is there for compatibility with C code, not because it is good C++. You would normally use stream manipulators to do this with std::cout.

Btw, sorry if I annoyed you by pointing out that typo, I didn't intend to...
it's cool I probably shouldn't be so defensive on a forum, where I am a noobie anyway :P
Thanks! Amazing replies! you guys have truly unlocked doors for me but after playing with it for about an hour now I still have a few bugs that are beyond my knowledge and Im positive I just didnt place the integer check right.
1. If a user enters 10 or more #s the programs loops to death
2. Enter a Letter and it loops "Input a number" to death and doesnt return to "ENTER LEVEL"
3. When you enter an out of range number 'x<0 or x>80' it tell you so and to try again. enter another wrong value and it says "What level is your WoW Char. ? - enter level" again. Do another wrong value and keeps this pattern. Not a HUGE issue but Id like to know why? Its inconsistent for a finished product. Newest Code btw THANKS!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
//Silly first program spawned from hello world program.
using namespace std;

int main()
{

    int iLevel;
    int iDifference;

do{ //note: begins ?"Sentinel Loop"? find out what this is later.
    cout << "What Level is your WoW Character?"<< endl;
    cout << "\nENTER LEVEL:";
    cin >> iLevel;
    cin.ignore();

//--- Checks
//these check that the variable is in range

    if (iLevel>80) {
        cout << "Level entered is to HIGH! try again...\n" << endl;
    }
    else if (iLevel<1) {
        cout << "Level entered is to LOW! try again...\n" << endl;
    }
while(!(cin >> iLevel)) { //probably very wrong. its never as easy as cut and paste.
    cout << "Input a number.\n";
    cin.clear();
    cin >> iLevel;
}
//--------------------------------

} while (iLevel>80 || iLevel<=0) ;;



cout<< "\n";

    iDifference = 80 - iLevel;

    cout<< "You are Level " << iLevel << " and have " << iDifference << " levels to go."<< endl;

    if ( iLevel < 30 )
  cout<<"You are low Level. Close this program and score some XP now!";

    if ( iLevel >= 31 && iLevel <= 59 )
  cout<<"You are of moderate level ";

    if ( iLevel >= 60 && iLevel <= 79 )
  cout<<"You are high level ";

    if (iLevel == 80)
  cout<< "Congrats! You are the highest level in wow. ";

cin.get(); //what about "return(0);"? whats the difference?
}
A do {} while(); loop is like a while loop, except it executes at least once even if the condition in the while loop is fulfilled already. http://www.cplusplus.com/doc/tutorial/control/ for website tutorial which probably explains it 2x better :D

1
2
3
4
do
{
    //statement
} while (condition); //note ; after the while 


To check if the input is a number / integer, you can use a method portrayed here: http://www.cplusplus.com/forum/articles/6046/ .

Instead of checking if iLevel is valid or not before getting input, you should swap the order.
int iLevel; - you have no idea what value is given to that variable.
also line 26 you forgot to close the DO braces...

a while loop will execute while a condition is true it gets to the bottom of the loop and loops back up to the statement to check if it is true or not then executes again if so.
Do while loop will execute and then check if the condition is true, if so it will execute again.

You code will work, but more than likely will only execute once, if the user enters in 89 for example, the do{ will print out you level is too high, and then check the while loop, the while loop wants to reloop if the input is not an int, and 89 is an int so will simply drop out and that will be the end of that.
a combination of our 2 examples you could use in this fashion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

while(!(cin >> iLevel)) 
{
    cout << "Input Wow level.\n";
    cin.clear();
    cin >> iLevel;
}

// the user will now enter a interger, this we know because we have trapped him in the sentinel 
// loop until he entered an integer. So now we need to check if the level entered was 1-80.

while (iLevel>80 || iLevel<1)
{
    if (iLevel>80) {
        cout << "Level entered is to HIGH! try again..." << endl;
    }
    else if (iLevel<1) {
        cout << "Level entered is too LOW! try again..." << endl;
    }
    cout << "Enter real level:" << endl;
    cin >> clear();
    cin >> iLevel;
}

Last edited on
Thanks everyone the methods you showed me seem cleaner. Ill let my brain suck on this for a few days and come back when I understand.
while(!(cin >> iLevel))
can be confusing at first.

what it is saying is "if iLevel is NOT an interger then keep looping".
because we have not assigned a value to iLevel yet, it will be true at least once and ask the user for input.
then if the user inputs something that is Non-int for example a string, the statement will be true again and ask the user to input value for iLevel and so on and so forth untill the user enters an integer.

lets say the person enters 109 as his level, then while(!(cin >> iLevel)) will be false, as 109 is an integer. so it will drop out of the loop into the second loop which checks if the level entered is within the range of 1-80.

because it is not it will ask him to re-enter the iLevel...

Do you get that so far? if so read on....

But lets say for example while in the while(range) he enters a string for iLevel, now we are back at problem one, but we are in the wrong loop to check for it...

this is where imbedded loops shine, if we need 2 or more conditions to hold true, then we can imbed loops within each other. eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
while(!(cin >> iLevel)) 
{
    cout << "Input Wow level.\n";
    cin.clear();
    cin >> iLevel;

    while (iLevel>80 || iLevel<1)
    {
        if (iLevel>80) {
            cout << "Level entered is to HIGH! try again..." << endl;
        }
        else if (iLevel<1) {
            cout << "Level entered is too LOW! try again..." << endl;
        }
        cout << "Enter real level:" << endl;
        cin >> clear();
        cin >> iLevel;
    }

}


Last edited on
Okay my latest attempt I marked up the code to sort of let you guys know whats going on inside my head. whatever it is its wrong. But I feel like im getting close. The lines help me see it in parts better. I need another clue. I get "no match for operator in 'myStream' Lines: 41,45,48" and " 'myStream' was not declared in this scope" Line: 56 useing Code::blocks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//DESCRIPTION: Silly first program. Tells user how many levels in WoW they have left until they cap and how good they are based on there level.
//Extensively labeled as I see how it works so far

//headers to include different code types... still hazy
#include <iostream>
#include <string>
#include <sstream>

using namespace std; //also still hazy

int main() //starts program
{ //main program start clamp

    //---Declaring Stuff--- note: Like empty containers for info unless you state different while declaring it.
    int iDifference; //For subtraction later
    int myNumber = 0;// Declares that myNumber should be a number "1,2,3,4,0"
    string myRange = "(iLevel>80 && iLevel<1)";                                      //this is wrong
    string iLevel = "";   //Declares a string named "iLevel" for the users input.

//---------------------------Integer//Range check------------------------------ Range check messed up idk
//-----------------------------Initial Prompt----------------------------------
while (true) { //Start typecheck loop. Will continue if everything inside loop is true but will run atleast once.

//------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------
   cout << "What level is your WoW Character?\n";// Prompt
   cout << "Level:";// More prompt                                //INPUT
   getline(cin, iLevel); // -user input-
//------------------------------------------------------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------
   // This code converts from string to number safely. // what he said
   stringstream myStream(iLevel); //Declares that myStream is iLevel
    if (myStream >> myNumber) //If myStream/"iLevel" is a number break the loop + no Ivalid txt       //Number Check
     break; //break the loop if above statment is true

   cout << "Not a Level\n" << endl; //runs when the "if" statment is false
//------------------------------------------------------------------------------------------------------------------------------------------

while (myStream>80 || myStream<1) {   //THIS is FUBAR

    if (myStream >> myRange)
     break;
    if (myStream>80) {
        cout << "Level to HIGH! try again...\n" << endl;                           //WTF really bad range check
    }
    else if (myStream<1) {
   cout << "Level to LOW! try again...\n" << endl;
    }
        cout << "Not a Level\n" << endl;

                                  }
}//end typecheck loop
//-----------------------------------------------------------------------------
iDifference = 80 - myStream; // mathy stuffy for level difference output.

//--------------------------Different Outputs here----------------------------- iLevel-- was changed to myStream on line 37...
    if ( myStream < 30 )
  cout<<"You are low Level. Close this program and score some XP now! ";

    if ( myStream >= 31 && myStream <= 59 )
  cout<<"You are of moderate level ";

    if ( myStream >= 60 && myStream <= 79 )
  cout<<"You are high level ";

    if ( myStream == 80)
  cout<<"Congrats! You are the highest level. ";

//----------------------------Final Output Line-------------------------------
cout<< "You are Level " << myStream << " and have " << iDifference << " levels to go."<< endl; //Final output result HERE

//---------------------------END PROGRAM STUFF---------------------------------
return(0); //Lets program know it reached the end safely by returning a 0 to main()<----in here
}//end program clamp
//----------------------------------------------------------------------------- 
Last edited on
if (myStream >> myNumber) //If myStream/"iLevel" is a number break the loop + no Ivalid txt //Number Check actually means (in my interpretation) - //if myStream can be put into an int (proves it is an int), break .
Which means you use "myNumber" instead, since that is the int.

A very detailed article by Bazzy on stringstreams, converting and the sort - http://www.cplusplus.com/articles/numb_to_text/
Last edited on
Thanks every After alot of trial and error and a ton of reading I final got. This is what I got.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {

reset:

 string input = "";
 int iDifference;
 int iNumber = 0;

 while (true)
    {
   cout << "What level is your WoW Character?\n";
   cout << "Level:";

   getline(cin, input);
   stringstream ssInput(input);
   if (ssInput >> iNumber)
    break;
    cout << "Not a Level\n" << endl;
    }

  while (iNumber>80 || iNumber<1)
    {
    if (iNumber>80) {
        cout << "Level to HIGH!\n";
        }
    else if (iNumber<1) {
        cout << "Level to LOW!\n";
        }
cout << "\n";
break;
}


while (iNumber<=80 && iNumber>=1)
{

        if (iNumber>80 || iNumber<1) continue;

   iDifference = 80 - iNumber;
   cout<< "You are Level " <<iNumber<< " and have " <<iDifference<< " levels to go."<< endl;
break;
}


while (iNumber<=80 && iNumber>=1)
{
    if ( iNumber <= 30 )
  cout<<"You are low Level. \n";

    if ( iNumber >= 31 && iNumber <= 59 )
  cout<<"You are of moderate level. \n";

    if ( iNumber >= 60 && iNumber <= 79 )
  cout<<"You are high level. \n";

    if ( iNumber == 80)
  cout<<"Congrats! You are the highest level. \n";

cout << "\n";
break;
}
goto reset;
return(0);
}
Topic archived. No new replies allowed.