C++ Hangman Project - c++

I have a problem with my Hangman code.
Here's the error what i get:
error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'char' for argument '1' to 'int checkGuess(char, std::__cxx11::string, std::__cxx11::string&)'
Never really seen any errors like this.
Here's my code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int NUM_TRY = 15;
int checkGuess(char, string, string&);
void main_menu();
string word;
void beolvas(vector <string> &words);
void kiir (vector <string> words);
void betolt (vector <string> words);
string message = "Play!";
int main(int argc, char *argv[])
{
vector <string> words;
string name;
string letter;
beolvas(words);
kiir(words);
betolt(words);
string hide_m(word.length(), '-');
while(NUM_TRY != 0){
main_menu();
cout<<"\n\n\t\t\t\t" <<hide_m;
cout<<"\n\n\t\t\t\tMondj egy betut: ";
getline(cin, letter);
if (checkGuess(letter, word, hide_m)==0)
{
message = "Incorrect letter!";
NUM_TRY = NUM_TRY - 1;
}
if (word == hide_m);
{
cout<<"Congratulations, you guessed the word."<<endl;
cout<<"\t\t\tThe word was "<<word<<endl;
break;
}
if (letter == hide_m)
{
cout<<"Good job. You guessed a letter."<<endl;
}
}
}
void beolvas(vector <string> &words)
{
string sor;
ifstream fin("words.txt");
while(!fin.eof()){
getline(fin, sor);
words.push_back(sor);
}
fin.close();
}
void kiir(vector <string> words)
{
ofstream fout("olvasd.txt");
srand(time(NULL));
int n =rand() % 236;
fout<<words[n]<<endl;
}
void betolt(vector <string> words)
{
ifstream input("olvasd.txt");
while(!input.eof()) {
getline(cin, word);
}
input.close();
}
int checkGuess(char guess, string secretword, string &guessword)
{
int i;
int matches = 0;
int len = secretword.length();
for(i = 0; i < len; i++)
{
if(guess == guessword[i])
{
return 0;
}
if(guess == guessword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
void main_menu()
{
system("color B"); //Light Aqua
system("cls");
cout<<"\t\t\t\t\t";
cout<<"\n\t\t\tHangman";
cout<< "\n\t\tYou have" <<NUM_TRY <<" tries, to guess the word. ";
cout<<"\n\n\t\t\t\t"+message;
}

checkGuess takes a char type as first parameter, but you are providing letter which is actually of type std::string. This code won't compile.
You can read letter[0] instead to get the first char of the string. Make sure letter.length() is bigger than 0 first.

Related

converting strings to integers by using "stringstream "

I am trying to convert strings of data to integers, (to use it for some calculations ) by using stringstream , but it fails when there is a space.
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
int main() {
string line;
vector <string>data;
for (int i = 0; i < 10;i++) {
getline(cin,line);
data.push_back(line);
}
///converting digits to int
vector<int> values;
int n;
char ch=',';
for (int i = 0; i < data.size();i++) {
stringstream stream(data[i]);
while( stream >>n ) {
if(stream >> ch) {
values.push_back(n);
}
else {
values.push_back(n);
}
cout<<n<<" ";
}
cout<<endl;
}
return 0;
}
input : 1,182,08 51 15 --> output : 1 182 8 1 5
there are some digits lost after spaces.
so, what can I do to avoid it?
Complete working code based on seccpur's answer. Visual Studio 2017 Community:
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
#define BUFSZ 100 // Set max size of the numbers as a single string
int convertToIntegers(char *s, vector<int> &values);
int main()
{
int count;
int i;
char data[BUFSZ];
vector<int> values;
strcpy_s(data, "1,182,08 51 15");
count = convertToIntegers(data, values);
// for (auto val : values) // Show the result
// cout << val << '\n';
// *** OR ***
for (i = 0; i < count; i++)
cout << values[i] << '\n';
}
//////////////////////////////////////
// Convert a C string to integers
//
int convertToIntegers(char *s, vector<int> &values)
{
vector<string> numbers;
char *next_token;
char* ptr = strtok_s(s, " -.,;", &next_token);
while (ptr)
{
string str(ptr);
numbers.push_back(str);
ptr = strtok_s(NULL, " -.,;", &next_token); // Next number
}
//
// Convert the resulting strings to integers
//
for (auto str : numbers)
values.push_back(stoi(str));
return (int)values.size();
}
If you know you have exactly one character as a separator, either a space, either a comma, the following code will work:
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
int main() {
string line;
vector <string>data;
for (int i = 0; i < 10;i++) {
getline(cin,line);
data.push_back(line);
}
///converting digits to int
vector<int> values;
int n;
char ch=',';
for (int i = 0; i < data.size();i++) {
stringstream stream(data[i]);
while( stream >>n ) {
char c = stream.get();
//if(stream >> ch) {
// values.push_back(n);
//}
//else {
values.push_back(n);
//}
cout<<n<<" ";
}
cout<<endl;
}
return 0;
}
You are using multiple delimiters in the input ( like whitespace : , ; -) which complicates the matter. Here's a possible snippet using std::strtok:
//Enter a line
string line;
getline(cin, line);
// Convert string to char* so that std::strtok could be used later
char *cstr = new char[line.length() + 1];
std::strcpy(cstr, line.c_str());
vector<string> words;
// split line into multiple strings using multiple delimiters
char* ptr = std::strtok(cstr, " -.,;");
while (ptr)
{
string str(ptr);
words.push_back(str);
ptr = strtok(NULL, " -.,;");
}
delete[] cstr;
// Convert string to int
vector<int> values;
for (auto str : words){
values.push_back(std::stoi(str));
}
// Print the values
for (auto val : values){
cout << val << '\n';
}

c++ reverse line using stack compile error no operator matches operand

I'm writing a program to reverse the string using a stack. I'm getting 2 errors in my code. 1. no operator >> matches the operand 2. On the line Reverse(string); it errors say (string)type name is not allowed. Any idea why?
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
void Reverse(char);
int main()
{
char object;
cout << "Please enter a line of text.\n";
cin >> object;
char * point = new char[object.length() + 1];
strcpy(point, object.c_str());
Reverse(point);
Reverse(point);
printf(" %s", object);
system("pause");
return 0;
}
void Reverse(char *p)
{
stack<char> S;
for (int i = 0; i<strlen(p); i++)
S.push(p[i]);
for (int i = 0; i<strlen(p); i++)
{
p[i] = S.top();
S.pop();
}
}
updated code: error on cin >> object says the initial question no operator matches an operand
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
void Reverse(string);
int main()
{
string object;
cout << "Please enter a line of text.\n";
cin >> object;
char * point = new char[object.length() + 1];
strcpy(point, object.c_str());
Reverse(point);
printf(" %s", point);
system("pause");
return 0;
}
void Reverse(char *p)
{
stack<char> S;
for (int i = 0; i<strlen(p); i++)
S.push(p[i]);
for (int i = 0; i<strlen(p); i++)
{
p[i] = S.top();
S.pop();
}
}
I am getting error strcpy_s does not take 2 arguments.
#include <iostream>
#include <cstring>
#include <string>
#include <stack>
using namespace std;
void Reverse(char *p)
{
stack<char> S;
for (int i = 0; i<strlen(p); i++)
S.push(p[i]);
for (int i = 0; i<strlen(p); i++)
{
p[i] = S.top();
S.pop();
}
}
int main()
{
string object;
cout << "Please enter a line of text.\n";
cin >> object;
char * point = new char[object.length() + 1];
strcpy_s(point, object.c_str());
Reverse(point);
printf(" %s", point);
system("pause");
return 0;
}
char * point = new char[object.length()+1];//+1 for the null terminator
strcpy(point, object.c_str());
Reverse(point);
printf("%s",point);
allocates space for point then copies http://en.cppreference.com/w/cpp/string/byte/strcpy and you were calling Reverse like this Reverse(char) you need to call it using the name of the char variable like this Reverse(point); since we allocated space we need to delete it after we are done using it.
Please check the modified code below. I can reverse the input string now.
#include <iostream>
#include <string>
#include <stack>
using namespace std;
void Reverse(char *p)
{
stack<char> S;
for (int i = 0; i<strlen(p); i++)
S.push(p[i]);
for (int i = 0; i<strlen(p); i++)
{
p[i] = S.top();
S.pop();
}
}
int main()
{
string object;
cout << "Please enter a line of text.\n";
cin >> object;
char * point = new char[object.length() + 1];
strcpy(point, object.c_str());
Reverse(point);
printf(" %s", point);
system("pause");
return 0;
}

counting words on in an string consisting alphabets,\n,\t and space characters

Given a string consisting of spaces,\t,\n and alphabets,task is to count the number of words where spaces,\t and \n work as separators.
For this problem, I wrote following code, which doesn't works properly, and suggestions why?
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
bool isalpha(char ch)
{
if((ch>='A'&& ch<='Z')||(ch>='a'&& ch<='z'))
return true;
return false;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string str;
int count=0;
getline(cin,str);
for(int i=0;i<str.length();i++)
{
if(isalpha(str[i]))
{
count++;
while(str[i]!='\n'||str[i]!='\t'||str[i]!=' ')
{
i++;
}
}
else
continue;
}
cout<<count<<endl;
}
return 0;
}

How to access numbers in string and convert it to integer?

I am using stoi function here and it is giving invalid argument error...
Here, the input file is something like "S13S12S11S10S1". I want to save the numbers in an array rank like rank[0]=13 rank[1]=12 and so on...
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
ifstream fin("input.txt");
string input;
fin>>input;
int count=0;
int val;
int rank[4];
for(int i=0 ; i < input.size(); i++)
{
string s1,s2;
s1=input[i];
s2=input[i+1];
if(s1[0]!='S' && s1[0]!='H' &&s1[0]!='D' && s1[0]!='C')
{
int a=stoi(s1);
rank[count]=a;
if(s2[0]!='S' && s2[0]!='H' &&s2[0]!='D' &&s2[0]!='C')
{
int temp;
int b=stoi(s2);
rank[count]=10+b;
count++;
i++;
}
else{
count++;
}
}
}
for (int count=0; count<=4 ; count++)
{
cout<<rank[count];
cout<<"\n";
}
}
You can tokenize the input string, using 'SHDC' for delimiters. And then use atoi to convert the tokens to integers. I would use a vector to store your rank values, if your input file(s) could have a varying number of tokens.
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
ifstream fin("input.txt");
string input;
fin >> input;
const char *delimiters = "SHDC";
char *next_token = NULL;
char *token = strtok_s(const_cast<char*>(input.c_str()), delimiters, &next_token);
vector<int> values;
while (token != NULL) {
values.push_back(atoi(token));
token = strtok_s(NULL, delimiters, &next_token);
}
for (int i = 0; i < values.size(); ++i) {
cout << values[i] << endl;
}
}

How to alphabetically sort strings?

I have been trying to use this c++ program to sort 5 names alphabetically:
#include <iostream>
#include <cstring>
#include <conio.h>
using namespace std;
int main()
{
char names[5][100];
int x,y,z;
char exchange[100];
cout << "Enter five names...\n";
for(x=1;x<=5;x++)
{
cout << x << ". ";
cin >> names[x-1];
}
getch();
for(x=0;x<=5-2;x++)
{
for(y=0;y<=5-2;y++)
{
for(z=0;z<=99;z++)
{
if(int(names[y][z])>int(names[y+1][z]))
{
strcpy(exchange,names[y]);
strcpy(names[y],names[y+1]);
strcpy(names[y+1],exchange);
break;
}
}
}
}
for(x=0;x<=5-1;x++)
cout << names[x];
return 0;
}
If I enter Earl, Don, Chris, Bill, and Andy respectively, I get this:
AndyEarlDonChrisBill
Could someone please tell me whats wrong with my program?
You could use std::set or std::multiset (if you will allow repeated items) of strings, and it will keep the items sorted automatically (you could even change the sorting criteria if you want).
#include <iostream>
#include <set>
#include <algorithm>
void print(const std::string& item)
{
std::cout << item << std::endl;
}
int main()
{
std::set<std::string> sortedItems;
for(int i = 1; i <= 5; ++i)
{
std::string name;
std::cout << i << ". ";
std::cin >> name;
sortedItems.insert(name);
}
std::for_each(sortedItems.begin(), sortedItems.end(), &print);
return 0;
}
input:
Gerardo
Carlos
Kamilo
Angel
Bosco
output:
Angel
Bosco
Carlos
Gerardo
Kamilo
You can use the sort function:
#include <algorithm>
#include <vector>
using namespace std;
...
vector<string> s;
sort(s.begin(),s.end());
You are using too much unnecessary loops. Try this simple and efficient one. You need to just swap when a string is alphabetically latter than other string.
Input
5
Ashadullah
Shawon
Shakib
Aaaakash
Ideone
Output
Aaaakash
Ashadullah
Ideone
Shakib
Shawon
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s[200],x[200],ct,dt;
int i,j,n;
cin>>n;
for(i=0;i<n;i++)
{
cin>>s[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(s[i]>s[j])
{
ct=s[i];
s[i]=s[j];
s[j]=ct;
}
}
}
cout<<"Sorted Name in Dictionary Order"<<endl;
for(i=0;i<n;i++)
{
cout<<s[i]<<endl;
}
return 0;
}
Your code implements a single-pass of bubble sort. Essentially missing the 'repeat until no changes are made to the array' loop around the outside.
The code does not take care when the names are already in order. Add the following
else if(int(names[y][z])<int(names[y+1][z]))
break;
To the if statement.
Putting this here in case someone needs a different solution.
/* sorting example */
#include <iostream>
using namespace std;
bool isSwap( string str1, string str2, int i)
{
if(str1[i] > str2[i])
return true;
if(str1[i] == str2[i])
return isSwap(str1,str2,i+1);
return false;
}
int main()
{
string str[7] = {"you","your","must","mike", "jack", "jesus","god"};
int strlen = 7;
string temp;
int i = 0;
int j = 0;
bool changed = false;
while(i < strlen-1)
{
changed = false;
j = i+1;
while(j < strlen)
{
if(isSwap(str[i],str[j],0))
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
changed = true;
}
j++;
}
if(changed)
i = 0;
else
i++;
}
for(i = 0; i < strlen; i++)
cout << str[i] << endl;
return 0;
}