Getting a logic error in my programming
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 80 81 82 83 84 85 86
|
#include <iostream>
void clearScreen(int lines)
{
for (int i = 0; i < lines; i++)
{
std::cout << std::endl;
}
}
struct shipSetup{
int xcoord;
int ycoord;
std::string shipName;
};
shipSetup ship;
void shipPosition(shipSetup ship)
{
std::cout << ship.shipName << "'s Galactic Positioning" << std::endl << std::endl;
std::cout << "X Position: " << ship.xcoord << std::endl;
std::cout << "Y Position: " << ship.ycoord << std::endl << std::endl;
}
int main()
{
int userInputDirection;
std::string inputBuffer;
int userInputYear = 0;
bool cont = 1;
bool validYearInput = 1;
ship.xcoord = 0;
ship.ycoord = 0;
clearScreen(100);
std::cout << "What would you like your ship to be called? ";
std::getline(std::cin, ship.shipName);
while (cont)
{
clearScreen(100);
shipPosition(ship);
std::cout << "1 = Up" << std::endl << "2 = Down" << std::endl << "3 = Left" << std::endl << "4 = Right" << std::endl;
std::cout << "What direction would you like to move? ";
std::cin >> userInputDirection;
std::cout << "How many lightyears would you like to move in the direction for? ";
std::cin >> userInputYear;
if (userInputYear <= 0)
{
std::cout << "Please enter a number larger then 0, Press \"enter\" to continue";
std::getline(std::cin, inputBuffer);
validYearInput = 0;
}
while(validYearInput)
{
if (userInputDirection == 1)
{
ship.xcoord = ship.xcoord + userInputYear;
}
else if (userInputDirection == 2)
{
ship.xcoord = ship.xcoord - userInputYear;
}
else if (userInputDirection == 3)
{
ship.ycoord = ship.ycoord - userInputYear;
}
else if (userInputDirection == 4)
{
ship.ycoord = ship.ycoord + userInputYear;
}
else
{
std::cout << "Please enter in a number 1-4, note the guide, Press \"enter\" to continue";
std::getline(std::cin, inputBuffer);
}
}
validYearInput = 1;
}
}
| |
After the user enters in the number for the lightyears, the program stops. No input and no Output. I have no idea why this is happening.
The program never gets out of the while loop on line 58. while(validYearInput)
Topic archived. No new replies allowed.