int main() {
int x;
const int Maxword = 5;
char Guess[Maxword] {};
std::string words[Maxword] = {
"Hello",
"World",
"Shift",
"Green",
"Seven"
};
srand(time(NULL));
int iSecret = rand() % Maxword;
std::string Word(words[iSecret]);
for (int i = 0; i < 5; i++) {
std::cout << Word[i] << std::endl;
}
for (int i = 0; i < 5; i++) {
std::cout << ("Please enter the letters you would like to guess") << std::endl;
std::cin >> Guess[i];
std::cout << Guess[i] << std::endl;
}
for (int i = 0; i < 5; i++) {
if (Guess[i] == Word[i]) {
std::cout << Guess[i] << "\t" << "Is in the right place" << std::endl;
} else if (Guess[i] != Word[i]) {
std::cout << Guess[i] << "\t" << "Isnt in the right place" << std::endl;
} else {
}
}
void InTheWord() {
for (int i = 0; i < 5; i++) {
}
}
I want to use elements of arrays Guess[] and Word[] how would I access them from the other function. So like I want to check if a letter from Guess[] is in the array of Word[] so id have to pass down each letter and check guess against every letter in word then return to the other function to then print out whether the letter that the person guessed was in the word that the program generated.
I want to write a program that adds words to a lexicon with pointers. However, I must not use strdup(), how can I do that? Below is what I tried but it gives me this error:
Exception thrown at 0x7C87EE72 (ucrtbased.dll) in (name of the file.exe): 0xC0000005: Access violation writing location 0xCDCDCDCD.
The problem is in the newStr() function.
#include <cstring>
#include <string>
#pragma warning (disable:4996)
#include <iostream>
using namespace std;
void delStr(char**&, int&, char*);
void newStr(char**&, int&, char*);
void printchar(char**, int, char);
char* searchStr(char**&, int&, char*);
void printAll(char**&, int);
enum ACTIONS { NEW, DELETE, SEARCH, PRINTLETTER, PRINTALL, EXIT };
int main() {
char** lexicon = NULL;//pointer to pointer fo the dictionary
int x = 0;
int size = 0;
char word[81];
char ch;
cout << "Enter 0-5:" << endl;
cin >> x;
while (x < 0 || x > 5) {//while loop for correct input
cout << "ERROR" << endl;
cin >> x;
}
while (x >= 0 && x <= 5) {
switch (x) {
case NEW:
cout << "Enter the word:" << endl;
ch = cin.get();
cin.getline(word, 80);
newStr(lexicon, size, word);
printAll(lexicon, size);
break;
case DELETE:
cout << "Enter the word to delete:" << endl;
ch = cin.get();
cin.getline(word, 80);
delStr(lexicon, size, word);
printAll(lexicon, size);
cout << endl;
break;
case SEARCH:
cout << "Enter the word to search for:" << endl;
ch = cin.get();
cin.getline(word, 80);
searchStr(lexicon, size, word);
break;
case PRINTLETTER:
cout << "Enter the char:" << endl;
ch = cin.get();
ch = cin.get();
printchar(lexicon, size, ch);
cout << endl;
break;
case PRINTALL:
if (size > 0 && lexicon != NULL) {
printAll(lexicon, size);
}
break;
case EXIT:
return 0;
break;
}
cout << "Enter your choice:" << endl;
cin >> x;
while (x < 0 || x > 5) {//while loop for correct input
cout << "ERROR" << endl;
cin >> x;
}
}
return 0;
}
void printAll(char**& lex, int size) {
for (int i = 0; i < size; i++) {
cout << lex[i] << " ";
}
cout << endl;
}
void newStr(char**& lex, int& size, char* word) {
int i = 0;
for (i = 0; i < size; i++) {
if (strcmp(lex[i], word) == 0) {
//cout << "Word " << word << " already exists\n";
return;
}
}
if (size == 0) {
lex = new char* [1];
for (int i = 0; i < strlen(word); i++) {
strcpy(*lex, word);
}
size++;
}
else {
char** temp = new char* [size + 1];
for (i = 0; i < size; i++) {
temp[i] = lex[i];
}
//strcpy(temp[size],word);
strcpy(temp[size], word);
delete[] lex;
lex = temp;
size++;
}
}
void delStr(char**& lex, int& size, char* word) {
int j = 0;
for (int i = 0; i < size; i++) {
if (strcmp(lex[i], word) == 0) {
for (j = i; j < size - 1; j++) {
lex[j] = lex[j + 1];
}
size--;
char** temp = new char* [size];
for (j = 0; j < size; j++) {
temp[j] = lex[j];
}
delete[] lex;
lex = temp;
}
}
}
void printchar(char** lex, int size, char ch) {
for (int i = 0; i < size; i++) {
if (lex[i][0] == ch) {
cout << lex[i] << " ";
}
}
}
char* searchStr(char**& lex, int& size, char* word) {
for (int i = 0; i < size; i++) {
if (strcmp(lex[i], word) == 0) {
cout << "Found" << endl;
//cout << "Word " << word << " already exists\n";
return lex[i];
}
}
cout << "Not found";
return NULL;
}
In the file a1.txt, there are ames of people and their addresses. I am thinking of storing them in strings and then comparing and printing them in alphabetic order. Here is how the information in the file looks:
Kitty Garfield
36 Jon Havey Court, Middle, MO 66222
Bill Lake
21 Ritts, Middletown, MI 48788
...The list continues
I think I have included everything in the my code shown below but the function does not run.
I am using Microsoft Visual Studio and CodeBlocks.
Here is the code:
#include <math.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct person
{
string name;
string address;
};
int getWhatTheyWant();
void displayPeople();
void addPerson();
void searchByLastName();
void printAllSearchedAndFoundPeople();
int main()
{
int whatTheyWant;
do
{
whatTheyWant = getWhatTheyWant();
switch (whatTheyWant)
{
case 1:
displayPeople();
break;
case 2:
addPerson();
break;
case 3:
searchByLastName();
break;
case 4:
printAllSearchedAndFoundPeople();
break;
case 5: //just not to print the cout statement after
break;
default:
cout << "Please enter something valid" << endl;
}
} while (whatTheyWant != 5);
cout << endl << endl;
system("PAUSE");
return(0);
}
//display
int getWhatTheyWant()
{
int choice;
cout << "1-Print people in the alphabetic order" << endl;
cout << "2- If you want to add people" << endl;
cout << "3-Search by last name" << endl;
cout << "4-Print all the peope who are searched and found" << endl;
cout << "5- Exit the program" << endl;
cin >> choice;
return(choice);
}
//printing the people
void displayPeople()
{
ifstream theFile("a1.txt");
if (theFile.is_open())
{
person x[6];
string justToGetFromFile;
string *temporary;
temporary = new string[12];
int whereFirstLetterOfSurnameIs[6];
char whatIsTheFirstLetterOfSurname[6];
for (int i = 0; i < 12; i++)
{
getline(theFile, justToGetFromFile);
temporary[i] = justToGetFromFile;
}
theFile.close();
for (int i = 0; i < 6; i++)
{
x[i].name = temporary[2 * i];
x[i].address = temporary[2 * i + 1];
}
for (int i = 0; i < 6; i++)
{
whereFirstLetterOfSurnameIs[i] = x[i].name.find(" ", 0) + 1;
whatIsTheFirstLetterOfSurname[i] = x[i].name.at(whereFirstLetterOfSurnameIs[i]);
}
for (int j = 1; j < 6; j++)
{
for (int k = 0; k < 6; k++)
{
if (whatIsTheFirstLetterOfSurname[j - 1] > whatIsTheFirstLetterOfSurname[j])//comparison as the letters are numbers
{
person temp;
temp = x[j];
x[j] = x[j - 1];
x[j - 1] = temp;
}
}
}
for (int i = 0; i < 6; i++)
{
cout << x[i].name << endl << x[i].address << endl << endl;
}
}
else
{
cout << "you messed it up" << endl;
}
}
void addPerson()
{
}
void searchByLastName()
{
}
void printAllSearchedAndFoundPeople()
{
}
Im writing a program, which is supposed to print "X' made of ASCII chars in the console, change size and allow to move it with keys. I know how to print this X and change a size, but I'm totally stuck and i don't know how to move it.
#include <iostream>
#include <conio.h>
#include <Windows.h>
void Intro();
void Draw();
const int Esc = 27;
int main()
{
Intro();
Draw();
return 0;
}
void Intro()
{
std::cout << "Napisz program rysowania znakiem ponizszej figury:\n";
std::cout << " * * \n";
std::cout << " * * \n";
std::cout << " * \n";
std::cout << " * * \n";
std::cout << " * * \n";
std::cout << std::endl;
std::cout << "Program powinien umozliwiac:\n"
<< " - Wybor znaku kodu ASII,\n"
<< " - Wczytanie poczatkowych rozmiarow figury,\n"
<< " - Zmiane wielkosci figury klawiszami '+' i '-',\n"
<< " - Przesuwanie figury w czterech kierunkach za pomoca kursorow,\n"
<< " - Ograniczenie przesuwania i rozmiarow figury do obszaru ekranu.\n";
_getch();
return;
}
void Draw()
{
int Size;
char AsciiChar;
char Tab[50][80];
int AsciiCharPosX = 0;
int AsciiCharPosY = 0;
char Key;
system("cls");
std::cout << "Enter the size: ";
std::cin >> Size;
std::cout << std::endl;
std::cout << "Enter the ASCII char from the keyboard: ";
std::cin >> AsciiChar;
std::cout << std::endl;
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
Tab[i][j] = 'e'; // e - empty field
}
}
Tab[AsciiCharPosX][AsciiCharPosY] = 'f'; //f - filled
do
{
system("cls");
for (int Rows = 1; Rows <= Size; Rows++)
{
for (int Cols = 1; Cols <= Size; Cols++)
{
if (Rows == Cols || Cols == (Size + 1) - Rows)
{
Tab[Rows][Cols] = 'f';
if (Tab[Rows][Cols] == 'f')
{
std::cout << AsciiChar;
}
}
else
{
Tab[Rows][Cols] = 'e';
if (Tab[Rows][Cols] == 'e')
{
std::cout << " ";
}
}
}
std::cout << std::endl;
}
Key = _getch();
switch (Key)
{
case '+':
{
Size = Size + 2;
break;
}
case '-':
{
Size = Size - 2;
break;
}
case 's':
{
AsciiCharPosY++;
break;
}
case 'w':
{
AsciiCharPosY--;
break;
}
case 'a':
{
AsciiCharPosX--;
break;
}
case 'd':
{
AsciiCharPosX++;
break;
}
}
} while (Key != Esc);
return;
}
To move it you will need to follow one of 2 aproaches:
Console control
Write code that can send special characters that the console picks up to mean special things like move cursor or change color etc. The most common way is tu use a library for this, like libncurses. Please note that this depends greatly on the console you are using, and sxo you need to find which code or library to use based on that.
Faking it
You can fake it by simply rewriting the whole screen-full of text while incorporating the changes you want and hope your victim (user) does not scroll upward to see what is going on.
In both cases, you will need to write out changes in steps (frames) and you will benefit by knowing about animation.
Good luck!
I am trying to solve this Problem.The question is as follows
Given an input string and a dictionary of words, find out if the input string can be segmented into a space-separated sequence of dictionary words.
Dictionary is an array of strings.
My Approach is the following recursive fn with storing of the results of recursive calls. The output is fine but I see that the stored result is never used.
My solution is hopefully correct as it passed the test cases.But I would be great if I know whether DP is used.
The code is:
#include <iostream>
#include <string.h>
using namespace std;
int r[100][100] = {0}; //To Store the calculated values
bool searchWord(char q[], char D[][20], int start, int end) {
cout << "In Search Word Loop with " << start << " " << end << endl;
char temp[end - start + 1];
int j = 0;
for (int i = start; i <= end ; ++i) {
//cout << "Looping i " << i << endl;
temp[j] = q[i];
j++;
}
// cout << "For Word " << temp << endl;
for (int i = 0; i < 12; ++i) {
// cout << "Comparing with " << D[i] << endl;
if (!strcmp(temp, D[i])) {
cout << "Found Word" << temp << " " << D[i] << endl;
return 1;
}
}
return 0;
}
bool searchSentence(char q[], char D[][20], int qstart, int qend) {
cout << "In Search Sentence Loop" << endl;
if (r[qstart][qend] != 0) {
cout << "DP Helped!!!" << endl;
return 1;
}
if (qstart == qend) {
if (searchWord(q, D, qstart, qstart))
return 1;
else return 0;
}
if (qstart > qend) return 1;
int i;
for (i = qstart; i <= qend; i++) {
if (searchWord(q, D, qstart, i)) {
r[i + 1][qend] = searchSentence(q, D, i + 1, qend);
if (r[i + 1][qend] == 1) return 1;
}
}
return 0;
}
int main() {
char D[20][20] = { "i", "like", "sam", "sung", "samsung", "mobile", "ice", "cream", "icecream", "man", "go", "mango"};
char q[100] = "samsungmango";
int index = 0; char ch;
ch = q[0];
while (ch != '\0') {
index++;
ch = q[index];
}
if (searchSentence(q, D, 0, index - 1))
cout << "Yes" << endl;
else cout << "No" << endl;
}
Is recursion mandatory? I see, iterative DP-solution is easiest and compact:
#include <stdio.h>
#include <string.h>
int main() {
const char *D[] = { "i", "like", "sam", "sung", "samsung", "mobile", "ice", "cream", "icecream", "man", "go", "mango", NULL};
const char q[] = "samsungmango";
char dp[100];
short d_len[20];
memset(dp, 0, sizeof(dp));
dp[0] = 1; // 0 element is always reacheable
int i, j;
// compute dict string lengths
for(i = 0; D[i]; i++)
d_len[i] = strlen(D[i]);
// Compute splits using DP array
for(i = 0; q[i] != 0; i++)
if(dp[i]) // this index is reacheable
for(j = 0; D[j]; j++) // try to make next reacheable indexes
if(strncmp(&q[i], D[j], d_len[j]) == 0)
dp[i + d_len[j]] = 1; // That position is reacheable, too
// if EOLN(q) is reached, then yes
printf("Answer is %s\n", dp[i]? "YES" : "NO");
} // main
Your code is actually wrong. To fail your code, try input like "likeman"
Note that there are two different return values possible from function searchSentence, 0 or 1. So if you initialize the r array with 0 there's no guarantee it's a new state when r[x][y] = 0. Initialize r array with some impossible value like -1 or 2 for this program and test again. Now you can easily confirm that if r[qbegin][qend] != -1 then this state has already been checked so you can return r[qbegin][qend] from here
Updated code :
#include <iostream>
#include <string.h>
using namespace std;
int r[100][100]; //To Store the calculated values
bool searchWord(char q[], char D[][20], int start, int end)
{
cout << "In Search Word Loop with " << start << " " << end << endl;
char temp[end - start + 1];
int j = 0;
for (int i = start; i <= end ; ++i)
{
//cout << "Looping i " << i << endl;
temp[j] = q[i];
j++;
}
temp[j] = '\0';
//cout << "For Word " << temp << endl;
for (int i = 0; i < 12; ++i)
{
// cout << "Comparing with " << D[i] << endl;
if (!strcmp(temp, D[i]))
{
cout << "Found Word" << temp << " " << D[i] << endl;
return 1;
}
}
return 0;
}
bool searchSentence(char q[], char D[][20], int qstart, int qend)
{
cout << "In Search Sentence Loop" << endl;
if (r[qstart][qend] != -1)
{
cout << "DP Helped!!!" << endl;
return r[qstart][qend];
}
if (qstart == qend)
{
if (searchWord(q, D, qstart, qstart))
return 1;
else return 0;
}
if (qstart > qend) return 1;
int i;
for (i = qstart; i <= qend; i++)
{
if (searchWord(q, D, qstart, i))
{
r[i + 1][qend] = searchSentence(q, D, i + 1, qend);
if (r[i + 1][qend] == 1) return 1;
}
}
return 0;
}
int main()
{
char D[20][20] = { "i", "like", "sam", "sung", "samsung", "mobile", "ice", "cream", "icecream", "man", "go", "mango"};
char q[100] = "ilike";
int index = 0; char ch;
ch = q[0];
memset(r, -1, sizeof(r));
while (ch != '\0')
{
index++;
ch = q[index];
}
if (searchSentence(q, D, 0, index - 1))
cout << "Yes" << endl;
else cout << "No" << endl;
}
P.S : There are some redundant lines of codes but I didn't change them and I added a null character in the end of the character array temp in function searchWord