i trying print out 3 lines in file, ends happening prints 1 of lines in 3 spots, prints second line in 3 spots, , then, well, guessed it, same 3rd. want print out 3 lines in 1 push of button. ideas?
ifstream read_file; string fname, name; cout << "type complete address of file open.\n"; cin >> fname; system("cls"); read_file.open(fname.c_str()); while(getline(read_file, fname)) { if (fname == "")continue; cout << "you had "<< fname << " health left\n"; cout << "you delt " << fname << " damage\n"; cout << "there " << fname << " enemies left\n"; system("pause"); } read_file.close(); system("pause");
looking @ code
while(getline(read_file, fname)) { if (fname == "") continue; cout << "you had "<< fname << " health left\n"; cout << "you delt " << fname << " damage\n"; cout << "there " << fname << " enemies left\n"; system("pause"); }
it's obvious you're reading 1 line, , printing 3 times - you're not doing fname
between outputs.
i recommend stop reusing same variable multiple purposes, it's source of many bugs.
(the following assumes there no blank lines.)
string health; string damage; string enemies; while(getline(read_file, health) && getline(read_file, damage) && getline(read_file, enemies)) { cout << "you had "<< health << " health left\n"; cout << "you delt " << damage << " damage\n"; cout << "there " << enemies << " enemies left\n"; }
if there blank lines in file, consider writing own getnonemptyline
function skips blank lines.
Comments
Post a Comment