それで、私は今しばらくこのプログラムを作っています。私はインターネット全体を見てきましたが、私が見つけた解決策はどれもうまくいきませんでした。arr[i].question と arr[i].answer に入力を入れるたびに、質問に答えなくても、質問が間違っていると表示されます。cin.ignore()、cin.clear()、および cin.sync() を使用してみました。間違った場所で使用していた可能性がありますが、よくわかりません。混乱するかもしれないので、コードを見てください。
入力形式はこちら。
cin >> count;
cin.ignore();
for(int i =0; i < count; i++){
cout << "Enter the question.\n" << endl;
//enter the question
getline(cin, arr[i].question);
cin.ignore();
cout << "Enter the answer.\n" << endl;
//enter the answer
getline (cin, arr[i].answer);
cin.ignore();
}
そして、これがあなたにクイズを出すためのアウトプットフォーマットです。
for(int j =0; j < count; j++){
cout << "\n" << arr[j].question << endl;
getline(cin, userguess);
if(arr[j].answer.compare(userguess) !=0){
cout << "Wrong. Keep trying!\n";
incorrect++;
total++;
}
if(arr[j].answer.compare(userguess) ==0){
cout << "Nice job. Keep it up!\n";
correct++;
total++;
}
質問を入力しても、コンソールに質問が出力されないか、回答を入力できません。それは間違っていると言っているだけです。少し助けてください?
編集:ここにコード全体があります:
// final PROJECT.cpp : Defines the entry point for the console application.
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <cstring>
#include <cstdio>
#include <stdio.h>
#include <tchar.h>
#include <string.h>
#include <cstdlib>
using namespace std;
struct flashcards{
string question;
string answer;
}FC1, FC2, FC3, FC4, FC5, FC6, FC7, FC8, FC9, FC10, FC11, FC12, FC13, FC14, FC15, FC16, FC17, FC18, FC19, FC20;
int _tmain(int argc, _TCHAR* argv[])
{
system ("color 4B");
string userguess;
string again;
flashcards arr[10000];
int count;
float correct=0;
float incorrect=0;
float total=0;
//one major problem, wont accept spaces.
cout<< "Flash Card Runner\n";
cout<< "This program was made by Jacob Malcy\n";
cout<< "Beta 3.8\n";
cout<< "IN ORDER FOR YOU TO HAVE MULTIPLE WORD FLASH CARDS,\n";
cout << "SKIP NUMBER ONE FLASHCARD QUESTION AND ANSWER!\n";
cout<< "This bug is currently being worked on.\n";
cout << "If you happen to have problems conntact Jacob.\n";
int choice;
cout<< "Would you like to create or test? Enter 0 for create and 1 for test.\n";
cin >> choice;
if(choice==0){
//Creating new deck of cards
cout<< "How many flashcards do you want?\n";
cin >> count;
cin.clear();
for(int i =0; i < count; i++){
cout << "Enter the question.\n" << endl;
//enter the question
getline(cin, arr[i].question);
cin.ignore();
cout << "Enter the answer.\n" << endl;
//enter the answer
getline (cin, arr[i].answer);
cin.ignore();
}
}
else if(choice==1){
//Reading in new file
cout << "Reading file...\n";
string line;
ifstream myfile ("Save.txt");
if (myfile.is_open())
{
count = 0;
while ( myfile.good () )
{
getline (myfile,line);
arr[count].question = line;
cout << line << endl;
getline (myfile,line);
arr[count].answer = line;
cout << line << endl;
count++;
}
myfile.close();
}
else cout << "Unable to open the file";
}
do
{
for(int j =0; j < count; j++){
cout << "\n" << arr[j].question << endl;
getline(cin, userguess);
if(arr[j].answer.compare(userguess) !=0){
cout << "Wrong. Keep trying!\n";
incorrect++;
total++;
}
if(arr[j].answer.compare(userguess) ==0){
cout << "Nice job. Keep it up!\n";
correct++;
total++;
}
}
cout<< "The number of correct questions answered was: \n" << correct << endl;
cout<<"The number of total questions answered was: " << total <<"\n";
cout<< "The number of incorrect questions answered was: \n" << incorrect << endl;
//cout<< total;
float percent = (correct/total)*100;
cout<< "The total percent you got right is: \n" << percent << "% correct" << endl;
system("pause");
cout << "Would you like to run the quiz again?\n"
<< "Type y or Y to run this again. If not, enter any other letter.\n";
cin >> again;
if((again == "y") || (again == "Y")){
correct=0;
incorrect=0;
total=0;
}
}while((again == "y") || (again == "Y"));
ofstream myfile ("Save.txt");
if (myfile.is_open())
{
for(int i =0; i <count; i++){
myfile << arr[i].question << "\n";
myfile << arr[i].answer << "\n";
}
myfile.close();
}
else cout << "Unable to save file";
cout << "Your finished with the quiz. Goodbye!\n";
system("PAUSE");
return 0;
}
実行すると、このようになります
Flash Card Runner
This program was made by Jacob Malcy
Beta 3.8
IN ORDER FOR YOU TO HAVE MULTIPLE WORD FLASH CARDS,
SKIP NUMBER ONE FLASH CARD QUESTION AND ANSWER!
This is a bug is currently being worked on.
if you happen to have problems, conntact Jacob.
Would you like to create or test? Enter 0 for create and 1 for test.
ゼロを入力した場合:
How many flashcards do you want?
2 2 を入力すると言う
Enter the question.
Hi ho
Enter the answer.
Merry oh
enter the question.
fi fa
enter the answer.
fo fum
その後、次のようにスキップします。
Wrong. Keep trying!
erry oh
間違っている前に、最初の質問が表示され、答える機会が与えられます。私ができる前に、それはただ間違っていると言います。次に、最初の文字が欠落している回答を表示します。ほらね。