Hello everyone! I am trying to make a program that can take input from the user (me) about my MTG (magic the gathering) cards and output the text to a text file then run again if I want to enter another card. My compiler says there are no errors but will not compile the program. I have narrowed down my problem to one specific part of the program. Here it is:
do
{
std::string name, stat, pow; // The below code opens the file and adds the name etc of the card then closes the file.
std::ofstream outfile;
outfile.open("MyMTGcards.txt");
std::cout << "Please enter the name of the card: ";
std::cin >> name;
outfile << name;
std::cout << std::endl;
std::cout << "Please enter its abilites or specail features: ";
std::cin >> stat;
outfile << stat;
std::cout << std::endl;
std::cout << "Please enter the cards power and toughness: ";
std::cin >> pow;
outfile << pow;
std::cout << std::endl;
outfile.close();
std::cout << "\n Do you want to add another card? (Y/N)";
std::cout << std::endl;
std::cout << "Type Y or N to continue: ";
char runAgain;
std::cin >> runAgain;
if (!(runAgain == 'Y'))
{
runAgain = true;
}
if (!(runAgain == 'y'))
{
runAgain = true;
}
if (!(runAgain == 'N'))
{
runAgain = false;
}
if (!(runAgain == 'n'))
{
runAgain = false;
}
while (runAgain)
{
system("pause");
}
}
int main (int argc, char *argv[])
{
bool runAgain;
do
{
std::cout << "\n Do you want to add another card? (Y/N)";
std::cout << std::endl;
std::cout << "Type Y or N to continue: ";
char runAgain;
std::cin >> runAgain;
if ((runAgain == 'Y') || (runAgain == 'y'))
{
runAgain = true;
cout << "True" << endl; // see if the result is what you expected
}
elseif ((runAgain == 'N') || (runAgain == 'n'))
{
runAgain = false;
cout << "False" << endl; // see if the result is what you expected
}
} while (runAgain ==true);
return 0;
}
There is no problem with that part you have given us, either give us all of the code or write a sample code to re-produce the problem. Give us the error message as well. http://cpp.sh/8meo
Thanks so much for your assistance! It's still not working. I'm attempting to make this program as a gift for my wife. She loves MTG as well. Thanks to old responders as well as new ones. You folks volunteer your time and it is appreciated!
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <conio.h>
std::string getFileContents(std::ifstream&); //Gets file contents
int main(int argc, char *argv[])
{
std::ifstream Reader("MTGlogo.txt"); //Open file
std::string Art = getFileContents(Reader); //Get file
std::cout << Art << std::endl; //Print it to the screen
Reader.close(); //Close file
}
std::string getFileContents(std::ifstream& File)
{
std::string Lines = ""; //All lines
if (File) //Check if everything is good
{
while (File.good())
{
std::string TempLine; //Temp line
std::getline(File, TempLine); //Get temp line
TempLine += "\n"; //Add newline character
Lines += TempLine; //Add newline
}
return Lines;
}
else //Return error
{
return"ERROR File does not exist.";
}
bool runAgain;
do
{
std::ofstream outfile;
std::string name, stat, pow; // The below code opens the file and adds the name etc of the card then closes the file.
outfile.open("MyMTGcards.txt");
std::cout << "Please enter the name of the card: ";
std::cin >> name;
outfile << name;
std::cout << std::endl;
std::cout << "Please enter its abilites or specail features: ";
std::cin >> stat;
outfile << stat;
std::cout << std::endl;
std::cout << "Please enter the cards power and toughness: ";
std::cin >> pow;
outfile << pow;
std::cout << std::endl;
outfile.close();
std::cout << "\n Do you want to add another card? (Y/N)";
std::cout << std::endl;
std::cout << "Type Y or N to continue: ";
char runAgain;
std::cin >> runAgain;
if ((runAgain == 'Y') || (runAgain == 'y'))
{
runAgain = true;
std::cout << "True" << std::endl; // see if the result is what you expected
}
elseif ((runAgain == 'N') || (runAgain == 'n'))
{
runAgain = false;
std::cout << "False" << std::endl; // see if the result is what you expected
}
} while (runAgain == true);
system("pause");
return 0;
}
I will eventually add a feature so she can see if the card she entered is already in the file. That's way beyond my skill level. The first part of the program prints ASCII art from a TXT file (its a giant "MTG" with credits to the artist) The remainder of the program is suppose to output her text to the TXT document.
in your function you have an if-else statement and in both of these branches you return something so it is impossible to reach the do-while loop.
Also, at the end you return 0 which is of type integer. you want to return a string.
An integer is not implicitly convertible to a string so you can't write return 0;
I think that the part in the do-while loop and the return 0 should be in the main function, is my assumption correct?