Hi, I have question regarding a program I'm doing. We are non-sensense sentences in the form of The <noun> <verb>, the <noun>. The program uses a Word Class and two objects one Noun object, one Verb Object. The program starts off with the user entering a list of 10 nouns and 10 verbs stored in a Noun & Verb object arrays.Then the program asks for an integer which will output the number of sentences. The rand() function is used to select the 2 nouns and 1 verb that the user entered. I've done all this but the once catch I'm having trouble with is the second noun in the sentence has to be tested against the first noun because the need to be different if they are the same we have to call rand() again to get a different noun. My problem is that my test to see if the nouns are alike isnt working, I'm getting a compiler error with a redefinition of the variable I use for rand(). If anyone can tell me how I can test the two nouns and if they are alike call rand() again to get a different one. I tried using different variables for rand() in the test but the program bombs after entering the 10 nouns and verbs saying the variable is unreferenced. Thanks you all for you assistance. My code is below:
#include <iostream>
#include <cstring>
#include <cmath>
usingnamespace std;
class Word {
public:
Word () {for (int i = 0; i< 20; x[i++]= '\0');}
void putX (char* s) {strcpy (x, s);}
char* getX () {return x;}
private:
char x[20];
};
int main () {
int a;
int b;
int nr;
char inp [20];
Word Noun [10], Verb [10];
for (int j = 0; j< 10; ++j) {
cout << "Please enter a noun ";
for (int i = 0; i< 20; inp [i++]= '\0');
cin.getline (inp, 19);
// test here for cin.eof() and terminate if true
Noun[j].putX(inp);
//cout << " " << Noun[j].getX() << endl; // fenceposting
}
for (int j = 0; j< 10; ++j) {
cout << "Please enter a verb ";
for (int i = 0; i< 20; inp [i++]= '\0');
// test here for cin.eof() and terminate if true
cin.getline (inp, 19);
Verb[j].putX(inp);
//cout << " " << Verb[j].getX() << endl; // fenceposting
}
cout << "Please enter a positive integer ";
cin >> nr;
if (cin.eof()) exit (1);
if (!cin.good()) { cout<< "Invalid input"; exit (2);}
if (nr < 1) { cout<< "Invalid input"; exit (3);}
srand(0);
for (int k = 0; k < nr; k++) {
int m = rand ();
int n = m % 10;
Noun[n].getX();
for ( int k = 0; k < nr; k++){ // gets a verb
int m = rand();
int n = m % 10;
Verb[n].getX();
for (int k = 0; k < nr; k++) {
int m = rand();
int n = m % 10;
Noun[n].getX();
if(Noun[n].getX() == Noun[n].getX())//test if nouns are the same.
int m = rand();//if the nouns are the same get another noun
int n = m % 10;
Noun[n].getX();
cout << " The " << Noun[n].getX()<< Verb[n].getX() << //prints out sentence
" the " << Noun[n].getX() << endl;
}
}
}
return 0;
}
On line 42, use srand(time(0)) and inclde <ctime>
On line 58 you have two errors:
1. Compare C strings using strcmp() (for strcmp(a,b), 0 if a==b, a negative number if a<b, a non-zero, positive number if a>b).
2. You are comparing 'Noun[n].getX()' to 'Noun[n].getX()', which will always be equal.
Hi thank you for the help, I completely forgot about strcmp to compare strings. I've now used that but I'm getting a new compiler error which is
.\Nonsense3.cpp(64) : error C2664: 'strcmp' : cannot convert parameter 1 from 'int' to 'const char *'
How can I use strcmp to compare the two nouns if they both just elements in the character array? Is that why why I'm getting the compiler error. Also the other part I'm still unsure of is, after I compare them and if they are equal, how do I tell it to generate a new noun, then have that noun display out , in the part where I cout my sentence. Thanks. My code with the new addition is below.
#include <iostream>
#include <cstring>
#include <cmath>
#include <ctime>
usingnamespace std;
class Word {
public:
Word () {for (int i = 0; i< 20; x[i++]= '\0');}
void putX (char* s) {strcpy (x, s);}
char* getX () {return x;}
private:
char x[20];
};
int main () {
int nr;
int b;
char inp[20];
Word Noun [10], Verb [10];
for (int j = 0; j< 10; ++j) {
cout << "Please enter a noun ";
for (int i = 0; i< 20; inp [i++]= '\0');
cin.getline (inp, 19);
// test here for cin.eof() and terminate if true
Noun[j].putX(inp);
//cout << " " << Noun[j].getX() << endl; // fenceposting
}
for (int j = 0; j< 10; ++j) {
cout << "Please enter a verb ";
for (int i = 0; i< 20; inp [i++]= '\0');
// test here for cin.eof() and terminate if true
cin.getline (inp, 19);
Verb[j].putX(inp);
//cout << " " << Verb[j].getX() << endl; // fenceposting
}
cout << "Please enter a positive integer ";
cin >> nr;
if (cin.eof()) exit (1);
if (!cin.good()) { cout<< "Invalid input"; exit (2);}
if (nr < 1) { cout<< "Invalid input"; exit (3);}
srand(time(0));
for (int k = 0; k < nr; k++) {
int a = rand ();
int b = a % 10;
Noun[b].getX();//gets a noun
for ( int k = 0; k < nr; k++){
int m = rand();
int n = m % 10;
Verb[n].getX(); //gets a verb
for (int k = 0; k < nr; k++) {
int c = rand();
int d = c % 10;
Noun[d].getX(); //gets 2nd noun
strcmp(b,d);//test if nouns are the same.
if(strcmp<= 0)//test nouns to see if the are the same
int a = rand();
int b = m % 10;
Noun[b].getX();
//How do I code here if the two nouns are equal?
//As you see above after I used strcmp I then did another rand() for
// a new noun but is that correct, and also how do I get it to //display the //new noun sense I used the same variables for the array element in rand?
cout << " The " << Noun[b].getX()<< Verb[n].getX() << //prints out sentence
" the " << Noun[b].getX() << endl;
}
}
}
return 0;
}
Line 63: Noun[d].getX() has no effect. You're returning a pointer but you're not assigning it to anything.
Line 64: strcmp(b,d). 'b' was declared as int b. 'd' was declared as int d. strcmp() only takes pointers to C strings, such as Noun[/*Damn BBS code was parsing this as a tag*/b].getX(). That would be a pointer to a C string.
Be careful when using rand(). It returns a random 32-bit integer. Which mean you could be getting any number between 0 and 2^32-1. This will most likely result in a segmentation fault. To sort of trim rand(), use rand()%10, for this case. That will ensure that the random value is never more than 9.
int main () {
int num;
char arr[20];
char* n1;//poniter variable to hold 1st noun
char* n2;//pointer variable to hold 2nd noun
char* v;//pointer variable to hold verb
//Prints out the rest
srand(time(0));
for (int k = 0; k < num; k++) {
int a = rand ();
int b = a % 10;
Noun[b].getX();//gets a noun
n1 = Noun[b].getX();//assign 1st random noun to char pointer
for ( int k = 0; k < num; k++){
int m = rand();
int n = m % 10;
Verb[n].getX(); //gets verb
v = Verb[n].getX();//assign random verb to char pointer
}
for (int k = 0; k < num; k++) {
int c = rand();
int d = c % 10;
Noun[d].getX(); //gets 2nd noun
n2 = Noun[d].getX();//assign 2nd random noun to char pointer
if(!strcmp(n1,n2)){//tests if 2 nouns are the same
int e = rand();
int f = e % 10;
Noun[e].getX();
n2 = Noun[e].getX();
}
else
{
n2 = Noun[d].getX();