显示整个文件
Display the whole file
我有一个关于要求用户输入 his/her 姓名和他们想要的分数并显示它的评估。我已经取得了进展,但是当我尝试显示保存文件中的所有文本时(我调用了 scores.txt),它并没有全部打印出来,而是只打印了文本文件的第一行。
这里是代码:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int score;
int highscore;
string name;
void menu() {
cout << "1.Enter a score" << endl;
cout << "2.Display scores" << endl;
cout << "3.Exit" << endl;
}
void enterScore() {
cout << "Enter your desired score: ";
cin >> score;
cout << "Please enter your name and seperate by a (_): ";
cin.ignore();
getline(cin, name);
}
void displayScore() {
cout << "Display scores: " << endl;
}
void exit() {
}
int main()
{
ofstream outFile("scores.txt", ios::app);
ifstream inFile("scores.txt", ios::in);
int choice;
char yesOrNo;
do
{
menu();
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1: enterScore();
outFile << name << ": " << score << endl;
break;
case 2: displayScore();
inFile >> name;
inFile >> score;
cout << name << " " << score << endl;
break;
case 3: exit();
break;
default:
cout << "Error!!!" << endl;
break;
}
cout << "Would you like to try again? (y/n): ";
cin >> yesOrNo;
system("cls");
} while (yesOrNo == 'y');
return 0;
}
如何显示文件中的所有文本,有没有办法像高分板一样对分数进行排序。请帮忙,我非常感谢。
这样试试:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int score;
int highscore;
string name;
void menu() {
cout << "1.Enter a score" << endl;
cout << "2.Display scores" << endl;
cout << "3.Exit" << endl;
}
void enterScore() {
cout << "Enter your desired score: ";
cin >> score;
cout << "Please enter your name and seperate by a (_): ";
cin.ignore();
getline(cin, name);
}
void displayScore() {
cout << "Display scores: " << endl;
}
void exit() {
}
int main()
{
ofstream outFile("scores.txt", ios::app);
ifstream inFile("scores.txt", ios::in);
int choice;
char yesOrNo;
do
{
menu();
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1: enterScore();
outFile << name << ": " << score << endl;
break;
case 2: displayScore();
while(true){
inFile >> name;
inFile >> score;
cout << name << " " << score << endl;
if(inFile.eof())
break;
}
break;
case 3: exit();
break;
default:
cout << "Error!!!" << endl;
break;
}
cout << "Would you like to try again? (y/n): ";
cin >> yesOrNo;
system("cls");
} while (yesOrNo == 'y');
return 0;
}
我有一个关于要求用户输入 his/her 姓名和他们想要的分数并显示它的评估。我已经取得了进展,但是当我尝试显示保存文件中的所有文本时(我调用了 scores.txt),它并没有全部打印出来,而是只打印了文本文件的第一行。
这里是代码:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int score;
int highscore;
string name;
void menu() {
cout << "1.Enter a score" << endl;
cout << "2.Display scores" << endl;
cout << "3.Exit" << endl;
}
void enterScore() {
cout << "Enter your desired score: ";
cin >> score;
cout << "Please enter your name and seperate by a (_): ";
cin.ignore();
getline(cin, name);
}
void displayScore() {
cout << "Display scores: " << endl;
}
void exit() {
}
int main()
{
ofstream outFile("scores.txt", ios::app);
ifstream inFile("scores.txt", ios::in);
int choice;
char yesOrNo;
do
{
menu();
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1: enterScore();
outFile << name << ": " << score << endl;
break;
case 2: displayScore();
inFile >> name;
inFile >> score;
cout << name << " " << score << endl;
break;
case 3: exit();
break;
default:
cout << "Error!!!" << endl;
break;
}
cout << "Would you like to try again? (y/n): ";
cin >> yesOrNo;
system("cls");
} while (yesOrNo == 'y');
return 0;
}
如何显示文件中的所有文本,有没有办法像高分板一样对分数进行排序。请帮忙,我非常感谢。
这样试试:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int score;
int highscore;
string name;
void menu() {
cout << "1.Enter a score" << endl;
cout << "2.Display scores" << endl;
cout << "3.Exit" << endl;
}
void enterScore() {
cout << "Enter your desired score: ";
cin >> score;
cout << "Please enter your name and seperate by a (_): ";
cin.ignore();
getline(cin, name);
}
void displayScore() {
cout << "Display scores: " << endl;
}
void exit() {
}
int main()
{
ofstream outFile("scores.txt", ios::app);
ifstream inFile("scores.txt", ios::in);
int choice;
char yesOrNo;
do
{
menu();
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1: enterScore();
outFile << name << ": " << score << endl;
break;
case 2: displayScore();
while(true){
inFile >> name;
inFile >> score;
cout << name << " " << score << endl;
if(inFile.eof())
break;
}
break;
case 3: exit();
break;
default:
cout << "Error!!!" << endl;
break;
}
cout << "Would you like to try again? (y/n): ";
cin >> yesOrNo;
system("cls");
} while (yesOrNo == 'y');
return 0;
}