-
Need a quick check
Hey guys,
Im pretty new to c ++ and was just wonderiing if anyone could quuckly correct my conversion function I have and tell me how it should be as it is not working.
Thanks kindly
Code:
//<-Libs----||
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <algorithm>
#include <string>
#include <cctype>
//--Files-->>
#include "Classes.h"
#include <stdio.h>
#include <ctype.h>
//---------<<
using namespace std;
string playerInName; // Declares variables.
string playerInAge;
string playerInOrigin;
string playerInPet;
string helpAnswer;
int petChoice;
int main()
{
cout << "Welcome to the C++ Adventure Game Version 1.\nBy Darryl Mitchell \n\n"; // Introduces the game
cout << "Please enter your name: "; // Takes details from the
cin >> playerInName; // player and stores them
cout<< "Now your age: "; // for later use.
cin >> playerInAge;
cout << "Lastly; please enter your place of origin: ";
cin >> playerInOrigin;
cout << "\nNo great adventurer is complete without their trusty companion!\nWhich do you have? A Dog, Cat or an Eagle? ";
cin >> playerInPet;
//------------------ ISSUE -------------------------------------//
string data = playerInPet;
transform(data.begin(), data.end(),
data.begin(), ::tolower);
//--------------------------------------------------------------//
while((playerInPet != "cat") && (playerInPet !="dog")&& (playerInPet != "eagle"))
{
cout << "Ahahah dont be foolin' me! There are no such companions! \nPlease re-validate your companion!\n";
cin >> playerInPet;
if(playerInPet == "cat")
{petChoice = 1;}
if(playerInPet == "dog")
{petChoice = 2;}
if(playerInPet == "eagle")
{petChoice = 3;}
}
-
How is it not working?
Code:
if(playerInPet == "cat")
{petChoice = 1;}
Absolutely horrid formatting.
Use this
Code:
if(playerInPet == "cat")
{
petChoice = 1;
}
Or perhaps
Code:
if(playerInPet == "cat") petChoice = 1;
// or
if(playerInPet == "cat")
petChoice = 1;
Never put braces on the same line as the code itself. If your function gets too long, split it up.
Now, further, you probably want to do the translation EVERY time, not just when the answer the user gave is incorrect the first time round.
And why are all your variables globals?
--
Mats
-
Well, as I said im pretty new to this and alot of it I have been teaching myself so thats why my formatting is terrible !
:)
Um when I compile it and enter CAT or CaT it does not convert it to lower case.
Yeah I would like that, but I am not sure how to make it work, and I was wondering if I can I dunno put the translation function somewhere else have have some way of calling it to where I need it without riting it all out again, is that possible?
as for yhe variables im not sure.. I did a bit of VB coding prior to starting C ++ so a combination of uncertainty and habit
Thanks for the input :)
-
You convert data to lowercase, not playerInPet.
-
Changed the argument but still doesnt work
-
Your logic is still messed up. You prompt for new input, but that is not converted to lower-case. Why don't you rather loop getting input until it is satisfactory, and only then continue with the rest (setting the petChoice value).
-
Hmmm.. isn't that pretty much what ive done?
I just need the input of the pet turned to lowercase so then i can check it against the while statement.
-
You only convert input to lowercase first time it is entered. Within the loop when you ask it to be reentered, you don't convert that. Also your code wouldn't set the petChoice variable if the user entered a correct thing the first time.
You could restructure the logic a bit. A do...while loop is good for getting validated input. You can also recognize whether something valid was entered by seeing whether the petChoice variable has been set to anything other than 0 within the loop. No need to recheck the long and complicated condition.
Code:
cout << "\nNo great adventurer is complete without their trusty companion!\nWhich do you have? A Dog, Cat or an Eagle? ";
petChoice = 0;
do {
cin >> playerInPet;
transform(playerInPet.begin(), playerInPet.end(), playerInPet.begin(), ::tolower);
if (playerInPet == "cat") {
petChoice = 1;
}
else if (playerInPet == "dog") {
...
}
else {
cout << "Ahahah dont be foolin' me! There are no such companions! \nPlease re-validate your companion!\n";
}
} while (petChoice == 0);