Sign in to follow this  

Unexplainable Loop Errors?

This topic is 4854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I have a program I'm working on, to generate maps for my main Pacman project.. Source to the map editor below. However, the problem is that the first time the main input loop runs, it won't let me enter a value for "Can Move up", rather, it skips right to "Can move right", for reasons which I can not see.. If anyone could point out why this is not working correctly, please say so :) Thanks! [EDIT] Could anyone point out to me how to get those nifty code boxes? Thanks a lot! [EDIT #2] Nevermind, thanks wild_pointer!
// Pacman Editor main .cpp file. 

#include <fstream>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
	char fileName[80];
	char buffer[5];
	int xTileCount = 0;
	int yTileCount = 0;
	int tempX = 0;
	int tempY = 0;
	int firstTime = 0;
	
	cout << "Enter file name: ";
	cin >> fileName;
	cout << "\nNumber of tiles on the X-Axis (0-14): ";
	cin >> xTileCount;
	cout << "\nNumber of tiles on the Y-Axis (0-18): ";
	cin >> yTileCount;
	
	if (xTileCount > 14)
	{
	    cout << "\nError!  X Tile Count can not be larger then 14!\n";
	    xTileCount = 14;
	}
	if (yTileCount > 18)
	{
     	cout << "Error! Y Tile Count can not be larger then 18!\n";
     	yTileCount = 18;
	}
	
	ofstream fout(fileName);
	if (!fout)
	{
	    cout << "Unable to open " << fileName << " for writing.\n";
	    return(1);
    }
	fout << "X" << xTileCount << "\n";
	fout << "Y" << yTileCount << "\n";
	
	cout << "Remember, direction questions, such as, 'can move up?' are only to\n";
    cout << "be answered with 1s and 0s, else, bad things can and probably will\n";
    cout << "happen.....\n\n\n";
    
	// Main input loop
	while (tempX < xTileCount)
    {
        tempY = 0;
        while (tempY < yTileCount)
        {
            if (tempX < 10)
            {
                fout << "x0" << tempX;
            }
            else
            {
                fout << "x" << tempX;
            }
            
            if (tempY < 10)
            {
                fout << "y0" << tempY << "-";
            }
            else
            {
                fout << "y" << tempY << "-";
            }
            cout << "Values for tile " << tempX << ", " << tempY << " --\n";
            cout << "\nCan move up: ";
            cin.getline(buffer,2);
            fout << buffer;   
            cout << "\nCan move right: ";
            cin.getline(buffer,2);
            fout << buffer;
            cout << "\nCan move down: ";
            cin.getline(buffer,2);
            fout << buffer;
            cout << "\nCan move left: ";
            cin.getline(buffer,2);
            fout << buffer << "\n";
            tempY++;
        }
        tempX++;
    }
    fout.close();
    cout << "Map creation finished.\n";
	system("PAUSE");
	return 0;
}

[Edited by - SirLuthor on August 28, 2004 4:03:06 PM]

Share this post


Link to post
Share on other sites
Humor me. I'm a n00b, I have no idea what the problem is, much less what to search for, because I have no error messages or anything.. It's a pain not knowing anything, but I can't help it.

Share this post


Link to post
Share on other sites
Quote:
Original post by SirLuthor
Humor me. I'm a n00b, I have no idea what the problem is, much less what to search for, because I have no error messages or anything.. It's a pain not knowing anything, but I can't help it.


The picture is in response to your asking how to get code blocks.

Share this post


Link to post
Share on other sites
Looks like you have a spare \n in the input buffer, so when getline trys to read from it, it sees the \n in the buffer and returns straight away, meaning you don't get a chance to enter any input. There are many ways to fix it, all with varying levels of complexity. Something like this should fix it.



#include<iostream>

std::istream& clear(std::istream& str)
{
str.ignore(str.rdbuf()->in_avail());
return str;
}

int main(){

...
std::cin >> clear;

cout << "\nCan move up: ";
cin.getline(buffer,2);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by wild_pointer
Quote:
Original post by SirLuthor
Humor me. I'm a n00b, I have no idea what the problem is, much less what to search for, because I have no error messages or anything.. It's a pain not knowing anything, but I can't help it.


The picture is in response to your asking how to get code blocks.


Oh, righto, sorry bout' the misunderstanding :) My thanks!

Quote:

Looks like you have a spare \n in the input buffer, so when getline trys to read from it, it sees the \n in the buffer and returns straight away, meaning you don't get a chance to enter any input. There are many ways to fix it, all with varying levels of complexity. Something like this should fix it.


Thanks, I really apreciate it!

Share this post


Link to post
Share on other sites

This topic is 4854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this