C++ program help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 4
Reputation: syphon_hawk is an unknown quantity at this point 
Solved Threads: 0
syphon_hawk syphon_hawk is offline Offline
Newbie Poster

C++ program help

 
0
  #1
Nov 30th, 2008
I need help with errors I am getting with my program that is suppose to take in lines of text and out put the number of letters to the user. I have a lot these errors
expected primary-expression before ' ' token
Attached Files
File Type: cpp letterCount2.cpp (2.5 KB, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ program help

 
0
  #2
Nov 30th, 2008
  1. void conversion (int size, char lines [Max_Lines][Max_Size], char alphabet[27] = "abcdefghijklmnopqrstuvwxyz", int alphacount [27] = 0);
  2. void displayTable (int alphacount [27] = 0, char alphabet[27] = "abcdefghijklmnopqrstuvwxyz");
Remove the { and } around the default parameters. That will cause the int array to be NULL pointers if the calling function doesn't supply the array. For example
  1. int foo(int ary[4] = 0)
  2. {
  3. if( ary != NULL)
  4. {
  5. for(int i = 0; i < 4; i++)
  6. {
  7. cout << ary[i] << "\n";
  8. }
  9. }
  10. cout << "All done\n";
  11.  
  12. return 1;
  13. }
  14.  
  15. int main()
  16. {
  17. foo();
  18. }
Last edited by Ancient Dragon; Nov 30th, 2008 at 7:10 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: C++ program help

 
0
  #3
Nov 30th, 2008
Check your variable typing (i.e. Max_Lines instead of Max_Line in line 66 but I also read countAlpha instead of alphacount elsewhere).

Also, you should pass lines as argument to the function displayTable.
You have an out-of-boundaries access for your lines array - be careful how you play with size <= Max_Lines and how you declared lines itself char lines [Max_Lines][Max_Size]; . Notice that lines is also accessed in displayTable function at line 92 if(!isalpha(lines[n][z])) . Ask yourself: what value does n has?

EDIT: Imho it's better to post your code with the help of code tags instead of providing it as an attachment for programs of this size - but this is just a suggestion
Last edited by mrboolf; Nov 30th, 2008 at 7:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 4
Reputation: syphon_hawk is an unknown quantity at this point 
Solved Threads: 0
syphon_hawk syphon_hawk is offline Offline
Newbie Poster

Re: C++ program help

 
0
  #4
Nov 30th, 2008
First I would like to say thanks to everyone's help I have cut down my errors tremendously. Second I am really struggling with this class and I am now at the point to where I'm just trying to pass and leave with the little bit of knowledge I've gained. That being said some of the advice given is beyond me, and though I'm sure you covered these error codes already here is what I'm getting specifically.

letterCount2.cpp: In function `void conversion(int, char (*)[80], char*, int*)':
letterCount2.cpp:72: error: default argument given for parameter 3 of `void conversion(int, char (*)[80], char*, int*)'
letterCount2.cpp:32: error: after previous specification in `void conversion(int, char (*)[80], char*, int*)'
letterCount2.cpp:72: error: default argument given for parameter 4 of `void conversion(int, char (*)[80], char*, int*)'
letterCount2.cpp:32: error: after previous specification in `void conversion(int, char (*)[80], char*, int*)'
letterCount2.cpp: In function `void displayTable(char (*)[80], int*, char*)':
letterCount2.cpp:91: error: default argument given for parameter 2 of `void displayTable(char (*)[80], int*, char*)'
letterCount2.cpp:33: error: after previous specification in `void displayTable(char (*)[80], int*, char*)'
letterCount2.cpp:91: error: default argument given for parameter 3 of `void displayTable(char (*)[80], int*, char*)'
letterCount2.cpp:33: error: after previous specification in `void displayTable(char (*)[80], int*, char*)'
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: C++ program help

 
0
  #5
Nov 30th, 2008
You should remove default values for default arguments from your functions definitions.

For example this code
  1. #include <iostream>
  2.  
  3. int myfunction(int one, int two = 2);
  4.  
  5. int myfunction(int one, int two = 2) {
  6. return one + two;
  7. }
  8.  
  9. int main(void) {
  10. std::cout << myfunction(1) << std::endl;
  11. std::cout << myfunction(1, 3) << std::endl;
  12. return EXIT_SUCCESS;
  13. }

Would result in the following compile errors:
  1. test.cpp: In function ‘int myfunction(int, int)’:
  2. test.cpp:5: error: default argument given for parameter 2 of ‘int myfunction(int, int)
  3. test.cpp:3: error: after previous specification in ‘int myfunction(int, int)

while this code (without " = 2 " default value in function definition)
  1. #include <iostream>
  2.  
  3. int myfunction(int one, int two = 2);
  4.  
  5. int myfunction(int one, int two) {
  6. return one + two;
  7. }
  8.  
  9. int main(void) {
  10. std::cout << myfunction(1) << std::endl;
  11. std::cout << myfunction(1, 3) << std::endl;
  12. return EXIT_SUCCESS;
  13. }

will compile fine
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 4
Reputation: syphon_hawk is an unknown quantity at this point 
Solved Threads: 0
syphon_hawk syphon_hawk is offline Offline
Newbie Poster

Re: C++ program help

 
0
  #6
Nov 30th, 2008
Hey thanks I got it to compile and run, one obstacle done now to the next, lol. For some reason its not counting the letters correctly in the lines of text i type? I believe that would have something to do with the conversion function or maybe not reading in all the lines of text. Any help would be appreciated.

void readLine (char letter[][Max_Size], int Max_Lines, int& size)
{  
  cout << "How many lines of text are you entering? ";
  cin >> size; 
  cout << "Type in "<< size <<" lines of text, pressing enter after each line. \n";
  if (size <= Max_Lines)
  {
    for (int i = 0; i <= size; i++)
    {
      cin.getline (letter[i], Max_Size);
      //cout << letter[i];
    }
  }
  else
   {
   cout << "You have entered too many lines of text, the limit is " << Max_Lines << ".";
   }
  cout << endl;
}

void conversion (int size, char lines [Max_Lines][Max_Size], char alphabet[27], int alphacount [27])
{
for(i=0;i<26;i++)
{
  for(n=0;n<size;n++)
  {
    for(z=0;lines[n][z]!= '\0'; z++)
    {
      lines[n][z]= tolower(lines[n][z]);
      
      if (lines[n][z]== alphabet[i])
      {
        alphacount[i]++;
      }
    }
  }
}
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 4
Reputation: syphon_hawk is an unknown quantity at this point 
Solved Threads: 0
syphon_hawk syphon_hawk is offline Offline
Newbie Poster

Re: C++ program help

 
0
  #7
Dec 1st, 2008
Thanks everyone I got it working correctly

PS: I should have come here for help earlier in my semester
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC