Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Text adventure help

So i'm creating a basic text adventure to help with my skills.

I'm running into a problem with an if-else statement. It keeps wanting to run the


Code
#include <iostream>
#include <string>
using namespace std;

int main() {

    int UserInpInt;
    string UserInpChar;

    cout << "Welcome to my first shitty game!\n" << "Please specify what difficulty you wish to play in. Type 'easy' or 'hard' to begin\n"
        << "I choose: ";
    cin >> UserInpChar;

    if (UserInpChar == "easy" || "hard") {
        cout << endl << "Congrats! You are ready to play!\n";
        cin.get();
    }
    else {
        cout << "That's not a valid input!\n";
    }


    cin.get();
    return 0;
}

Excuse the cin.get()'s. Im using visual studios so it's the only way to keep the console up?...

Anywho, it keeps giving me the Congrats line no matter what I type in. Why is my else statement not working?
Also, is there a way I can keep the console up without having to use cin.get() in every single statement/function? It's really annoying imo
This is the issue.
PHP Code

<?php 
if (UserInpChar == "easy" || "hard") {
    
//...
} 

try:
PHP Code

<?php 
if(UserInpChar == "easy" || UserInpChar == "hard"){
    
//...
} 

Also, I just use system("pause") but whatever works.
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
Set breakpoints to keep the console open at whatever point without moving forward

I always have the final "return 0" in main breakpointed.
Also, you might want to use transform to make sure the input will be caught. For example, if I type "Easy", it will not work because only "easy" is correct. Instead of doing something stupid like:
if (... "easy" || ... "Easy" || "EAsy" ... etc)

You can do something like:
PHP Code

<?php 
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <cctype>

int main() {
    
std::set<std::string> levels = { "easy", "medium", "hard" };
    
std::string uInput;
    
std::cout << "Enter easy, medium, or hard: ";
    
std::cin >> uInput;
    
std::transform(uInput.begin(), uInput.end(), uInput.begin(), std::tolower);
    if (
std::find(levels.begin(), levels.end(), uInput) != levels.end()) {
        
std::cout << "ready!" << std::endl;
    }
    else {
        
std::cout << "not ready" << std::endl;
    }
    
system("pause");
} 
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
Thanks! Really helped a lot mate
(Jan 26, 2016 06:26 PM)Goodies Wrote:  Also, you might want to use transform to make sure the input will be caught. For example, if I type "Easy", it will not work because only "easy" is correct. Instead of doing something stupid like:
if (... "easy" || ... "Easy" || "EAsy" ... etc)

You can do something like:
PHP Code

<?php 
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <cctype>

int main() {
    
std::set<std::string> levels = { "easy", "medium", "hard" };
    
std::string uInput;
    
std::cout << "Enter easy, medium, or hard: ";
    
std::cin >> uInput;
    
std::transform(uInput.begin(), uInput.end(), uInput.begin(), std::tolower);
    if (
std::find(levels.begin(), levels.end(), uInput) != levels.end()) {
        
std::cout << "ready!" << std::endl;
    }
    else {
        
std::cout << "not ready" << std::endl;
    }
    
system("pause");
} 

Why would you use std::find() when std::set::find() already exists? http://www.cplusplus.com/reference/set/set/find/

I didn't like the usage of system("pause") thrown into the example either, not really necessary to use it in snippets for learning material, but you haven't included the proper header to use it either (<cstdlib>)
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Jan 26, 2016 08:49 PM)0xDEAD10CC Wrote:  Why would you use std::find() when std::set::find() already exists? http://www.cplusplus.com/reference/set/set/find/

I didn't like the usage of system("pause") thrown into the example either, not really necessary to use it in snippets for learning material, but you haven't included the proper header to use it either (<cstdlib>)

1) I'm brand new to this. How would you use that? just like:
std::set<std::string> myset = {"a", "b", "c"};
myset.find("b")
?

2) How do you do it? std::cin.getline()?
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
If I ever need to std::cin.get() works completely fine for me. I hardly ever have a reason to do so in a production application though, and you really don't see it very often anyways. That's batch stuff...
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Jan 26, 2016 11:10 PM)0xDEAD10CC Wrote:  If I ever need to std::cin.get() works completely fine for me. I hardly ever have a reason to do so in a production application though, and you really don't see it very often anyways. That's batch stuff...

Right. Do you not do a lot of console applications? or do you test from command line? Or are you just focused on debugging etc so CLI output is not really relevant? I would assume so.
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
(Jan 26, 2016 11:28 PM)Goodies Wrote:  Right. Do you not do a lot of console applications? or do you test from command line? Or are you just focused on debugging etc so CLI output is not really relevant? I would assume so.

There are other ways to debug without writing output to the console buffers. Visual Studio comes pre-packaged with a fully equipped debugger that saves more time if you actually know how to use it properly. There's really no reason to keep a line of code like that in a production program though, console or not. If you open up a command prompt in Windows, type the executable name, cmd.exe remains running and you can see output even after the program has terminated. Chances are unless the program takes no command line arguments, or if you create a shortcut that invokes it already with specific arguments, you're going to invoke the program that obvious way anyways.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by CupOfJavaOct 25, 2018 03:28 PM 12 814 Oct 25, 2018 03:28 PM
Last Post: Andsworth
[C#] by SkyCloudsSep 28, 2017 05:37 PM 6 391 Sep 28, 2017 05:37 PM
Last Post: Skylumz
[Python] by MorphineMar 23, 2017 01:02 AM 5 319 Mar 23, 2017 01:02 AM
Last Post: Morphine