C++ STL - Trouble with getline

What is the problem with this 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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void GiveNamesMaps(){
    auto int amount;
    auto ofstream file;
    auto string level_header = "map ", level_name="-none-";

    cout << "How many level names you would like to give: ";
    cin >> amount;

    file.open("defs_levels.def");
    for(int i=0; i <= amount; i++){
        file << level_header;

        cout << "Give level name for level " << i << ": ";
        getline(cin,level_name);

        i < 10 ? file << 0 << i : file << i;
        file << " " << level_name << endl;
    }
    file.close();
}

int main(){
    cout << "Script Manager:\n\n";
    GiveNamesMaps();
    return 0;
}


The goal of this program is to create output file "defs_levels.def" and contain data like this:

1
2
3
4
map 00 Hub World
map 01 Level 1
map 02 Another level
map 03 Third level

etc.

Of course I add the file name into output file later on too, but getline doesn´t seem to work correctly. Now when I run this it doesn´t allow me to give a name for level 00. Help, please.
Add cin.ignore(); after line 12. The problem: The ENTER you hit after typing the amount is lingering on and ignore() should be able to get rid of it.
Thanks a lot webJose, it works now. Fortunately it was that simple.
Last edited on
Topic archived. No new replies allowed.