Trouble with C++ structs

buzzard34

Gawd
Joined
Feb 14, 2004
Messages
619
What do I have to fix here? I'm trying to compile this propgram using structs and having little success. The compiler lists errors tracing back to the "word charm" function, where it says "conversion from 'wordType'to non-scalar type 'wordtype' requested" I don't really know what that means, I have a terrible time trying to figure out what the hell the compiler tells me. If anyone could help me out that'd be great.
Code:
   #include <iostream>
   #include <cctype>
  #include <iomanip>
  using namespace std;
  
  const int MAX_WORD_LENGTH = 20;
  
  struct wordType
      {
             char word[MAX_WORD_LENGTH];
             int len;
      };
      
  //Function Prototypes
  
  
  
  void Description();
  //Description of program 
  
  
  void InitWord(wordType w);
  //Cells 0..len -1 are initialized to space character
  
  void GetWord (wordType w);
  //User is prompted to enter a word with less than MAX_WORD_LENGTH
  //letters. The user is then prompted to enter the word.
  
  void ShowWord (wordType w);
  //Precondition: w is an array which contains a word of
  //len letters stored in cells 0..len-1.
  //Postcondition: the word is displayed on the screen.
  // w and len are unchanged.
  
  void ShowReversedWord (wordType w);
  //Precondition: w is an array which contains a word of
  //len letters stored in cells 0..len-1.
  //Postcondition: the word is displayed backwards on the screen.
  //w and len are unchanged
  
  bool WordsAreSame (wordType w1, wordType w2);
  //Precondition: w1 is an array which contains a word of
  //len1 letters stored in cells 0..len1-1.
  // w2 is an array which contains a word of
  //len2 letters stored in cells 0..len2-1.
  //Postcondition: Returns true if the words are the same, false otherwise
  //Case is ignored in that Hello and hello will be regarded as equal.
  // w1, w2, len1, and len2 are unchanged.
  //Uses <cctype>
  
  bool IsPalindrome (wordType w);
  //Precondition: w is an array which contains a word of
  //len letters stored in cells 0..len-1.
  //Postcondition: Returns true if the word is a palindrome, false otherwise
  //Case is ignored in that AbcBa will be regarded as a palindrome.
  // w, len are unchanged.
  //Uses <cctype>
  
  int NumberVowels (wordType w);
  //Precondition: w is an array which contains a word of
  //len letters stored in cells 0..len-1.
  //Postcondition: Returns the number of letters that are in the set
  //{A,a,E,e,I,i,O,o,U,u}
  //w and len are unchanged.
  
  void DisplayWordCharm (wordType w);
  //Precondition: w is an array which contains a word of
  //len letters stored in cells 0..len-1.
  //Postcondition: The word is displayed on the screen as an ancient 
  //word charm as described on the assignment.
  //Uses <iomanip> and <cctype>
  
  
  
  //The main program
  
  int main()
  {
      wordType word1[MAX_WORD_LENGTH];
      int word1Length;
      wordType word2[MAX_WORD_LENGTH];
      int word2Length = MAX_WORD_LENGTH;
   
     
      
      Description();
      
      cout << "First word:\n";
      InitWord(word1,MAX_WORD_LENGTH);
      GetWord(word1,word1Length);
      cout << "\n\nSecond word\n";
      InitWord(word2,MAX_WORD_LENGTH);
      GetWord(word2,word2Length);
   
   
   //Test GetWord and ShowWord
      cout <<"Your first word(between the bars): ";
      cout << '|';
      ShowWord(word1,word1Length);
      cout << '|'<< endl;
      cout << "Your second word:";
      cout << '|';
      ShowWord(word2,word2Length);
      cout << '|'<< endl;
      
   //Test ShowReversedWord
      cout << "Your first word spelled backwards is ";
      ShowReversedWord(word1,word1Length);
      cout << endl;
      
   //Test WordsAreSame  
      if (WordsAreSame(word1,word1Length,word2,word2Length))
          cout << "Your words were the same.\n";
      else
          cout << "Your words are different.\n";
  
  
  //Test NumberVowels
      cout << "The number of vowels in your second word is ";
      cout << NumberVowels(word2,word2Length);
      cout << ".\n";
  	
   //Test IsPalindrome 
      if (IsPalindrome(word1,word1Length))
          cout << "Your first word is a palindrome.\n";
      else
          cout << "Your first word is not a palindrome.\n";
  	
  //Test WordCharm
      cout << "Your second word displayed as a word charm:\n\n";
      DisplayWordCharm(word2,word2Length);
  	
  
      return 0;
  }
  
  //Function Implementations
  
  
  //Description of program
  
  void Description()
  {
      cout << "Greetings to the fun with words program.\n";
      cout << "You will be prompted to enter two words with ";
      cout << MAX_WORD_LENGTH << " \nletters or less. The program";
      cout << " will then test your \nfunction implementations.\n";
  }
  
  void InitWord(wordType w)
  //Cells 0..len -1 are initialized to space character
  {
      for (int i = 0; i < w.len; i++)
          w.word[i] = ' ';
  }
  
  void GetWord (wordType w)
  //User is prompted to enter a word with less than MAX_WORD_LENGTH
  //letters. The user is then prompted to enter the word.
  {
      cout << "How many letters are in your word? ";
      cin >> w.len;
      cout << "Enter your word:";
      for (int i = 0; i < w.len; i++)
      {
          cin >> w.word[i];
      }
      
  }
  
  void ShowWord (wordType w)
  //Precondition: w is an array which contains a word of
  //len letters stored in cells 0..len-1.
  //Postcondition: the word is displayed on the screen.
  // w and len are unchanged.
  {
      for(int i = 0; i < w.len; i++)
          cout << w.word[i];
  }
  
  void ShowReversedWord (wordType w)
  //Precondition: w is an array which contains a word of
  //len letters stored in cells 0..len-1.
  //Postcondition: the word is displayed backwards on the screen.
  //w and len are unchanged
  {
      for (int i = w.len - 1; i >=0; i--)
          cout << w.word[i];
      
  }
  
  bool WordsAreSame (wordType w1, wordType w2)
  //Precondition: w1 is an array which contains a word of
  //len1 letters stored in cells 0..len1-1.
  // w2 is an array which contains a word of
  //len2 letters stored in cells 0..len2-1.
  //Postcondition: Returns true if the words are the same, false otherwise
  //Case is ignored in that Hello and hello will be regarded as equal.
  // w1, w2, len1, and len2 are unchanged.
  //Uses <cctype>
  {
      if (w1.len != w2.len)
          return false;
      else
      {
          for (int i = 0; i < w1.len; i++)
  	{
  	    if (toupper(w1.word[i]) != toupper(w2.word[i]))
  	        return false;
  	}
  	return true;
      }
  return true;
  }
  
  bool IsPalindrome (wordType w)
  //Precondition: w is an array which contains a word of
  //len letters stored in cells 0..len-1.
  //Postcondition: Returns true if the word is a palindrome, false otherwise
  //Case is ignored in that AbcBa will be regarded as a palindrome.
  // w, len are unchanged.
  //Uses <cctype>
  {
      for (int i = 0; i < w.len/2; i++)
      {
          if (toupper(w.word[i]) != toupper(w.word[w.len-1-i]))
  	    return false;
      }
  	return true;
  }
  
  int NumberVowels (wordType w)
  //Precondition: w is an array which contains a word of
  //len letters stored in cells 0..len-1.
  //Postcondition: Returns the number of letters that are in the set
  //{A,a,E,e,I,i,O,o,U,u}
  //w and len are unchanged.
  {
      int count = 0;
      char ch;
      for (int i = 0; i < w.len ; i++)
      {
          ch = w.word[i];
  	if (ch == 'A' || ch == 'a' || ch == 'E' || ch == 'e' || ch == 'I' || ch
  	== 'i' || ch == 'O' ||ch == 'o' || ch == 'U' || ch == 'u' ) 
  	    count++;
      }
      
      return count;
  }
  
  void DisplayWordCharm (wordType w)
  //Precondition: w is an array which contains a word of
  //len letters stored in cells 0..len-1.
  //Postcondition: The word is displayed on the screen as an ancient 
  //word charm as described on the assignment.
  //Uses <iomanip> and <cctype>
  {
      for ( int row = 0; row < w.len; row++)
      {
          //First indent the needed amount
  	cout << setw((80 - 2*w.len)/2)<< ' ';
  	cout << setw(w.len-row) << ' ';
          //Now print the row of letters    
          for (int col = 0; col <= row; col++)
  	{
  	    cout << setw(2) << char(toupper(w.len[row]));
  	}
  	cout << endl;
      }
  }
  [/indent][font=verdana, arial, helvetica][size=2]
[/size][/font]​
 
You're trying to pass an array of wordType structs to functions that take a single wordType by value.

Code:
wordType word1[MAX_WORD_LENGTH];

creates an array of MAX_WORD_LENGTH structs, not a single wordType like your function signatures specify.
 
bassman said:
You're trying to pass an array of wordType structs to functions that take a single wordType by value.

Code:
wordType word1[MAX_WORD_LENGTH];

creates an array of MAX_WORD_LENGTH structs, not a single wordType like your function signatures specify.
ok so to change it, would i just make it "word"? then would it compile correctly?
 
It may fix one problem, but your code will still not compile.

This isn't to be rude, but there are some pretty basic problems with the program. I'd find someone to help you in person. I think you'll get your code working faster and you'll learn more.
 
bassman said:
It may fix one problem, but your code will still not compile.

This isn't to be rude, but there are some pretty basic problems with the program. I'd find someone to help you in person. I think you'll get your code working faster and you'll learn more.
i understand
 
Just one thing to add, you declared wordType as:

struct wordType
{
...
}

Declaring it this way, whenever you declare variables with a structure you need to preface the declared with the word "struct". Like:

struct wordType varname;

A better way would be to declare wordType as a new type, that is:

typedef struct wordType
{
...
}

Now when you declare variables of wordType, you can declare them as:

wordType varname;
 
Seated said:
Just one thing to add, you declared wordType as:

struct wordType
{
...
}

Declaring it this way, whenever you declare variables with a structure you need to preface the declared with the word "struct". Like:

struct wordType varname;

A better way would be to declare wordType as a new type, that is:

typedef struct wordType
{
...
}

Now when you declare variables of wordType, you can declare them as:

wordType varname;
havent covered that in class yet.
 
Seated said:
Just one thing to add, you declared wordType as:

struct wordType
{
...
}

Declaring it this way, whenever you declare variables with a structure you need to preface the declared with the word "struct". Like:

struct wordType varname;

A better way would be to declare wordType as a new type, that is:

typedef struct wordType
{
...
}

Now when you declare variables of wordType, you can declare them as:

wordType varname;

Not in C++, thats C. In C++ Structs are the same thing as Classes, except defaulting to Public.
To demonstrate

Code:
#include <iostream>
using namespace std;

struct MyType
{
   int MyInt;
};

int main ()
{
   MyType MyVariable;
   MyVariable.MyInt = 12;
   cout << "MyVariable.MyInt equals " << MyVariable.MyInt << endl;
}

output
Code:
./structest
MyVariable.MyInt equals 12
 
Back
Top