Hello again.
So far, my ventures have been productive, also thanks to some excellent support I got here. My thanks for that .
Now, however, I have a new problem >.<.
Since I am planning on writing a rather huge program with several functions, which will be strung together at some point, I decided to split it up into individual, smaller programs first, to test if they all do what they are supposed to do.
The first of these is the program with the code below this. It is designed to do the following (though the comment should give some hints :P): It accepts input for a name and stores it in a string, then it queries for a 'casting cost', again storing the input into a string.
Now, casting cost is comprised of colorless (a simple number such as 1, 2, 3 or whatever) and the five main colors: White (W), Blue (U), Black (B), Red (R) and Green (G).
Thus, a spell with a casting cost of 2 colorless and 2 green would be akin to 2GG.
following a simple example program from the site, I used the program on the side that counts the number of times "cat" occurs in the input sentence. (Should be under the Lesson on Strings)
I simply edited it to incorporate my variables instead of the variables used there.
I compiled the code, got a few errors, quickly removed them (thank god, there were only some stupid errors like a forgotten semicolon or a forgotten " here and there :-D) and compiled again. It compiled! I was happy.
Then I ran it.
It asked me for a Spell Name. I was delighted, it worked.
I entered a spell name, and it went on to ask me for Casting Cost! Woohoo!
But as soon as I hit the enter/return key after entering a casting cost, the thing immediately shuts down . I thought I added all the proper instances of cin.ignore() and cin.get() to avoid this issue!
Can anyone help me identify why my program immediately shuts down after I press the enter/return key after typing in the Casting Cost?
Thank you so much!
(lots of code...if anyone has any suggestions on how to make it more efficient while sticking to relatively easy code, I'd love to hear it, too.)
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
string SpellName = " ";
// To ask for Spell Name
cout<< "Spell Name: \n";
cin>> SpellName;
cin.ignore();
string CastingCost = " ";
// To ask for Mana Cost
cout<< "Casting Cost: \n";
cin>> CastingCost;
cin.ignore();
//Defining variables needed to find and recognise colors.
int White_Counter = 0;
int WhiteMana = 0;
int Blue_Counter = 0;
int BlueMana = 0;
int Black_Counter = 0;
int BlackMana = 0;
int Red_Counter = 0;
int RedMana = 0;
int Green_Counter = 0;
int GreenMana = 0;
int ColorNumber = 0;
string Colors = "Colorless.\n";
string Color1 = " ";
string Color2 = " ";
string Color3 = " ";
string Color4 = " ";
string Color5 = " ";
// Series of 'for' statements with .find to find colored mana and count it.
// ColorMana can be used for seeing if the spell is of a color AND the amount of mana.
// Color_Counter is an indicator for the .find function to function properly.
for(White_Counter = CastingCost.find("W", 0); White_Counter != string::npos; White_Counter = CastingCost.find("W", White_Counter))
{
WhiteMana++;
White_Counter++; // Move past the last discovered instance to avoid finding same string
}
for(Blue_Counter = CastingCost.find("U", 0); Blue_Counter != string::npos; Blue_Counter = CastingCost.find("U", Blue_Counter))
{
BlueMana++;
Blue_Counter++; // Move past the last discovered instance to avoid finding same string
}
for(Black_Counter = CastingCost.find("B", 0); Black_Counter != string::npos; Black_Counter = CastingCost.find("B", Black_Counter))
{
BlackMana++;
Black_Counter++; // Move past the last discovered instance to avoid finding same string
}
for(Red_Counter = CastingCost.find("R", 0); Red_Counter != string::npos; Red_Counter = CastingCost.find("R", Red_Counter))
{
RedMana++;
Red_Counter++; // Move past the last discovered instance to avoid finding same string
}
for(Green_Counter = CastingCost.find("G", 0); Green_Counter != string::npos; Green_Counter = CastingCost.find("G", Green_Counter))
{
GreenMana++;
Green_Counter++; // Move past the last discovered instance to avoid finding same string
}
// Series of 'if' statements to set color values to strings
if (WhiteMana > 0) {
Color1 = "White";
}
if (BlueMana > 0) {
Color2 = "Blue";
}
if (BlackMana > 0) {
Color3 = "Black";
}
if (RedMana > 0) {
Color4 = "Red";
}
if (GreenMana > 0) {
Color5 = "Green";
}
// Set of 'if' statements that ouput the colors if the previous 'if' statements counted them
if (Color1 == "White") {
cout<<Color1;
ColorNumber++;
}
if (Color2 == "Blue") {
if (ColorNumber > 0) {
cout<<" and "<<Color2<<"";
ColorNumber++;
}
else {
cout<<Color2;
ColorNumber++;
}
if (Color3 == "Black") {
if (ColorNumber > 0) {
cout<<" and "<<Color3<<"";
ColorNumber++;
}
else {
cout<<Color3;
ColorNumber++;
}
if (Color4 == "Red") {
if (ColorNumber > 0) {
cout<<" and "<<Color4<<"";
ColorNumber++;
}
else {
cout<<Color4;
ColorNumber++;
}
if (Color5 == "Green") {
if (ColorNumber > 0) {
cout<<" and "<<Color5<<"";
ColorNumber++;
}
else {
cout<<Color5;
ColorNumber++;
}
//Checking for correct reply to test if the program worked.
int CorrectReply = 0;
int ErrorMessage = 0;
int Useless_Counter = 0;
cout<< "Is this correct?\n Press 1 for yes, press 2 for no.\n";
cin>> CorrectReply;
cin.ignore();
for (ErrorMessage = 0; ErrorMessage == 1; Useless_Counter++) {
if (CorrectReply = 1) {
cout<< "Thank you for using this program.\n";
ErrorMessage = 0;
cin.get();
}
else {
if (CorrectReply = 2) {
cout<< "Please return to your code editor and redo the code.\n";
ErrorMessage = 2;
cin.get();
}
else {
cout<< "Error, wrong input. Please enter 1 for yes or 2 for no.\n";
ErrorMessage = 1;
cin>> CorrectReply;
cin.ignore();
}
}
}
}
}}
}}