int main() {
int x;
const int Maxword = 5;
std::string Guess[Maxword];
std::string words[Maxword] = {
"Hello",
"World",
"Shift",
"Green",
"Seven"
};
srand(time(NULL));
int iSecret = rand() % 4 + 1;
std::string Cool(words[iSecret]);
for (int i = 0; i < 5; i++) {
std::cout << Cool[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] == Cool[i]) {
std::cout << Guess[i] << "Is in the word";
}
}
For this statement here at the Bottom for statement within the if statement it has a no operator dont mind the actual code it is just a rough draft before I make the actual code but i dont see the problem.
Cool is a string. Cool[i] is a character. Guess is an array of strings. Guess[i] is a string. You're trying to compare a character to a string. You probably mean if (guess[i] == Cool)
I can't seem to get my array's length or to print backwards! ANY HELP PLEASE?!
the functions on the bottom for void GetStringLenght and Print Backwards aren't working
#include <iostream>
void ReadString(char* c, int maxLength);
void GetStringLength(char* c, int* length);
void PrintString(char* const c);
void PrintStringBackwards(char* const c);
int main()
{
const int SIZE = 50;
char ca[SIZE];
char* pc = ca;
int fPrints = 0;
int bPrints = 0;
int lengthChecks = 0;
char selection = 'z';
while (selection != 'Q') {
std::cout << "\n[ 1] Test ReadString\n";
std::cout << "[ 2] Test GetStringLength\n";
std::cout << "[ 3] Test PrintString\n";
std::cout << "[ 4] Test PrintStringBackwards\n";
std::cout << "[Q] Quit\n";
std::cout << "Selection: ";
std::cin >> selection;
std::cin.ignore();
std::cout << std::endl;
switch (selection) {
//Test ReadString
case '1':
ReadString(pc, SIZE);
break;
//Test GetStringLength
case '2': {
lengthChecks += 1;
int length = 0;
GetStringLength(pc, &length);
std::cout << "Length[" << lengthChecks << "]=" << length << std::endl;
break;
}
//Test PrintString
case '3':
fPrints += 1;
std::cout << "Foward[" << fPrints << "]=";
PrintString(pc);
std::cout << std::endl;
break;
//[ 4] Test PrintStringBackwards
case '4':
bPrints += 1;
std::cout << "Backwards[" << bPrints << "]=";
PrintStringBackwards(pc);
std::cout << std::endl;
break;
case 'Q':
break;
default:
break;
} //end switch
} //end while
std::cout << "Press ENTER";
std::cin.get();
return 0;
}
void ReadString(char* c, int maxLength)
{
std::cout << "Enter a string less than " << maxLength << " characters." << std::endl;
std::cin.getline(c, maxLength, '\n');
}
//BELOW THIS DOESNT WORK EITHER///
//////////////////////////////////////////
void GetStringLength(char* c, int* length)
{
for (int i = 0; i < *length; i++) {
if (c[i] == '\0')
*length = i - 1;
}
}
void PrintString(char* const c)
{
int counter = 0;
for (int i = 0; i < 100; i++) {
if (c[i] == '\0') {
counter = i;
break;
} //end if
} //end for
for (int j = 0; j < counter; j++) {
std::cout << c[j];
if (j == counter)
std::cout << '\0';
} //end for
std::cout << std::endl;
} //end void
void PrintStringBackwards(char* const c)
{
//this is where I’m lost! I’ve tried 25 different ways and everything is error.
}
Consider for (int i = 0; i < *length; i++) where both i and *length are 0, as in your case... will that loop ever execute? Nope... Consider a loop that starts with x set to 0 and return x; when str[x] is '\0'. i.e. the loop in strlen does this.
As for printing backwards, start at the highest index (returned by strlen or your function once fixed) and decrement until you reach 0, printing the characters at those offsets as you go.
strlen is available in <cstring>, by the way, not just <string>... and as it is a mandatory function it'll always be part of any C++ implementation. You should probably just use strlen...
To get a C string (char*)'s length you can use strlen(...).
To print backwards, do:
for(int i = strlen(str) - 1; i >= 0; --i)
std::cout << str[i];
i have a problem on this code
the errors says ISO c++ forbids comparison between pointer and integer
here is the exercise problem
-the answers to a true false test are as follows TTFFT given a two dimensional answer array in which each row corresponds to the answers provided on one test, write a function that accepts the two dimensional array and the number of tests as parameters and returns a one dimensional array containing the grades for each test.(each question is worth 5 points so that the maxinum possible grade is 25)
any answers will help.
this is the code
#include <iostream>
using namespace std;
int numberofgrades(char [][5], int []);
int main()
{
char testanswers[5][5] = {0};
int testgrades[5] = {0};
int counting1, counting2, counting3;
counting1 = 1;
counting2 = 1;
counting3 = 1;
cout << "this program will record the testgrades you entered: "
<< endl;
for(int i = 0; i < 5; i++)
{
for(int k = 0; k < 5; k++)
{
cout << "enter answers for test no. " << counting1 << ": ";
cin >> testanswers[i][k];
}
counting1++;
}
numberofgrades(testanswers,testgrades);
cout << "testing..." << endl;
cout << endl;
for(int o = 0; o < 5; o++)
{
cout << "test no. " << counting2;
for(int l = 0; l < 5; l++)
{
cout << " " << testanswers[o][l];
}
counting2++;
cout << endl;
}
cout << "testing...." << endl;
cout << endl;
cout << "the total number of scores per test no. is: "
<< endl;
for(int t = 0; t < 5; t++)
{
cout << "test no. " << t << ": " << testgrades[t] << endl;
}
return 0;
}
int numberofgrades(char t1a [][5], int tg1[])
{
for(int j = 0; j < 5; j++)
{
for(int i = 0; i < 5; i++)
{
if(t1a[i] == 't') //this is the problem
{
tg1[j] = tg1[j] + 5;
}
else if(t1a[i] == 'T')
{
tg1[j] = tg1[j] + 5;
}
else if(t1a[i] == 'f')
{
tg1[j] = tg1[j] + 0;
}
else if(t1a[i] == 'F')
{
tg1[j] = tg1[j] + 0;
}
else{
cout << "invalid letter exiting the program"
<< endl;
return 0;
}
}
}
return tg1[5];
}
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
Hey I was wondering how I could have settings in-game which would allow the user to set the size of the 'game-board' by changing the array values. Here is the code. I know the code is messy and over the place but it is my first program.
#include "stdafx.h"
#include "iostream"
#include "string"
#include "cstdlib"
#include "ctime"
int xRan;
int choicei = 17;
int choicej = 17;
const int row = 15;
const int col = 16;
int play = 0;
void fill(char Array[row][col]);
int main()
{
int play = 0;
char Array[row][col];
srand((unsigned int)time(0));
xRan = rand() % 15 + 1;
if (play == 0)
{
std::cout << "1. To Play Treasure Hunt!" << std::endl;
std::cout << "2. How To Play Treaure Hunt!" << std::endl;
std::cout << "3. Treaure Hunt Settings! (Comming Soon)\n" << std::endl;
std::cin >> play;
std::cout << "-----------------------------------------------------------------------" << std::endl;
}
if (play == 2)
{
std::cout << "1. Select a row number. Be sure to make it less than or equal to " << row << "!" << std::endl;
std::cout << "2. Select a column number. Be sure to make it less than or equal to " << col << "!" << std::endl;
std::cout << "3. If you see the 'X' you have won! If you see the 'O' you lose!" << std::endl;
std::cout << "-----------------------------------------------------------------------\n" << std::endl;
std::cin >> play;
}
if (play == 3)
{
std::cout << "\nComming Soon!" << std::endl;
std::cout << "-----------------------------------------------------------------------\n" << std::endl;
std::cin >> play;
}
while (choicei > row || choicej > col || choicei < 1 || choicej < 1)
{
std::cout << "\nEnter The Row Number Less Than Or Equal To " << row << "!" << std::endl;
std::cin >> choicei;
std::cout << std::endl;
std::cout << "Enter The Column Number Less Than Or Equal To " << col << "!" << std::endl;
std::cin >> choicej;
std::cout << "\n-----------------------------------------------------------------------" << std::endl;
if (choicei > row || choicej > row)
{
std::cout << "Make Sure The Row And Column Numbers Are Less Than Or Equal To " << row << "and" << col << "!\n" "---------------------------------------------------------------------- - " << std::endl;
}
if (choicei < 1 || choicej < 1)
{
std::cout << "Make Sure The Row And Column Numbers Are More Than Or Equal To 1" << "!\n" "-----------------------------------------------------------------------" << std::endl;
}
}
fill(Array);
std::cout << std::endl;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
std::cout << Array[i][j] << " ";
}
std::cout << std::endl;
}
if (xRan > 11)
{
std::cout << "\nCongratulations! You Won!\n" << std::endl;
}
else
{
std::cout << "\nBetter Luck Next Time!\n" << std::endl;
}
}
void fill(char Array[row][col])
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Array[i][j] = '*';
}
}
if (xRan > 11)
{
for (int i = 0; i < 1; i++)
{
for (int j = 0; j < col; j++)
{
Array[choicei - 1][choicej - 1] = 'X';
}
}
}
else
{
for (int i = 0; i < 1; i++)
{
for (int j = 0; j < col; j++)
{
Array[choicei - 1][choicej - 1] = 'O';
}
}
}
}
Thank you in advance.
you can't do that with ordinary arrays. you should use dynamic arrays, for example std::vector http://www.cplusplus.com/reference/vector/vector/
Actually, what you want to do can be done in C, not in C++: C++ requires array dimensions to be compile time constants, C can use any runtime value.
If you stay in C++, you should take a look at vector<>. If, however, you choose to use C you can simply remove the const from the declaration of row and col.
You may find this answer useful. It lists several methods to create dynamic arrays.
Quoting the answer :
In C++, variable length arrays are not legal. G++ allows this as an "extension" (because C allows it), so in G++ (without being -pedantic about following the C++ standard)
Based on the suggestions, here are some ways you could initialize it (ignoring how you take the input value) :-
vector<vector<char>> Array(row, vector<char>(col));
or
char **Array = new char*[row];
for(int i = 0; i < row; i++)
{
Array[i] = new char[col];
}
UPDATE
Based on the comments, I am adding how to use the vector method and use it with the function 'fill'. fill uses reference while fill_with_ptr makes use of pointer. Although I list both the methods, I strongly recommend the one using reference.
void fill(vector<vector<char> >& array);
void fill_with_ptr(vector<vector<char> >* array);
int main()
{
...
cin >> row;
cin >> col;
vector<vector<char> > Array(row, vector<char>(col));
...
fill (Array); // or fill_with_ptr(&Array);
}
void fill(vector<vector<char> >& array)
{
... // access elements as array[i][j]
}
void fill_with_ptr(vector<vector<char> >* array)
{
... // access elements as (*array)[i][j]
}