わかりましたので、他の言語の経験がなく、最近 C++ でプログラミングを始めたばかりです。
私は学校のプロジェクトのシンプルなアイデアから始めましたが、どういうわけか、作成しようとしていたコードで私を失いました。
アイデアは次のとおりです。最初に「開始」「ロード」「ハイスコア」のメニューを表示するプログラムを作成したいのですが、そのうちの1つを選択すると、その内容が表示されます: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();
}
}
ご覧のとおり、これは私を混乱させます。おそらく、あなたにもヒントがありました。私は非常に感謝しています
ヘルプのためのthx :>
このフォーラムに参加したばかりなので、まだ自分の質問に答えることができないので、元のコードを更新してコードを再度送信することにしましたが、今回は別の問題があります.今回は保存するスコアを取得できません.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 ファイルに保存するコードを取得するにはどうすればよいですか?