String Output Problem

This is a discussion on String Output Problem within the C++ Programming forums, part of the General Programming Boards category; How come the following c++ code generate nonsense character output? #include <iostream.h> char* mmString(int mm) { char mmList[12][20] = { ...

  1. #1
    Yin
    Guest

    Question String Output Problem

    How come the following c++ code generate nonsense character output?

    #include <iostream.h>

    char* mmString(int mm) {
    char mmList[12][20] =
    { "January ", "February ", "March ", "April ", "May ", "June ", "July ", "August ", "September ", "October ", "November ", "December " };
    return(mmList[mm-1]);
    }

    int main() {
    int month;
    for (month=1; month<13; month++) {
    cout << mmString(month) << endl;
    }
    return 0;
    }

    The actual out put is 12 lines of nonsense character rather than the name of 12 months.

    Why? Is it because I haven't initialize the string properly? If that is really the case, I cannot figure the rationale behind.

  2. #2
    Yin
    Guest

    Arrow

    Sorry...
    Problem solved. It is because char mmList[12][20] should be declared outside the function.

  3. #3
    Yin
    Guest

    Question

    #include <iostream.h>

    class getmonth {
    public:
    char mmList[12][20] =
    { "January ", "February ", "March ", "April ", "May ", "June ", "July ", "August ", "September ", "October ", "November ", "December " };
    public:
    char* mmString(int mm) const {
    return(mmList[mm-1]);
    }
    };

    int main() {
    int month;
    for (month=1; month<13; month++) {
    cout << getmonth.mmString(month) << endl;
    }
    return 0;
    }

    This program generates error. I don't the mmList to be initialize at public part. How should I improve the program?

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    Code:
    #include <iostream.h> 
    
    const char* mmString(int mm) 
    { 
    static char mmList[12][20] = { "January ", "February ", "March ", "April ", "May ", "June ", "July ", "August ", "September ", "October ", "November ", "December " }; 
    return(mmList[mm-1]); 
    } 
    
    int main() 
    { 
    int month; 
    for (month=1; month<13; month++) 
    { 
    cout << mmString(month) << endl; 
    } 
    return 0; 
    }
    Making the array static would be the easiest way.
    You cannot initialize class member this way. You would have to strcpy all values in the constructor.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. string problem
    By INeedSleep in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2007, 10:52 PM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. string pattern search problem
    By goron350 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 07:50 AM

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