This compiles fine and works well with no spaces, but once I put spaces in it either tells me its not a palindrome or times out. Any help would be greatly appreciated!
int main( )
{
queue<char> q;
stack<char> s;
string the_string;
int mismatches = 0;
cout << "Enter a line and I will see if it's a palindrome:" << endl;
cin >> the_string;
int i = 0;
while (cin.peek() != '\n')
{
cin >> the_string[i];
if (isalpha(the_string[i]))
{
q.push(toupper(the_string[i]));
s.push(toupper(the_string[i]));
}
i++;
}
while ((!q.empty()) && (!s.empty()))
{
if (q.front() != s.top())
++mismatches;
q.pop();
s.pop();
}
if (mismatches == 0)
cout << "This is a palindrome" << endl;
else
cout << "This is not a palindrome" << endl;
system("pause");
return EXIT_SUCCESS;
}
Why so complicated?
You could simply do:
#include <string>
#include <algorithm>
bool is_palindrome(std::string const& s)
{
return std::equal(s.begin(), s.begin()+s.length()/2, s.rbegin());
}
Firstly the line
cin >> the_string;
does not get a whole line. Use this instead
getline(cin, the_string);
Secondly, while debugging your algorithm print lots of information out. For example, if you add the line
cout << "You entered: '" << the_string << "'" << endl;
you can easily see what string you're actually testing.
I got this solution to work just fine.
int main( )
{
queue<char> q;
stack<char> s;
string the_string;
int mismatches = 0;
cout << "Enter a line and I will see if it's a palindrome:" << endl;
int i = 0;
while (cin.peek() != '\n')
{
cin >> the_string[i];
if (isalpha(the_string[i]))
{
q.push(toupper(the_string[i]));
s.push(toupper(the_string[i]));
}
i++;
}
while ((!q.empty()) && (!s.empty()))
{
if (q.front() != s.top())
++mismatches;
q.pop();
s.pop();
}
if (mismatches == 0)
cout << "This is a palindrome" << endl;
else
cout << "This is not a palindrome" << endl;
system("pause");
return EXIT_SUCCESS;
}
void main()
{
queue<char> q;
stack<char> s;
char letter;
int mismatches = 0;
cout << "Enter a word and I will see if it's a palindrome:" << endl;
cin >> letter;
q.push(letter);
s.push(letter);
int i = 0;
while (cin.peek() != '\n')
{
cin >> letter;
if (isalpha(letter))
{
q.push(letter);
s.push(letter);
}
i++;
}
while ((!q.empty()) && (!s.empty()))
{
if (q.front() != s.top())
++mismatches;
q.pop();
s.pop();
}
if (mismatches == 0)
{
cout << "This is a palindrome" << endl;
}
else
{
cout << "This is not a palindrome" << endl;
}
cout << endl;
cout << "Homework done!" << endl;
cout << "You are Welcome!" << endl;
system("pause");
}
Related
#include <iostream>
using namespace std;
bool play_game(int n) {
int guess;
bool noguesses = false;
int numofguesses = 0;
cout << "Welcome to my number guessing game\n";
while (n!=guess && !noguesses)
{
if (numofguesses < 6)
{
cout << "\n";
cout << "Enter your guess: ";
cin >> guess;
cout << "\n";
cout << "You entered: " << guess;
numofguesses++;
return false;
}
else
{
oog = true;
}
}
if (noguesses) {
cout << "I'm sorry. You didn't find my number.\n";
cout << "It was" << n << endl;
}
else
{
cout << "\n";
cout << "You found it in" << numofguesses << "guess(es)\n";
return true;
}
}
int main()
{
int secretnum = 5;
play_game(secretnum);
}
When I run this, the program stops after cout << "You entered: " << guess;. I want it to keep looping until the number of guesses reaches 6, or until the user inputs the correct answer.
Remove return false;
if (numofguesses < 6)
{
cout << "\n";
cout << "Enter your guess: ";
cin >> guess;
cout << "\n";
cout << "You entered: " << guess;
numofguesses++;
return false; //Remove this line
}
We we're tasked to write a program that
Display the inputted strings,
Count the number of characters in the string(s),
Count the number of vowels in the string(s),
Count the number of consonants in the string(s),
Convert the string(s) to uppercase,
Convert the string(s) to lowercase,
Compare the two strings and Combine the two strings
Now, I'm facing a problem where everytime I try to run a program it gets an error that says function-definition is not allowed here before { token and error expected } at end of input.
Code below:
#include <cstring>
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
void Display_Inputted_Strings()
{
string str1,str2;
cin >> str1;
cin >> str2;
cout << str1 << endl;
cout << str2 << endl;
return;
}
void The_Number_Of_Character()
{
char str1[10];
cout << "Input you're First String: ";
cin >> str1;
char str2[10];
cout << "Input you're Second String: ";
cin >> str2;
cout << "Lenght of string 1: " << strlen(str1) << endl;
cout << "Lenght of string 2: " << strlen(str2) << endl;
}
void The_Number_Of_Vowels()
{
bool isVowel(char ch)
{
{
ch = toupper(ch);
return (ch=='A' || ch=='E' || ch=='I' ||
ch=='O' || ch=='U');
}
}
}
int countVowels(int str)
{
int isVowel;
int count = 0;
for (int i=0; i<str.length(); i++)
if (isVowel(str[i]))
++count;
return count;
int str;
cout << "Enter a string: ";
cin >> str;
cout << "There are " << countVowels(str) << " Vowels in this string" << endl;
return 0;
}
void The_Number_Of_Consonants()
{
bool isConsonant(char ch)
{
ch = toupper(ch);
return !(ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' ||
ch == 'U') && ch >= 65 && ch <= 90;
}
int totalConsonants(string str)
{
int count = 0;
for (int i = 0; i < str.length(); i++)
if (isConsonant(str[i]))
++count;
return count;
string str;
cout << "Enter a string: ";
cin >> str;
cout << "There are a total of " << totalConsonants(str) << " Consonant";
}
}
void Convert_Strings_To_Uppercase()
{
void convert(string & s1)
{
for(int i = 0; i<s1.length(); i++)
{
s1[i] = toupper(s1[i]);
}
}
{
string s1;
cout << "Enter a string: " << endl;
getline(cin, s1);
convert(s1);
cout << s1 << endl;
return 0;
}
}
void Convert_Strings_To_Lowercase()
{
void convert(string & s1)
{
for(int i = 0; i<s1.length(); i++)
{
s1[i] = tolower(s1[i]);
}
}
{
string s1;
cout << "Enter a string: " << endl;
getline(cin, s1);
convert(s1);
cout << s1 << endl;
return 0;
}
}
void Compare_Two_Strings()
{
{
char str1[10] = "String";
char str2[10] = "Strings";
int result;
result=strcmp(str1,str2);
if(result==0)
cout << "String are equal" << endl;
else
cout << "String are not equal" << endl;
return 0;
}
}
void Combine_Two_Strings()
{
{
string s1, s2, result;
cout << "Enter string s1: ";
getline(cin,s1);
cout << "Enter string s2: ";
getline(cin,s2);
result = s1+s2;
cout << result << endl;
return 0;
}
}
void Input_String()
{
string str1;
cout << "Input a String: ";
cin >> str1;
cout << "The String You Inputted: " << str1;
}
void Exit_Program()
{
cout << "Thank You.\n";
}
int main()
{
int str1,str2;
{
cout << "Input Two Strings: ";
getline(cin, str1);
getline(cin, str2)
}
int selected
do
{
std::cout << "A.(0) Display Inputted Strings\n";
std::cout << "B.(1) Count the number of characters in the string(s)\n";
std::cout << "C.(2) Count the number of vowels in the string(s)\n";
std::cout << "D.(3) Count the number of consonants in the string(s)\n";
std::cout << "E.(4) Convert the string(s) to uppercase\n";
std::cout << "F.(5) Convert the string(s) to lowercase\n";
std::cout << "G.(6) Compare the two strings\n";
std::cout << "H.(7) Combine the two strings\n";
std::cout << "I.(8) Input another\n";
std::cout << "J.(9) Exit Program\n";
std::cin >> selected;
switch (selected)
{
case 0:
Display_Inputted_Strings();
break;
case 1:
The_Number_Of_Character();
break;
case 2:
The_Number_Of_Vowels();
break;
case 3:
The_Number_Of_Consonants();
break;
case 4:
Convert_Strings_To_Uppercase();
break;
case 5:
Convert_Strings_To_Lowercase();
break;
case 6:
Compare_Two_Strings();
break;
case 7:
Combine_Two_Strings();
break;
case 8:
Input_String();
break;
case 9:
Exit_Program();
break;
default:
std::cout << "You have entered an invalid option\n";
}
} while (selected != 9);
}
}
You probably come to the world of C++ from the world of another language like Python. It is not allowed to define a nested function within another function in C++. However you may define a lambda that may have the same semantics. For example:
void Convert_Strings_To_Lowercase()
{
auto convert = [](string & s1)
{
for(int i = 0; i<s1.length(); i++)
{
s1[i] = tolower(s1[i]);
}
};
{
string s1;
cout << "Enter a string: " << endl;
getline(cin, s1);
convert(s1);
cout << s1 << endl;
}
}
P.S. I've just converted your function into something compilable, without checking whether the code has any meaning. For example, I removed the return 0; statement as your function has the void return type.
I have this school project of mine and I need some help to troubleshoot; I've debugged as far as if it goes into the while(prod_file.good()) loop and it does, it just fails to find an existing string within a file.
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define PROD_SIZE 22
class PRODUCT {
public:
string NAME;
string QUANTITY;
} product;
int main() {
int diff;
string length;
char answer = 0;
string line;
bool foundAndReplaced = false;
cout << product.NAME.length() << endl;
cout << product.NAME;
fstream prod_file("data/available.dat", ios::out | ios::app);
do {
if(prod_file.is_open()) {
cout << "Molq vavedete imeto na produkta: \n";
getline(cin, product.NAME);
if(product.NAME.length() < 30) {
diff = 30 - product.NAME.length();
for(int i = 1; i < diff; i++){
product.NAME.append(".");
}
}
else if(product.NAME.length() > 30) {
cout << "Product name cannot exceed 30 characters.";
return 0;
}
//
cout << "Molq vavedete kolichestvoto na produkta: \n";
getline(cin, product.QUANTITY);
size_t pos;
while(prod_file.good()) {
cout << "asd\n";
getline(prod_file, line);
pos = line.find(product.NAME);
cout << "stignah\n";
if(pos != string::npos) {
prod_file << product.NAME;
prod_file << "-" << product.QUANTITY << "\n";
}
}
}
else {
cout << "Error: File inaccessible. (0x1)";
return 0;
}
cout << "Do you want to add more products? Y/N \n\n";
answer = 0;
while (answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N') {
answer = _getch();
}
}
while(answer == 'Y' || answer == 'y');
prod_file.close();
cout << "Entries added." << "\n\n";
return 0;
}
The idea of
size_t pos;
while(prod_file.good()) {
cout << "asd\n";
getline(prod_file, line);
pos = line.find(product.NAME);
cout << "stignah\n";
if(pos != string::npos) {
prod_file << product.NAME;
prod_file << "-" << product.QUANTITY << "\n";
}
}
Is to check for a certain string in a file and if it does not find the string to add to the file, if it finds it then it will not add it. Any idea why it fails the if?
You are opening prod_file for output only, not for input, so there is nothing for std::getline() to read from it.
You should NOT be trying to read from and write to the same file using a single stream. Open the original file for reading with one stream, and create a new file for writing with a separate stream. Use the first stream to scan the original file for existing entries, and use the second stream to write entries to the new file. When done, replace the original file with the new file if needed.
Try something like this:
#include <iostream>
#include <string>
#include <fstream>
#include <limits>
#include <cstdio>
using namespace std;
struct Product {
string Name;
int Quantity;
};
int main() {
string line;
char answer;
bool found, added = false, copyLines = true;
Product product;
ifstream prod_file("data/available.dat");
ofstream prod_file_new("data/available_new.dat", ios_base:::trunc);
do {
if (!prod_file || !prod_file_new) {
cerr << "Error: File inaccessible." << endl;
return 0;
}
do {
cout << "Molq vavedete imeto na produkta: " << endl;
if (!getline(cin, product.Name)) {
if (!cin.eof()) {
cerr << "Error: user input failure." << endl;
return 0;
}
break;
}
if (product.Name.length() <= 30) {
break;
}
cerr << "Product name cannot exceed 30 characters." << endl;
}
while (true);
if (cin.eof()) {
break;
}
if (product.Name.length() < 30) {
product.Name.append(30 - product.Name.length(), '.');
}
//
found = false;
do {
if (!getline(prod_file, line)) {
if (!prod_file.eof()) {
cerr << "Error: input file failure." << endl;
return 0;
}
break;
}
if (copyLines) {
if (!(prod_file_new << line << "\n")) {
cerr << "Error: output file failure." << endl;
return 0;
}
}
found = (line.find(product.Name) != string::npos);
}
while (!found);
if (!found) {
cout << "Molq vavedete kolichestvoto na produkta: " << endl;
if (!(cin >> product.Quantity)) {
cerr << "Error: user input failure." << endl;
return 0;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (!(prod_file_new << product.Name << "-" << product.Quantity << "\n")) {
cerr << "Error: output file failure." << endl;
return 0;
}
added = true;
}
cout << "Do you want to add another product? Y/N" << endl << endl;
cin >> answer;
while (answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N') {
cin >> answer;
}
if (answer != 'Y' && answer != 'y')
break;
if (!prod_file.seekg(0)) {
cerr << "Error: input file failure." << endl;
return 0;
}
copyLines = false;
}
while (true);
prod_file.close();
prod_file_new.close();
if (added) {
if (remove("data/available.dat") == 0) {
if (rename("data/available_new.dat", "data/available.dat") == 0) {
cout << "Entries added." << endl << endl;
}
else {
cerr << "Error: renaming available_new.dat to available.dat" << endl << endl;
}
}
else {
cerr << "Error: removing available.dat" << endl << endl;
}
}
else {
remove("data/available_new.dat");
cout << "No Entries added." << endl << endl;
}
return 0;
}
Alternatively, read the existing file into memory, and then search and append to that as needed, and then write out a new file only if entries have been added, eg:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <limits>
#include <cstdio>
using namespace std;
struct Product {
string Name;
int Quantity;
};
int main() {
string line;
char answer;
bool found, added = false;
Product product;
vector<Product> products;
ifstream prod_file("data/available.dat");
if (!prod_file) {
cerr << "Error: input file inaccessible." << endl;
return 0;
}
while (getline(prod_file, line)) {
istringstream iss(line);
if (!(getline(iss, product.Name, '-') && (iss >> product.Quantity))) {
cerr << "Error: input file malformed." << endl;
return 0;
}
products.push_back(product);
}
if (!prod_file) {
cerr << "Error: input file failure." << endl;
return 0;
}
prod_file.close();
do {
do {
cout << "Molq vavedete imeto na produkta: " << endl;
if (!getline(cin, product.Name)) {
if (!cin.eof()) {
cerr << "Error: user input failure." << endl;
return 0;
}
break;
}
if (product.Name.length() <= 30) {
break;
cerr << "Product name cannot exceed 30 characters." << endl;
}
while (true);
if (cin.eof()) {
break;
}
if (product.Name.length() < 30) {
product.Name.append(30 - product.Name.length(), '.');
}
//
found = std::find_if(products.begin(), products.end(),
[&](const Product &p){ return (p.Name == product.Name); }
) != products.end();
if (!found) {
cout << "Molq vavedete kolichestvoto na produkta: " << endl;
if (!(cin >> product.Quantity)) {
cerr << "Error: user input failure." << endl;
return 0;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
products.push_back(product);
added = true;
}
cout << "Do you want to add another product? Y/N" << endl << endl;
cin >> answer;
while (answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N') {
cin >> answer;
}
if (answer != 'Y' && answer != 'y')
break;
}
while (true);
if (added) {
ofstream prod_file_new("data/available_new.dat", ios_base:::trunc);
if (!prod_file_new) {
cerr << "Error: output file inaccessible." << endl;
return 0;
}
for (auto &p : products) {
if (!(prod_file_new << p.Name << "-" << p.Quantity << "\n")) {
cerr << "Error: output file failure." << endl;
return 0;
}
}
prod_file_new.close();
if (remove("data/available.dat") == 0) {
if (rename("data/available_new.dat", "data/available.dat") == 0) {
cout << "Entries added." << endl << endl;
}
else {
cerr << "Error: renaming available_new.dat to available.dat" << endl << endl;
}
}
else {
cerr << "Error: removing available.dat" << endl << endl;
}
}
else {
cout << "No Entries added." << endl << endl;
}
return 0;
}
i am using turbo c++. when i run the following code from compiler i get different answer at marked point in function result(int) then what i get from running .exe file created.
#include<fstream.h>
#include<conio.h>
#include<process.h>
#include<iomanip.h>
#include<string.h>
#include<stdio.h>
#include<dos.h>
ifstream fil;
int pos[50];
char date[11];
void exitt(int times = 0)
{
cout << endl << endl << " Enter 0 to exit." << endl;
if (times == 0)
cout << " Enter L to return to last screen." << endl;
}
void options();
void companychoose();
void companyscreen(int);
void write(int ch, int pos = 0) //add a check for duplicacy
{
ofstream fout;
clrscr();
if (ch == 1)
{
fout.open("database.dat", ios::binary | ios::app | ios::ate);
char companyname[20], temp;
exitt();
cout << " Enter Company name: ";
gets(companyname);
if (strcmp(companyname, "0") == 0)
exit(0);
else if (strcmp(companyname, "l") == 0)
options();
for (int i = 19; i>0; i--)
companyname[i] = companyname[i - 1];
companyname[0] = '%';
fout << endl;
fout << companyname;
fout.close();
cout << " Add data now?(y/n)" << endl;
askagain:
cin >> temp;
switch (temp)
{
case 'y':
fil.close();
write(2);
break;
case 'n':
options();
break;
default:
cout << " Invalid input" << endl;
goto askagain;
break;
}
}
}
void result(int ch)
{
int high[4], low[4], end, i = 0, enough = 0, temp = 0;
char check[20];
fil.open("database.dat", ios::binary);
fil.seekg(pos[ch], ios::beg);
fil >> check;
cout << endl;
if (check[0] == '%')
{
cout << " Not Enough Data!!!" << endl;
fil.close();
return;
}
while (!fil.eof())
{
if (i == 3)
{
i = 0;
enough = 1;
}
fil >> high[i] >> low[i] >> end >> check;
if (check[0] == '%')
break;
i++;
}
low[i] = 0;
temp = low[0];
if (enough == 0)
cout << " Not Enough Data!!!" << endl;
else
{
for (i = 0; i<3; i++)
{
if (low[i]<low[i + 1])
temp = low[i + 1];
}
if (temp>end)
cout << " Stock Running Low!!";
else if (temp = end)
cout << " Stock Is Stable";
else
cout << " Stock is HIGH!!";
cout << " " << end - temp << endl << endl << endl;
}
fil.close();
}
int read(int ch, int find = 0)
{
clrscr();
result(ch);
fil.open("database.dat", ios::binary);
fil.seekg(pos[ch], ios::beg);
char entry[20];
fil >> entry;
cout << setw(20) << "Date" << setw(10) << "High" << setw(10) << "Low" << setw(10) << "Close" << endl;
while (entry[0] != '%')
{
if (find == 1)
{
if (strcmp(entry, date))
return(fil.tellg() - 11);
else
continue;
}
cout << setw(20) << entry;
fil >> entry;
cout << setw(10) << entry;
fil >> entry;
cout << setw(10) << entry;
fil >> entry;
cout << setw(10) << entry << endl;
fil >> entry;
delay(500);
}
fil.close();
getch();
clrscr();
companyscreen(ch);
}
void edit(int ch)
{
cout << "Enter date of data to be edited";
gets(date);
write(2, read(ch, 1));
}
void companyscreen(int ch)
{
int ch1;
askagain:
result(ch);
cout << " 1. Add Data" << endl;
cout << " 2. Show history" << endl;
cout << " 3. Edit Data" << endl;
exitt();
ch1 = getch() - 48;
if (ch1 == 1)
write(2);
else if (ch1 == 2)
read(ch);
else if (ch1 == 3)
{
read(ch);
edit(ch);
}
else if (ch1 == 0)
{
cout << " exiting!!" << endl;
exit(500);
}
else if (ch1 == 60)
companychoose();
else
{
cout << " Invalid option chosen" << endl;
getch();
clrscr();
goto askagain;
}
}
void companychoose()
{
char name[20];
int i, ch;
clrscr();
fil.open("database.dat", ios::binary);
askagain:
fil.seekg(0, ios::beg);
cout << " Choose Company:";
cout << endl;
i = 1;
while (!fil.eof())
{
fil >> name;
if (name[0] == '%')
{
name[0] = ' ';
pos[i] = fil.tellg();
cout << setw(10) << i << "." << name << endl;
i++;
}
}
fil.close();
exitt();
ch = getch() - 48;
if (ch == 0)
exit(0);
else if (ch == 60)
options();
else if (ch>i)
{
cout << "Invalid choice" << endl;
getch();
clrscr();
goto askagain;
}
clrscr();
companyscreen(ch);
}
void options()
{
int ch;
clrscr();
askagain:
cout << endl << endl;
cout << " 1. Add company" << endl;
cout << " 2. Choose company" << endl;
exitt(1);
ch = getch() - 48;
if (ch == 1)
write(1);
else if (ch == 2)
companychoose();
else if (ch == 0)
{
cout << setw(10) << " Exiting!!";
exit(500);
}
else
{
cout << setw(10) << " Invalid choice chosen" << endl;
getch();
clrscr();
goto askagain;
}
}
void main()
{
clrscr();
textbackground(MAGENTA);
textcolor(WHITE);
clrscr();
options();
getch();
}
pls note that program is yet not fully complete so some features dont work.
i don't know how to include dat file data nor screenshot here.
i don't use visual c++ cause my pc is slow.
i don't use codeblocks cause i dont know how to use it. above code give hundreds of error even after adding "using namespace std;"
pls help me solve it. if you need anything else then ask me. thanks
I get different answer (…) in function result(int) then what I get from running .exe file created.
When you execute your program from the IDE a different working directory is used so different files are seemingly present/missing. Usually the working directory is configurable.
By the way, the goto is not needed. Really, it is not.
my code compiles but it doesn't allow the user to guess the word correctly and when it displays the correct word, it is just a string of nonsense.
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int instructions();
void manual();
void file(char*);
int letterFill(char, char*, char*);
void Unknown(char*, char*);
const int MAX_LENGTH = 10;
void main()
{
bool done = false;
char word[MAX_LENGTH];
char unknown[MAX_LENGTH];
char letter;
char name[MAX_LENGTH];
int wrong_guesses = 0;
int MAX_TRIES;
char ans;
while (!done)
{
switch (instructions())
{
case 1:
{
manual();
break;
}
case 2:
{
file(word);
break;
}
}
cout << "WHAT IS THE NUMBER OF GUESSES ALLOWED?: " << endl;
cin >> MAX_TRIES;
Unknown(word, unknown);
cout << endl << endl << "HANGMAN";
cout << endl << endl << "Each letter is represented by a star." << endl;
cout << "You have " << MAX_TRIES << " tries left.";
cout << "ENTER GUESS WHEN READY: ";
cin >> letter;
while (letter != 'N' && letter != 'n')
{
while (wrong_guesses < MAX_TRIES)
{
cout << unknown << endl;
cout << "Guess a letter: " << flush;
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "You got it wrong! You lose a guess" << endl;
wrong_guesses++;
}
else
{
cout << endl << "Pfft, you got lucky" << endl;
}
cout << "Guesses Left: " << MAX_TRIES - wrong_guesses << endl;
if (strcmp(word, unknown) == 0)
{
cout << word << endl;
cout << "You got it!" << endl;
exit(0);
}
cout << "You've been hanged." << endl;
cout << "The word was : " << word << endl;
}
}
cout << "Try again? (y/n): " << flush;
cin >> ans;
if (ans == 'y' || ans == 'Y')
done = true;
else
done = false;
}
system("pause");
}
int instructions()
{
int select = 0;
cout << endl << "HANGMAN" << endl << endl;
cout << " PROGRAM MENU" << endl;
cout << " Select option 1 or 2" << endl << endl;
cout << " 1. INPUT WORD MANUALLY" << endl;
cout << " 2. PLAY AGAINST THE COMPUTER" << endl;
cout << " 3. EXIT PROGRAM BY INPUTING: N or n" << endl << endl;
cin >> select;
return select;
}
void manual()
{
string word;
cout << endl << "INPUT WORD: " << endl;
cin >> word;
return;
}
void file(char *roc)
{
ifstream fin("word.txt");
int x;
int count = 1;
int word;
int i = 0;
srand(time(0));
word = rand() % 20;
while (count < word)
{
fin >> x;
if (x == 0)
{
count++;
}
}
do
{
fin >> x;
roc[i++] = char(x);
} while (x);
return;
}
int letterFill(char guess, char *secretword, char *guessword)
{
int i;
int matches = 0;
for (i = 0; i<MAX_LENGTH; i++)
{
if (secretword[i] == 0)
{
break;
}
if (guess == guessword[i])
{
return 0;
}
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
void Unknown(char *word, char *unknown)
{
int i;
int length = strlen(word);
for (i = 0; i<length; i++)
{
unknown[i] = '*';
}
unknown[i] = 0;
}
Again
my code compiles but it doesn't allow the user to guess the word correctly and when it displays the correct word, it is just a string of nonsense.