好的,所以我最近才开始用 C++ 编程,没有其他语言的经验。
我一开始对学校项目有一个简单的想法,但不知何故,它让我在我试图创建的代码中迷失了方向。
想法如下,我想创建一个程序,首先给我一个“开始”“加载”“高分”菜单,然后当你选择其中一个时,你会得到它所说的:P 所以我从一些基本结构开始包括我在每个问题后分配给 .txt 文档的点,然后是一些 if 和 switch-case 和一个 do-while,所以如果你失败了,你可以选择重新启动。
到目前为止我已经完成的代码(距离完整还很远,但这是我所得到的)(svar 表示答案 / fraga 表示问题):
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
struct highscore{
int svar;
}
int main(){
bool fail = true;
int svar = 0, fraga;
char val;
do{
cout << " Menu" \n;
cout << " 1. Start" \n;
cout << " 2. Load" \n;
cout << " 3. Credits" \n;
cin >> val;
switch(val){
case '1':
ofstream utFil;
utFil.open("highscore.txt", ios::app);
if (!utFil){
cout << "file could not open";
exit (1);
}
cout << "WELCOME TO THE CISCO QUIZ\n" "GOOD LUCK\n \n \n" ;
cout << "how many layers are there in the OSI model?" \n "4"\n "5"\n "or 7?" \n;
cin >> fraga;
switch(fraga){
case '4':
cout << "Game Over!"\n;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)\n";
cin >> fail;
if(fail != '1'){
fail = false;
}
break;
case '5':
cout << "Game Over!"\n;
cout << "Do you wish to restart? (answer with 1 for yes, 2 for no)\n";
cin >> fail;
if(fail != '1'){
fail = false;
}
break;
case '7':
svar = svar + 5;
break;
}
if(svar == 5){
cout << "what is name full name of OSPF?" \n;
cout << "1. Open Shortest Path First" \n;
cout << "2. Online Shooting Pro Fishing" \n;
cout << "3. On Short Path First"\n ;
cin >> fraga;
switch(fraga){
case '1':
svar = svar + 5;
}
case '2':
cout << "Game Over!"\n;
cout << "Do you wish to restart? (answer with yes, YES or Yes)"\n;
cin >> fail;
if(fail != '1'){
fail = false;
}
break;
}
case '3':
cout << "Game Over!"\n;
cout << "Do you wish to restart? (answer with yes, YES or Yes)"\n;
cin >> fail;
if(fail != '1'){
fail = false;
}
}
}
if(svar == 10){
cout << "What does TTL mean?" \n;
cout << "1. Twin Twitched Life"\n;
cout << "2. Two Time Lives"\n;
cout << "3. Time To Live"\n;
cin >> fraga;
switch(fraga){
case '1':
cout << "Game Over!"\n;
cout << "Do you wish to restart? (answer with yes, YES or Yes)"\n;
cin >> fail;
if(fail != '1'){
fail = false;
}
break;
}
case '2':
cout << "Game Over!"\n;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)"\n;
cin >> fail;
if(fail != '1'){
fail = false;
}
break;
}
case '3':
svar = svar + 10;
break;
}
}
if(svar == 20){
cout << "What is the first command you enter on a Cisco router?"\n;
cout << " 1. enable"\n;
cout << " 2. disable"\n;
cout << " 3. enter"\n;
cin >> fraga;
switch(fraga){
case '1':
svar = svar + 5;
break;
case '2':
cout << "Game Over!"\n;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)"\n;
cin >> fail;
if(fail != '1'){
fail = false;
}
break;
case '3':
cout << "Game Over!"\n;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)"\n;
cin >> fail;
if(fail != '1'){
fail = false;
}
break;
}
}
if(svar == 25){
cout << "which protocol is the most prefered?"
cout << " 1. RIP"\n;
cout << " 2. EGRP"\n;
cout << " 3. EIGRP"\n;
cin >> fraga;
switch(fraga){
case '1':
cout << "Game Over!"\n;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)"\n;
cin >> fail;
if(fail != '1'){
fail = false;
}
break;
}
case '2':
cout << "Game Over!"\n;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)"\n;
cin >> fail;
if(fail != '1'){
fail = false;
}
break;
}
case '3':
svar = svar + 15;
break;
}
}
utFil.close();
}
}
正如你所看到的,这让我感到困惑,也许你也得到了任何提示,我将非常感激
谢谢帮助:>
好的,因为我刚刚加入这个论坛,所以我还不能回答我自己的问题,所以我决定更新我的原件并再次发送代码,但这次有另一个问题。这次我无法保存分数到 .txt 文件中,它只创建一个 .txt 但保持为空 这是更新代码的样子:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
struct highscore{
int svar;
};
int main(){
ofstream highscore;
bool fail = true;
int svar = 0, fraga, lost;
char val;
do{
cout << " Menu" << endl;
cout << " 1. Start" << endl;
cout << " 2. Load" << endl;
cout << " 3. Credits" << endl;
cin >> val;
switch(val){
case '1':
highscore.open("Highscore.txt", ios::app);
if (!highscore){
cout << "file could not open";
exit (1);
}
cout << "WELCOME TO THE CISCO QUIZ GOOD LUCK" << endl;
cout << "how many layers are there in the OSI model?" << endl;
cout << "1. 4" << endl;
cout << "2. 5" << endl;
cout << "3. 7" << endl;
cin >> fraga;
switch(fraga){
case 1:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 2:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes, 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 3:
svar = svar + 5;
break;
default:
cout << "error" << endl;
}
if(svar == 5){
cout << "what is the full name of OSPF?" << endl;
cout << "1. Open Shortest Path First" << endl;
cout << "2. Online Shooting Pro Fishing" << endl;
cout << "3. On Short Path First" << endl;
cin >> fraga;
switch(fraga){
case 1:
svar = svar + 5;
break;
case 2:
cout << "Game Over!"<< endl;
cout << "Do you wish to restart? (answer with 1 for yes, 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 3:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes, 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
}
}
if(svar == 10){
cout << "What does TTL mean?" << endl;
cout << "1. Twin Twitched Life" << endl;
cout << "2. Two Time Lives" << endl;
cout << "3. Time To Live" << endl;
cin >> fraga;
switch(fraga){
case 1:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with yes, YES or Yes)"<< endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 2:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 3:
svar = svar + 10;
break;
}
}
if(svar == 20){
cout << "What is the first command you enter on a Cisco router?" << endl;
cout << " 1. enable"<< endl;
cout << " 2. disable" << endl;
cout << " 3. enter" << endl;
cin >> fraga;
switch(fraga){
case 1:
svar = svar + 5;
break;
case 2:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 3:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
}
}
if(svar == 25){
cout << "which protocol is the most prefered?" << endl;
cout << " 1. RIP" << endl;
cout << " 2. EGRP" << endl;
cout << " 3. EIGRP" << endl;
cin >> fraga;
switch(fraga){
case 1:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 2:
cout << "Game Over!"<< endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 3:
svar = svar + 15;
break;
}
}
highscore.close();
case '2':
cout << "Still working on it" << endl;
break;
case '3':
cout << "made by trickjay the most awesome guy on the planet!!!" << endl;
break;
default:
cout << "Error try again" << endl;
}
}while(fail);
}
如何让我的代码将分数保存到 .txt 文件中,而不仅仅是创建 .txt?