#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <algorithm>

using namespace std;

//Types and Arrays
string word[16] = {"GOLDFISH", "COMPUTER", "ANONYMOUS", "JACKET", "SHARP", "SERVICE", "EFFORT", "CHARACTER", "CHANGE", "WITHOUT", "PRODUCT", "UNFOLDS", "MUSIC", "MOMENT", "LIFETIME", "PROVIDE"};
char guess[50];
static const char Y= 'Y';
static const char N= 'N';
char ans;

// Functions
int instructions();
int game();



int main()
{
	cout << "\tWelcome to Word Jumble By Khoanyneosr!\n";
	cout << "\nWould you like to see the instructions?"
		 << "\n\t1) Yes = Y"
		 << "\n\t2) No = N\n- ";
		 bool rep=1;

   while (rep)  {
      cin >> ans;      
      switch (toupper(ans))  {
         case Y:
            instructions();
            rep=0;
            break;
         case N:
            game();
            rep=0;
            break;
         default:
			 cout << "\n\nInvalid input.\n\n- ";
			 cin.clear();
            rep=1;
            break;
       }
    }
	return 0;
}
int instructions()
{
	system("cls");
	cout << endl << endl << "\tINSTRUCTIONS" << endl;
	cout << endl << "1 - You will be presented with a word. Your job is to guess it." << endl;
	cout << endl << "2 - At the beginning of the game your given 10 points." << endl;
	cout << endl << "3 - You will be given 1 hint. Everytime you have to revisit\nthe hint 1 point will be deducted." << endl;
	cout << endl << "4 - For everytime you guess wrong 1 point will be deducted." << endl;
	cout << endl << "5 - When your point value reaches 0, the game is over." << endl;
	cout << endl << "You can exit at anytime by pressing Q." << endl;
	cout << endl << endl << "\tGood Luck!" << endl << endl << endl;

		system("pause");
		return game();

}
int game()
{
		cout << endl << "what is your guess?";
			cin.getline(guess, 50);

		cout << endl << guess[50] << endl;

		return 0;

}

I don't know whats wrong with my code, but i do know that after i answer the first question in the program for some reason the input carry's down to my other input sections... if that makes sense. If you wouldn't mind, please look it over, compile it and PLEASE PLEASE PLEASE tell me whats wrong.

Recommended Answers

All 8 Replies

You are experiencing a very common problem for new coder. When you enter Y for answer then you also press the Enter key, which is '\n'. That key stays in the keyboard buffer because your program only extracted the first character that was typed. You have to flush all the remaining charactgers from the keyboard to that the the program will want for you to type the answer to the next question instead of just getting whatever characters are already in the keyboard buffer.

One way to do that in c++ is described in this article written by Narue.

commented: Thanks very much! I'm pretty new to coding, I've only coded C++ for about six months and really appreciate your help. +0

This is a classic problem, you have a dangling new-line character on the input stream. You need to call ignore() right after you input the character to clear that dangling new-line. So, at line 31:

cin >> ans;
cin.ignore();
commented: Thanks so much! I'm new to coding and I really appreciate your help. +0
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <algorithm>

using namespace std;

//Types and Arrays
string word[16] = {"GOLDFISH", "COMPUTER", "ANONYMOUS", "JACKET", "SHARP", "SERVICE", "EFFORT", "CHARACTER", "CHANGE", "WITHOUT", "PRODUCT", "UNFOLDS", "MUSIC", "MOMENT", "LIFETIME", "PROVIDE"};
char guess[50];
static const char Y= 'Y';
static const char N= 'N';
char ans;

// Functions
int instructions();
int game();



int main()
{
	cout << "\tWelcome to Word Jumble By Khoanyneosr!\n";
	cout << "\nWould you like to see the instructions?"
		 << "\n\t1) Yes = Y"
		 << "\n\t2) No = N\n- ";
		 bool rep=1;

   while (rep)  {
      cin >> ans;  
		
      switch (toupper(ans))  {
         case Y:
			cin.ignore();
            instructions();
            rep=0;
            break;
         case N:
			cin.ignore();
            game();
            rep=0;
            break;
         default:
			 cout << "\n\nInvalid input.\n\n- ";
			 cin.clear();
            rep=1;
            break;
       }
    }
	return 0;
}
int instructions()
{
	system("cls");
	cout << endl << endl << "\tINSTRUCTIONS" << endl;
	cout << endl << "1 - You will be presented with a word. Your job is to guess it." << endl;
	cout << endl << "2 - At the beginning of the game your given 10 points." << endl;
	cout << endl << "3 - You will be given 1 hint. Everytime you have to revisit\nthe hint 1 point will be deducted." << endl;
	cout << endl << "4 - For everytime you guess wrong 1 point will be deducted." << endl;
	cout << endl << "5 - When your point value reaches 0, the game is over." << endl;
	cout << endl << "You can exit at anytime by pressing Q." << endl;
	cout << endl << endl << "\tGood Luck!" << endl << endl << endl;

		system("pause");
		return game();

}
int game()
{
		cout << endl << "what is your guess?";
			cin.getline(guess, 50);
			
		cout << endl << guess[50] << endl;

		cin.ignore();
		return 0;
}

That's my new code, I'm not sure if i'm using it right because it did nothing.

>> it did nothing

It did "nothing"? It should have at least displayed "Welcome to Word Jumble By Khoanyneosr!" Are you sure this compiled? If so, it did something. What did it do?

Sorry, I meant it didn't exactly change, after i enter my input it just skips two lines and either displays "y" or "n" then ends.

In order to debug this properly you need to get rid of all the "system" commands. If you're clearing the screen or pausing for input, you can't see what's been cleared or what's been typed in response to what, so you can't debug.

Nowhere do you check for a 'Q', yet you tell you the user to enter 'Q' to quit. 'N' should NOT quit, but in you program it does.

Line 74 is a segmentation fault. You can change it to this:

cout << endl << guess << endl;

It worked! I still have two more problems, number one. For the first switch statement if i enter a string it repeats "Invalid input" for as many characters as the sting was. How do get it to repeat only once?

Also, the hit 'Q' to quit is something I'm going to add in the near future after i get the rest of the kinks worked out. =)

>> Also, the hit 'Q' to quit is something I'm going to add in the near future after i get the rest of the kinks worked out. =)

Well, until you get to it, your program should not exit. It should go on forever. Which means that the "rep" variable should not be changed in the program. Right now you have it set to exit for 'Y' or 'N'.

http://www.cplusplus.com/reference/iostream/istream/ignore/

ignore takes parameters. Since you aren't providing any, it defaults to 1. Pass it some big number like 100.

Also, move lines 24 - 27 inside the loop. Right now you just get a cursor, so you have no idea what you're supposed to be answering.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.