While statement not terminating

Jun 5, 2013 at 1:28am
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
#include "profile.h"
#include "default.h"
#include "Global.h"
#include "c_screen.h"
void profile::gen_prof()
{
	c_screen cObj;
	player_cpu = 0;
	player_weapon = 0;
	player_weapon2 = 0;

	srand(time(NULL));
	player_health = rand() %  20 + 10; //10-30
	player_armor = rand() % 30 + 10; //10 -40
	player_attack = rand() %  10 + 5;//5-15
	player_defence = rand() % 5 + 5;//5-10
	player_magic_power = rand() % 10 + 5;//5-15
	player_magic_gauge = rand () % 20 + 20;//20-40
	

	std::cout<<"What is your name?\n";
	std::getline(std::cin,name);
	cObj.ClearScreen();

	while (player_weapon>3||player_weapon<1){
		//couts here
		std::cin>>player_weapon;
		cObj.ClearScreen();
	}


	while (player_cpu > 3||player_cpu < 1){
                //couts here
		std::cin>>player_cpu;
		cObj.ClearScreen();
	}

	if (player_cpu == 1)
		player_armor +=10;	
	else if (player_cpu == 2){
		while (player_weapon2 >3||player_weapon2 <1){
                        //couts here
			std::cin>>player_weapon2;
			cObj.ClearScreen();
		}
	}
	else if (player_cpu == 3){
		player_attack += 10;
	}
}

When I test this code the program starts to loop at the first while and never stop, no matter what I enter. Is there something I'm doing wrong? If you need to know the variables I'm inputting are global variables in another file(Global.h)
Jun 5, 2013 at 1:38am
the loop itself seems fine, i think the problem is in cscreen::ClearScreen().
Jun 5, 2013 at 1:40am
Removing that statement didn't do anything.
Jun 5, 2013 at 2:05am
Does it actually ask you for your weapon?
Jun 5, 2013 at 2:14am
This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdlib>
using namespace std;

void clearScreen ()
{return;} // do nothing

int main ()
{
    int player_weapon=0;

    clearScreen ();
    while (player_weapon<1 || player_weapon>3)
    {
         cout << "Enter a valid weapon: ";
         cin >> player_weapon;
         clearScreen();
    }

    system ("pause");
    return 0;
}


When I run this, it works fine. But, if I change clearScreen to be:
1
2
void clearScreen ()
{while(true);} // infinite loop 


it doesn't do any output, it just infinite loops.
Jun 5, 2013 at 2:47am
That doesn't really help. When my code reaches the while, it keeps asking for the players weapon choice. Even if you choose in its selection, it repeats anyways. The ClearScreen class doesn't affect other classes like this one.
Jun 5, 2013 at 5:36am
Sorry but I actually have no idea why it does that.
Jun 5, 2013 at 7:33pm
I fixed it, i need ' ' around the numbers being compared
Jun 7, 2013 at 7:26am
oh right.

is player_weapon a char?
Jun 7, 2013 at 10:55am
input from the command line will be.
Topic archived. No new replies allowed.