Could someone please correct this code for me?

Hello all!
Okay, so I was going to write a map-builder for my text-based game. The file structure goes as follows:

1) Number of rows for map.

2) Number of columns for map.

Newline

3) Length of the string that shall be printed at the start of the map.

4) The string that shall be printed at the start of the map.

Problem: The program compiles with no problems whatsoever, but ignores the getline function for the string to be printed at the start of the level.#

Here is the 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main(){
int rows, cols, **Map;
stringstream ss;
string StartText, level;

cout << "Welcome to the mapbuilder! What is the name of the map that you would like to make?" << endl;
cin >> level;
ss << level << ".aem";
ofstream MakeMap(ss.str().c_str(), ios::binary);
cout << "How many rows?" << endl;
cin >> rows;
cout << "How many columns?" << endl;
cin >> cols;

Map = new int*[rows];
for(int i = 0; i < rows; i++){
		Map[i] = new int[cols];}

for(int j = 0; j < rows; j++){
	for(int i = 0; i < cols; i++){
	    cout << "Enter type for space (" << j+1 << "," << i+1 << ")" << endl;
		cin >> Map[j][i];
		}
    }
cout << "Now enter the text that you wish to appear at the start of the map!" << endl;
getline(cin,StartText);
MakeMap << rows << " " << cols << " ";
for(int i=0;i<rows;i++){
    for(int j=0;j<cols;j++){
        MakeMap << Map[i][j];
        if(rows-1 != 0 || cols-1 != j){MakeMap << " ";}
    }
}
MakeMap << endl;
MakeMap << StartText.size() << " " << StartText;
return 0;
}


I am probably missing something obvious, but could someone help me, please?
Thanks for your time!
ss << level << ".aem";
What is the point of this? Why not just do level += ".aem";?


As for your problem, you need to cin.ignore(unsigned(-1), '\n') before the getline - there's a newline left over from previous inputs.
Thanks for the answers!
The reason for the (irrelevant) stringstream is that I was originally going to use an int, but then decided string and just forgot to change it. Thank you for pointing this out.

During the cin.ignore, why "unsigned(-1)" and not just "1" (as this works too)?

Also, how to I use a C++ style string for an ofstream variable name?
Last edited on
During the cin.ignore, why "unsigned(-1)" and not just "1" (as this works too)?
It would only work if the user didn't put any spaces after their last input. You want to ignore EVERYTHING until you find a new line and ignore it too, then move on in the code.
Also, how to I use a C++ style string for an ofstream variable name?
This problem is fixed in C++11 ;)
Thanks! One more post... what would you recommend as a C++11 compatible IDE (not VS or some other Windows dependent IDE (I have Windows, but primarily use Linux))? Also, how can an unsigned integer be "-1" when only signed integers can be negative in value?
Last edited on
I don't know of any IDEs off the top of my head. As for the unsigned integer thing, why do you think I am casting it? ;) it gets the highest value an unsigned integer can hold.
Never knew that (but, thinking about it, it does make sense binary counters increase to their maximum value when told to go below 0), I suppose that you learn something new every day! As for IDEs, I think that I'll just wait until they release the next Code::Blocks. Thanks for your time, L B!
Topic archived. No new replies allowed.