Thread: A little lost and confused with my pointers :/

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    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
    28,413
    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.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    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, 03: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, 03:19 PM