GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Nov-2009, 12:07
Saylor Saylor is offline
New Member
 
Join Date: Nov 2009
Posts: 2
Saylor is on a distinguished road
Question

Expected primary-expression before '{' token


I had to create a class that would accept a user input number between 0 and 9999 and translate it into "zero" to "nine thousand nine hundred ninety - nine". I had to use one int number and use static arrays to hold the strings. The problems arise when I try to initiallize the arrays. I originally had them initialized in the class when they were declared, but I saw a post saying that it would not work in C++, so I tried initializing them in the constructor, but I'm getting two errors for each array line: "expected-primary expression before '{' token" and "expected ';' before '{' token". Any help? Thanks in advance.

CPP / C++ / C Code:
#include <iostream>
using namespace std;

class Numbers
{
 public:
       int number; //to hold the number passed into the constructor from another program
 private:
          const static char lessThan10[]; // holds strings "one" through "nine"
          const static char tens[10]; //holds strings for "ten" through "nineteen"
          const static char timesTens[8]; //holds strings for "twenty" through "ninety"
          const static char hundred[1]; //holds string "hundred"
          const static char thousand[1]; //holds string "thousand"
          int numSplit[4];
 
 Numbers(int num) //Constructor
 {
  number = num; //passes number given to constructor from main program to the numbers variable
  
  /*************************************************************************************************************/
  //I tried to initiallize the arrays outside of the class....*suggested on some other sites
  lessThan10[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
  tens[10] = "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
  timesTens[8] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
  hundred[1] = {"hundred"};
  thousand[1] = {"thousand"};//this section presents two errors: "expected primary-expression before '{' token" and 
  //"expected ';' before '{' token"
  /**************************************************************************************************************/
  translator(number); //passes number into the translator
 }
 
 int Numbers::translator(int num)
 {
  
     
  numSplit[0] = (num/1000); //gets the thousand digit
  numSplit[1] = ((num%1000)/100); //gets the hundred digit
  numSplit[2] = (((num%1000)%100)/10); //gets the ten digit
  numSplit[3] = (((num%1000)%100)%10); //gets the one digit
  
  for (int i = 1; i < 10; i++) //loops to print the thousand digit. if digit is 0, prints nothing
  {
   if (numSplit[0] == i)
   {
    cout << lessThan10[i] << " " << thousand[0] << " ";                
   }
  }
  
  for (int i = 1; i < 10; i++) //loops to print hundred digit. if digit is 0, prints nothing
  {
   if (numSplit[1] == i)
   {
    cout << lessThan10[i] << " " << hundred[0] << " ";        
   }    
  }
  
  if (numSplit[2] == 1) //if ten digit is a one, loops to print correct string 
  {
   for (int i = 0; i < 10; i++)
   {
    if (numSplit[3] == i)
    {
     cout << tens[i];                
    }    
   }
  }
  
  else if (numSplit[2] == 0) //if ten digit is 0, loops to print the one digit
  {    
   for (int i = 1; i < 10; i++)
   {
    cout << lessThan10[i];     
   }
   if (numSplit[3] == 0 && numSplit[2] == 0 && numSplit[1] == 0 && numSplit[0] == 0) //if the number = 0, prints "zero"
   {
    cout << lessThan10[0];                
   }
  }
  
  else
  {
   for (int i = 2; i < 10; i++) //loops to print the ten digit if it is 1 < ten digit < 10
   {
    if (numSplit[2] == i)
    {
     cout << timesTens[i-2] << " ";                
    }    
   }
   
   for (int i = 1; i < 10; i++) //loops to print the one digit
   {
    if (numSplit[3] == i)
    {
     cout << "- " << lessThan10[i];                
    }    
   }
  }
 }
}

  #2  
Old 06-Nov-2009, 13:34
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Expected primary-expression before '{' token


They cannot be initialized like that. Try:

CPP / C++ / C Code:
// Numbers.h

class Numbers
{
 private:
          const static char lessThan10[]; // holds strings "one" through "nine"
};

CPP / C++ / C Code:
// Numbers.cpp

#include "Numbers.h"

const char Numbers::lessThan10[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

  #3  
Old 10-Nov-2009, 12:35
Saylor Saylor is offline
New Member
 
Join Date: Nov 2009
Posts: 2
Saylor is on a distinguished road

Re: Expected primary-expression before '{' token


CPP / C++ / C Code:
class Numbers
{
 public:
       int number; //to hold the number passed into the constructor from another program
 private:
          const static char lessThan10[]; // holds strings "one" through "nine"
          const static char tens[]; //holds strings for "ten" through "nineteen"
          const static char timesTens[]; //holds strings for "twenty" through "ninety"
          const static char hundred[]; //holds string "hundred"
          const static char thousand[]; //holds string "thousand"
          int numSplit[4];
          
  Numbers(int);
  int translator(int);
 }; 

and
CPP / C++ / C Code:
 
const char Numbers::lessThan10[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
  const char Numbers::tens[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
  const char Numbers::timesTens[] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
  const char Numbers::hundred[] = {"hundred"};
  const char Numbers::thousand[] = {"thousand"}; 

in the constructor, but now I get these errors:

Numbers.h: No such file or directory.
28 expected primary-expression before ']' token
28 expected primary-expression before '{' token
28 expected `;' before '{' token
29 redeclaration of `const char Numbers::tens[]'

Any help?
  #4  
Old 10-Nov-2009, 13:42
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Expected primary-expression before '{' token


For starters, this stuff...
CPP / C++ / C Code:
const char Numbers::lessThan10[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
  const char Numbers::tens[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
  const char Numbers::timesTens[] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
  const char Numbers::hundred[] = {"hundred"};
  const char Numbers::thousand[] = {"thousand"}; 
...does not go in the constructor. It is defined as you would a class method.

Your .cpp file needs to have #include "Numbers.h at the top. From the code you posted, it does not. Go ahead and post the ENTIRE code so that we can really see what you did. Use separate tags for the various files like I did.

As far as the error message goes, it looks like it is saying that it can't find the file Numbers.h.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Expected primary-expression before '.' token dinh0wjr C Programming Language 2 30-Mar-2009 13:13
main.cpp:15: error: expected primary-expression before ',' token DingbatCA C++ Forum 8 29-Oct-2007 13:52
Error: expected unqualified-id before ‘{’ token ftmthy412 C++ Forum 5 12-Sep-2007 17:49
Converting PHP to C and I need a little help Allenport C Programming Language 4 14-Aug-2006 14:38
error: expected primary-expression before '.' token Honourable Mist C++ Forum 11 18-Feb-2006 13:15

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:36.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.