Reputation: 128
I was wondering if there were other type of "loops" i could use to create a much easier to read program. I'm playing with the loops and i was wondering if there was a faster or easier method, rather than repeating a loop when a word is typed...
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <cstdlib>
using namespace std;
char guess1, guess2, guess3;
int main()
{
// the word is cat...
cout << " Please enter your first guess : _ _ _ \n";
cin >> guess1;
if (guess1 == 'c')
{
cout << " please enter your second guess: c _ _ \n";
cin >> guess2;
if (guess2 == 'a')
{
cout << " please enter your second guess: c a _ \n";
cin >> guess3;
}
if (guess2 == 't')
{
cout << " please enter your second guess: c _ t \n";
cin >> guess3;
}
else
{
cout << " Wrong answer : # ";
cin >> guess3;
}
if (guess1 == 'a')
{
cout << " please enter your second guess: _ a _ \n";
cin >> guess2;
}
if (guess1 == 't')
{
cout << " please enter your second guess: _ _ t \n";
cin >> guess2;
}
else
{
cout << " Wrong answer : # ";
cin >> guess2;
}
}
return 0;
}
Upvotes: 1
Views: 222
Reputation: 7249
Here is a state machine for you. It takes a current state and an input and gives you a new state. If there is no new state for your input the user entered a wrong value.
The advantage of this is you can automate the creation of the state machine to support more words.
std::map<std::pair<std::string, char>, std::string> translation;
translation[std::make_pair("_ _ _", 'c')] = "c _ _";
translation[std::make_pair("_ _ _", 'a')] = "_ a _";
translation[std::make_pair("_ _ _", 't')] = "_ _ t";
translation[std::make_pair("c _ _", 'a')] = "c a _";
translation[std::make_pair("c _ _", 't')] = "c _ t";
translation[std::make_pair("_ a _", 't')] = "_ a t";
translation[std::make_pair("_ a _", 'c')] = "c a _";
translation[std::make_pair("_ _ t", 'c')] = "c _ t";
translation[std::make_pair("_ _ t", 'a')] = "_ a t";
translation[std::make_pair("c _ t", 'a')] = "c a t";
translation[std::make_pair("c a _", 't')] = "c a t";
translation[std::make_pair("_ a t", 'c')] = "c a t";
std::string current_state = "_ _ _";
char input;
while(current_state != "c a t") {
cout << "Please enter your guess: " << current_state << endl;
cin >> input;
std::pair<std::string, char> p = std::make_pair(current_state, input);
if(translation.find(p) == translation.end()) {
cout << "wrong answer";
continue;
}
current_state = translation[p];
}
Upvotes: 2
Reputation: 524
Sure there is. You could do something like this:
bool solved = false;
prompt = "Please enter your guess:";
char guess;
char f1='_', f2='_', f3='_'
while(!solved){
cout << prompt << f1 << f2 << f3 << std::endl;
cin.get(guess);
switch(guess){
case 'c':
f1 = 'c';
break;
case 'a':
f2 = 'a';
break;
case 't':
f3 = 't';
break;
default:
cout << "wrong answer";
break;
}
if(f1=='c' && f2=='a' && f3=='t')
solved = true;
}
You could just as easily use while(1) and put a break; in the if condition at the end. I didn't bother to actually run this so there might be a few errors but hopefully this answers the general question of how you would do such a thing.
Also I noticed you didnt have variables for your guesses, but statially used c _ _ and _ _ t. What happens when you have 2 guesses? That is why variables are a good idea to use.
Upvotes: 4
Reputation: 119
From http://en.wikipedia.org/wiki/Loop_(computing)#Loops:
A loop is a sequence of statements which is specified once but which may be carried out several times in succession. The code "inside" the loop (the body of the loop, shown below as xxx) is obeyed a specified number of times, or once for each of a collection of items, or until some condition is met, or indefinitely.
You are only using if statements in your code; those are not loops, but simple one-time conditional choices.
See http://www.cplusplus.com/doc/tutorial/control/ for some basic information about C++ loops.
Upvotes: 0