Jump to content

Coding Help


Recommended Posts

So, I'm working on this code in C++. The input prompt is supposed to only accept 1, 2, and 3 as valid responses, while any number less than or equal to 0 or greater than or equal to 4 causes the program to send an invalid answer message before sending the player back to the selection prompt. However, I've recently discovered that if an alphabetical or special character is entered, it will break the program. I've been trying to figure out how to fix that, but I haven't had any success.

Here's the code I've been writing:

Spoiler

#include <iostream>

using namespace std;

int main()
{
    int choice;
    char continuation;
   
    //Section 1, paragraph 1
    cout << "(Insert scenario description here)\n\n";
    
    cout << "Press any key to continue: ";
    cin >> continuation;
    
    //Section 1, paragraph 2
    cout << "\n(Insert scenario description here)\n\n";
    
    cout << "Press any key to continue: ";
    cin >> continuation;
    
    //Section 1, paragraph 3
    cout << "\n(Insert scenario description here)\n\n";
    
    //Choice selection prompt
    first:cout << "1. (Insert Option 1 here)\n";
    cout << "2. (Insert Option 2 here)\n";
    cout << "3. (Insert Option 3 here)\n\n";
    
    cout << "What will you do? ";
    cin >> choice;
    
    //Correct answer
    if (choice == 1)
    {
        cout << "\nThat is the correct answer!\n";
    }
    //Incorrect answers
    else if (choice == 2 || player.choice == 3)
    {
        cout << "\nThat is the wrong answer.\n";
        goto first;
    }
    //Invalid answer
    else (choice <= 0 || choice >= 4);
    {
        cout << "\nInvalid Answer!\n";
        goto first;
    }
   
    cout << "Test run success confirmed.";

    return 0;
   
}

Any help is appreciated, because I am very stuck on this and desperate enough to go to a fucking ERP site for help lol.

Link to comment
Share on other sites

  • Community Administrator

@Aura might probably be the best person that I know of to ask on this (Could be wrong) as I am pretty good in HTML and CSS with a little knowledge in C#, and even less in PHP and JS.

C++ isn't something I've ever coded in. However, after looking it up online, this is an example that I found that worked:

#include <iostream>
using namespace std;

int main ()
{
	bool intEntered = false;
	char input[255] ;
	int number;

	do{
	cout<<"Input number: ";
	cin >> input;
	string s = input;
	number = atoi(s.c_str());
	if ( number == 0 )
	cout << "Input contained characters that are not allowed. Try again. ";
	else
		intEntered = true;

	}while(!intEntered);
}

I don't know if you can modify that for your use; or if someone else wants to come in and say "There's a better way" or there's a "Correct way" of doing it. I'd probably use Regular Expression (RegEx) myself but having no experience in C++, I am not sure if this is possible or not.

Link to comment
Share on other sites

  • Senior Staff

I'm assuming you're new to programming in general? Small word of advice, break as much as you possibly can into smaller methods. It'll save you a lot of time and headache in the longrun when you get to more complicated programs that span several components. Programming is all putting tiny parts into larger parts into even larger parts. You want to reuse as much as you can!

For example, instead of a main with

	cout << "(Insert scenario description here)\n\n";
    
	cout << "Press any key to continue: ";
	cin >> continuation;

written several times, it would help to turn that into something like

this->printDescription("(Insert scenario description here)");

and then define the printDescription method like:

void printDescription(std::string message)

{

  char continuation;

  std::cout << message << "\n\n";

  std::cout << "Press any key to continue: ";

  std::cin >> continuation;

}

It's been a while since I've written c++ and I don't have a compiler to talk to at the moment, so the syntax might be a bit rough. If you're self-teaching or starting out a c++ class and you haven't learned about local methods, you shouldn't be far off of learning about them. It might seem like it's not worth the extra hassle, but it absolutely is in the long run. (Also, fuck coding without a tab key omg.)

Note, if the value of continuation is important to keep, you can simply change void to char and add return continuation to the end. Then call it with continuation = this->printDescription(...);

This might seem nitpicky right now, but I assure you this is very important to get in the habit of doing and you'll thank me later when you need to debug whole components. Get in the habit of doing this as much as possible, and you'll know exactly where something breaks when everything in a component suddenly stops working!

================

Tiny rant out of the way, to answer your actual question, perhaps you could try isdigit() in the <cctype> header. It might look something like this

#include <cctype>

...

if (!isdigit(input))     //If input is NOT a digit

{

    //handle error, ask for a new input maybe

}

//Continue as normal otherwise

Again, it would be great to factor this out into its own method. Something like bool validateInput(char input);

More recently, I've gotten used to using char arrays rather than strings in c-based languages, I find it a lot easier to parse through strings that way. I hope this advice helps! c++ can be a bit frustrating at times, but it's very versatile! If you ever find yourself at a wall, it never hurts to resort to stackoverflow, either! If nobody else has asked your question, (that's rare, especially for entry-level programming tasks) you can always ask it yourself! Worst case scenario, somebody on the internet laughs at you.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • There are no registered users currently online
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Read our Privacy Policy for more information.

Please Sign In or Sign Up