A little lost and confused with my pointers :/

This is a discussion on A little lost and confused with my pointers :/ within the C++ Programming forums, part of the General Programming Boards category; ok in this program I'm trying to make the pointer variable totalW a function parameter that will return the number ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    10

    Question A little lost and confused with my pointers :/

    ok in this program I'm trying to make the pointer variable totalW a function parameter that will return the number of w's found in the string entered by the user but it doesn't seem to work ....I know it has to do something with the pointers but I'm at a loss as to how to fix it
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cstring>
    #include <cctype>
    
    using namespace std;
    const int SIZE = 1001;
    
    int numberOfW(int *totalW);
    
    int main()
    {
     int total = 0;
     
     numberOfW(&total);   
     
     cout << total << " yay did it work?"; 
     
     system("PAUSE");
     return 0;
    }
    
    int numberOfW(int *totalW){
        char letter[SIZE];
        cout << "Input a string of characters: " << endl;
        cin.getline(letter, 1000);
        for (int i = 0; i <= letter[i]; i++){
            if(isalpha (letter[i]) && letter[i] == 'w'){
            totalW++;
          
          }
           
            else 
            cout <<"X";
            
            }
            
        return *totalW;
        
        system("PAUSE");
    }
    output:

    Input a string of characters:
    wewe
    XX0 yay did it work?Press any key to continue . . .

    any suggestions:?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    if(isalpha (letter[i]) && letter[i] == 'w')
    If you want to count the letter w-s, what is the purpose of calling isalpha. If the second part is true, so must the be the first part.

    Code:
    totalW++;
    You probably didn't mean to increment the pointer (make it point to next integer in memory) but the value it is pointing at:

    Code:
    (*total)++;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Instead of this:
    Code:
    cin.getline(letter, 1000);
    for (int i = 0; i <= letter[i]; i++){
    you probably should write:
    Code:
    cin.getline(letter, SIZE);
    for (int i = 0; i < SIZE && letter[i]; i++){
    By the way, the isalpha() check is redundant.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    glo
    glo is offline
    Registered User
    Join Date
    May 2006
    Posts
    169
    Or even better, since you don't know what uninitialized array members may contain, try for looping like so:
    Code:
        int length=strlen(letter);
        for (int i = 0; i < length; i++) {

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by glo
    Or even better, since you don't know what uninitialized array members may contain, try for looping like so:
    That is not necessary, since the check for letter[i] as a non-null character would suffice. (I suppose that the check for i < SIZE is arguably unnecessary, but better safe than sorry, in my opinion.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    glo
    glo is offline
    Registered User
    Join Date
    May 2006
    Posts
    169
    Right, I overlooked that check.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    TY for the help now i changed it to
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cstring>
    #include <cctype>
    
    using namespace std;
    const int SIZE = 1001;
    
    int numberOfW(int *totalW);
    
    int main()
    {
     int total = 0;
     
     numberOfW(&total);   
     
     cout << total << " yay did it work?"; 
     
     system("PAUSE");
     return 0;
    }
    
    int numberOfW(int *totalW){
        char letter[SIZE];
        cout << "Input a string of characters: " << endl;
        cin.getline(letter, 1000);
        for (int i = 0; i <= letter[i]; i++){
            if(letter[i] == 'w')
            (*totalW)++;
       
            }
            
        return *totalW;
        
        system("PAUSE");
    }
    output :

    Input a string of characters:
    wewe
    2 yay did it work?Press any key to continue . . .

    and it works perfectly fine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. A little confused about this
    By manav in forum C++ Programming
    Replies: 20
    Last Post: 05-29-2008, 10:47 PM
  3. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  4. So LOST!!!
    By Drew Vance in forum C Programming
    Replies: 6
    Last Post: 04-25-2003, 05:37 PM
  5. API, LOST... help
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 03-13-2002, 02:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21