Error= while/for

m having problem with my code i need my character to move down the stairs , however i get an error message saying char movement is being used without being initialized

how do i overcome this: here is my code :

#include<iostream>
#include"110ct.h"
#include<string>

using namespace std;

int main()
{
CursorController crs;
ColourController cl;


int Number;// the number of stairs
int Amount = 1; // this is the amount of x it will add to the line

string A;// this is the variable for the character
char movement;// this is the variable to move the character

double xval ,yval;
xval= 20;// the set x postion coordinates
yval = 20;//the set x postion coordinates


cout << "Enter in how many 'stairs' you want" << endl;
cin >> Number;
cout << " now enter a special character to move up and down the starirs \n";
cin >> A;

for(int i = 0; i < Number; i++)
{
for(int j = 0; j < Amount; j++)
{
cout << "*";//the charater for the stair
}

cout << endl;
Amount +=1; //Adds 1 'X' after every new line

}

while( movement =='i')
{
yval=yval-0.5;
crs.setPosition(xval,yval);
cout << A <<"\n";
qin>>movement;
crs.clearAll();
cout<< " to move the special character press the following keys \n";
cout<< " press i for up \n k for down \n j for left and l for right \n";// intructions for the keys

}
while( movement == 'k')
{
yval=yval+0.5;
crs.setPosition(xval,yval);
cout << A <<"\n";
qin>>movement;
crs.clearAll();
cout<< " to move the special character press the following keys \n";
cout<< " press i for up \n k for down \n j for left and l for right \n";// intructions for the keys

}
while( movement == 'j')
{
xval=xval-0.5;
crs.setPosition(xval,yval);
cout << A <<"\n";
qin>>movement;
crs.clearAll();
cout<< " to move the special character press the following keys \n";
cout<< " press i for up \n k for down \n j for left and l for right \n";// intructions for the keys

}
while( movement == 'l')
{
xval=xval+0.5;
crs.setPosition(xval,yval);
cout << A <<"\n";
qin>>movement;
crs.clearAll();
cout<< " to move the special character press the following keys \n";
cout<< " press i for up \n k for down \n j for left and l for right \n";// intructions for the keys
}
while(movement!='x')

qin>> movement;
cout<< " you have terminated the program \n";



cin.get();

return 0;
}
while( movement =='i')

When your code gets to here, what value does movement have? What did you initialise it to?
Last edited on
i set the movement as a Char movement
Okay, let me put it another way.

What do you expect the following to output?

1
2
3
4
5
6
#include <iostream>
int main()
{
  char movement;
  std::cout << movement; // What will this put on screen? What has the variable movement been set to?
}
it is suppose to output a keypress movement of a chosen character for example if the user presses 'i' it moves up
Do you understand that every variable has a value?

int x = 7;
What is the value of x here? Can you tell me that?
That answer is 7.

char b = 'a';
What is the value of b here? The answer is 'a'.

char movement;
What is the value of movement here? Can you tell me? No you can't, because it could be anything. It has not been initialised. The variable movement has been created, but it has not been set to a value.

In your code, you do this:
while( movement =='i') before you set a value in movement. How can you try to compare it? It could be anything. It could be 'z'. It could be '4'. It could be 'i'. It could. Be. Anything.
Last edited on
Topic archived. No new replies allowed.