好的,所以我刚刚开始用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();
}
}
你可以看到这让我感到困惑,也许你有任何提示我会非常感激
求助:&gt;
好吧所以,因为我刚加入这个论坛,我不能回答我自己的问题,所以我决定更新我的原件并再次发送代码,但这次有另一个问题。这次我无法得到得分保存到.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?
答案 0 :(得分:4)
可能存在大量错误,但最大的错误是您编写了太多错误代码。对不起苛刻,但这是真的。要学习的主要经验是编写小块代码,并在编写之前让每个块工作。
举个例子,这是错误的
int fraga;
...
cin >> fraga;
switch(fraga){
case '4':
为什么这是错的?因为fraga是int
,但您说case '4':
而'4'
是char
。它应该是
cin >> fraga;
switch(fraga){
case 4:
因为4
是int
。
稍后你写
bool fail;
...
cin >> fail;
if(fail != '1'){
fail = false;
}
这是错误的,因为失败是bool
,因此fail != '1'
没有意义,因为'1'
是char
而不是bool
。看到这个问题?如果您花时间修复第一个错误,那么您将永远不会发生第二个错误。你的计划中有很多浪费,因为你试图过快。
如果你采取婴儿步骤,你会发现编程更容易,至少在你学习的时候。