// ----------------------------------------------
// Name:     William Rennie
// Program: 
// Description: 
// ----------------------------------------------
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <string.h>
using namespace std;
 
// ----------- Function definitions -------------
void printTitle(int gamenumber){
     gamenumber++;
     cout<<"***Word guessing game round "<<gamenumber;}

bool checkWord(char word[],char guess[]){
     int i;
    int x=strcmp(word,guess);
    if (x==0){return true;}
    else return false;
}                      
     	

     int main()
{

       // -------- Constant declarations --------

       // -------- Variable declarations -------- 
char wordone[4];
char wordtwo[4];   
int i;
int x;    
bool wordcheck;
        //--------Main Program Body----------
    cout <<"********"<<endl<<endl;
for (i=0;i<4;i++){
cin>>wordone[i];}
cout<<"word 2: "<<endl;
for (i=0;i<4;i++){
cin>>wordtwo[i];}
wordcheck=checkWord(wordone,wordtwo);
cout<<checkWord(wordone,wordtwo);
if (wordcheck){cout<<"t";} 

else{cout<<"f";}

cout << endl << endl;
       system("PAUSE");     // Pause the output window for reading
       return 0;
       }

it always returns 0 even when i enter the same words and im not understanding why. Is it a problem with the function itself or just how im getting the elements of the array?
also note this is just part of a game guessing program so dont mind the printtitle function thanks!

Arguments for strcmp must be terminated with 0. Your wordone and wordtwo are not: they are arrays of 4 characters, and all four are filled up with input. The trailing garbage they have is different thus causing strcmp to report inequality.

Arguments for strcmp must be terminated with 0. Your wordone and wordtwo are not: they are arrays of 4 characters, and all four are filled up with input. The trailing garbage they have is different thus causing strcmp to report inequality.

i tried that and it still doesnt work am i still screwing something up? i tried it with both 0 and '\0'

for (i=0;i<4;i++){
cin>>wordone[i];}
wordone[5]=0;
cout<<"word 2: "<<endl;
for (i=0;i<4;i++){
cin>>wordtwo[i];}
wordtwo[5]=0;
wordcheck=checkWord(wordone,wordtwo);
cout<<checkWord(wordone,wordtwo);
if (wordcheck){cout<<"t";}

The only thing I may recommend now is to put a breakpoint at checkWord and see what data are passed there. I have a suspicion that wordtwo may contain characters you do not expect (such as a newline).

wordone[5]

does not exist. The final element in your char array is wordone[3].

If you want to be able to fit four letter and a terminating zero in your char array, you must make it at least 5 elements long, like this:

char wordone[5];

and then you can put a zero in the final element like this:

wordone[4] = 0;

Edited 5 Years Ago by Moschops: n/a

The only thing I may recommend now is to put a breakpoint at checkWord and see what data are passed there. I have a suspicion that wordtwo may contain characters you do not expect (such as a newline).

i did thanks! it seems wordone is adding a - on the end and i really do not understand why when everything is identical to wordtwo, is this my fault or is this some mess of a compiler issue?

is this my fault or is this some mess of a compiler issue?

You've misunderstood how to address elements of an array.

When you make an array of size X, the first element is numbered zero, and the final element is numbered X-1.

Edited 5 Years Ago by Moschops: n/a

wordone[5]

does not exist. The final element in your char array is wordone[3].

If you want to be able to fit four letter and a terminating zero in your char array, you must make it at least 5 elements long, like this:

char wordone[5];

and then you can put a zero in the final element like this:

wordone[4] = 0;

okay i got it! thanks!

Edited 5 Years Ago by beejay321: n/a

a null at the end for no apparent reason

It's because you have misunderstood the numbering system in an array.


Read my post again.

If you have make array of size 5

char wordone[5];

the last element is wordone[[B]4[/B]]

You see in your code where you do this?

wordone[5]='\0';

That is off the end of your arry. The last element in your array is wordone[[B]4[/B]]

Edited 5 Years Ago by Moschops: n/a

You've misunderstood how to address elements of an array.

When you make an array of size X, the first element is numbered zero, and the final element is numbered X-1.

another quick question though, if i didnt know how many letters were to be entered by the user and wanted to fill the rest with null space what would be the easiest way to do that?

Perhaps with the C++ std::string

#include <iostream>
#include <string.h>

int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::string;
 
    
cout <<"********"<<endl<<endl;

string input1;
string input2;
cout<<"One:" << endl;
cin>>input1;
cout<<"Two:" << endl;
cin>>input2;

if (input1==input2)
  {
  cout << "same";
  }
else
  {
  cout << "Different";
  }

return 0;
}

Sample output:


j@j-desktop:~/badCode$ ./a.out
********

One:
egg
Two:
egg
same

j@j-desktop:~/badCode$ ./a.out
********

One:
hfushuorehiore
Two:
hfhfjd!!75765565hignjtuidgy
Different

Edited 5 Years Ago by Moschops: n/a

This article has been dead for over six months. Start a new discussion instead.