Pointers Help

This is a discussion on Pointers Help within the C++ Programming forums, part of the General Programming Boards category; Hi, so my problem is in the reverseCase function I think. I am getting fatal errors whenever I try to ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    Pointers Help

    Hi, so my problem is in the reverseCase function I think. I am getting fatal errors whenever I try to compile so I think I've set something wrong there. Any help would be appreciated.

    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    const int STRINGSIZE = 100;
    
    void getString(char string[]);
    void reverseCase(char string[]);
    int numGraph(string);
    
    
    int main()
    {
       char string[STRINGSIZE];
       int count;
    
       getString(string);
       cout << "Entered string => " << string << "\n";
    
       count = numGraph(string);
       cout << "Number of graphic characters in string = " << count << "\n";
    
       reverseCase(string);
       cout << "Reverse case   => " << string << "\n";
    
       return 0;
    }
    
    void reverseCase(char string[])
    {
       /****************************************************************\
          Reverse the case of all alphabetic characters in the string.
          That is, all upper case characters become lower case and 
          all lower case become upper case.
       \****************************************************************/
    
       int j=0;
       char *c;
    
       while (string[j])
       {
          c = (&string[j]);
          if (isupper(*c)) *c = tolower(*c);
          else *c = toupper(*c);
          j++;
       }
    }
    
    int numGraph(char string[])
    {
       /***************************************************************\
          Calculate the number of printable graphic characters in the
          string.
       \***************************************************************/
    
       int i, count;
    
       for (i=0; string[i] != '\0'; i++)
       {
          if (isgraph(string[i])) count++;
       }
       return count; 
    }
    
    void getString(char string[])
    {
       /************************************************************\
          
          Use getline function to get entire line of text up to
    
          maximum of STRINGSIZE-1 chars in length
       
       \************************************************************/
       
       cout << "Please enter a string to process\n";
    
       cin.getline(string, STRINGSIZE);
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    posting errors would help to get quick answer...

    int numGraph(char string[]) - in this function you are using uninitialized variable number
    and in the prototype you have
    int numGraph(string);

    which due to your using directive is interpreted as

    int numGraph(std::string some_string);

    and this function has no body
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    Umm your function prototype... it uses a std::string or a character array? I'm assuming you are using a character array so you need to put that. Secondly you should indent your code a little more. The if else statement in the body of the reversestring function is... hard to read.

    It seems to work fine after you fix your function prototype.
    -- Will you show me how to c++?

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Ugh stupid.

    Thanks guys.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    Quote Originally Posted by mramazing View Post
    The if else statement in the body of the reversestring function is... hard to read.
    I don't really think so.
    For this code, I might actually suggest that you try to get used to reading such code since it's in perfectly well levels of indentation that you might come across this kind of code elsewhere.
    Learning to read others code helps
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Elysia View Post
    I don't really think so.
    For this code, I might actually suggest that you try to get used to reading such code since it's in perfectly well levels of indentation that you might come across this kind of code elsewhere.
    Learning to read others code helps
    I agree that the indentation of that if-else block is hard to read, but instead of choking it down, I'd run it through something like SourceFormatX and fix it.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    I don't really think it's difficult at all. What's so difficult?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Not "Chinese arithmetic" difficult, just that my eyes are trained to see the code indented on a separate line.

    It's also inconsistent, perhaps, to see a single line for loop with curly braces and indentation in another function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

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