Compiler finds no errors but doesn't compile?

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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");

		}
	}
What's the output from the compiler?

Also, just checking, but is that your entire code? Because that's not a well-formed program.
To trouble shoot your code, you want to do something like what I have done below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
		}
		else
		if ((runAgain == 'N') || (runAgain == 'n'))
		{
			runAgain = false;
			cout << "False" << endl;  // see if the result is what you expected
		}

	} while (runAgain ==true);

return 0;
}
Last edited on
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
Last edited on
Am I the only one or is there no while at the end?

Your do-while loop looks like this:
1
2
3
4
5
6
7
do {
    // ....
    while (runAgain)
    {
        system("pause");
    }
}


but shouldn't it be like this:
1
2
3
do {
    // ....
} while (runAgain);

Last edited on
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!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#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
		}
		else
			if ((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.
You don't even get to the do-while loop

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?
Thanks for the help Gamer2015! I am very new to C++.
Update: Thanks to all of your advice, I was able to get the program working! Thanks so much for your continued support on this forum!

I have a small issue with the loop to run the program again but with some fiddling, I think I can work out that problem!

Thanks again!
Topic archived. No new replies allowed.