What's wrong with this

What I'm trying to do here is writing data from the input. The very first try is going great but there is something wrong about "nama" input after it loops.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main ()
{
ofstream myfile;
myfile.open ("example.txt");

string nama, npm;
int i=1;

myfile << "DATA -->> NAMA | NPM\n\n";
cout << "Silakan masukan data.\n\n";


while (i!=99) {
printf ("%d. Nama: ", i);
getline(cin,nama);
printf (" NPM : ");
cin >> npm;
cout << " " << nama << " | " << npm << "\n\n";
myfile << i << ". "<< nama << " | " << npm << "\n";
scanf("%d", &i);
}

myfile.close();
return 0;
}


Can somebody help me?
could you please be more specific on the problem. but to give some advice now:

1. you can declare and open an ofstream on the same line. like so:
ofstream myFile("mf.asd');
2. Instead of the while loop and the int i declaration rather use:
for(int i=1; i<100; ++i) {...}
@Script Coder

The problem is when I run it,
I cannot input "nama" for the 2nd time but I still can input "npm"

while (i!=99) {
printf ("%d. Nama: ", i);
getline(cin,nama); <-- problem
printf (" NPM : ");
cin >> npm;
cout << " " << nama << " | " << npm << "\n\n";
myfile << i << ". "<< nama << " | " << npm << "\n";
scanf("%d", &i);
}


and of course I put "while" instead of "for" because I still haven't know how much data I will get, so I can randomly enter a number just to start the program all over again so I can put another data. Thanks before.
Last edited on
Topic archived. No new replies allowed.