Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: displaying array and reversing an array in c++

  1. #1
    Join Date
    Aug 2012
    Beans
    12

    displaying array and reversing an array in c++

    Ive tried searching this but not luck, I have done this before and I am having a complete brain fart on how to do this. I am just tinkering around with programs getting back into the swing of things. When I try to display the array with a for loop I get the whole array. If I try to display it backwards it skips the loop. The array is a set size of 50 characters. I just cannot figure out what I missing. Thanks for taking a look.

    Here's the code.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    
    {
    
    void Hello();
    void Age();
    void Sentence();
    
    const int SIZE = 50;
    int Word = 0;
    int Length = 0;
    
    
    
    
    Hello();
    Age();
    Sentence();
    
    cout << endl;
    cout << myString;
    cout << endl << endl;
    
    cout << "Please hit enter to exit program.";
    cin.get();
    
    return 0;
    
    }
    
    
    /***************Hello**************/
    
    void Hello()
    
    {
    
        cout<< "Hello!" << endl;
    }
    
    /***********************************/
    
    
    
    /*************Age*****************/
    
    void Age()
    {
    
    int age;
    int i;
    
    cout << "How old are you? " << endl;
    
    cin >> age;
    
    cout << "You are " << age << " years old." << endl;
    
    for(i = 1; i <= age; ++i)
    {cout << i << " ";}
    
    cout << endl;
    
    cin.clear();
    
    }
    
    /************************************/    
    
    
    
    /***********Sentence*****************/
    
    void Sentence()
    {
    
    
    const int SIZE = 50;
    char sentence;
    char N[SIZE];
    int i = 0;
    
    cout << "Write a sentence up to 50 characters long. --> ";
    
    cin >> sentence;
    
    cout << endl;
    
    while(sentence != '\n')                
    {
        N[i]=(sentence);    
        cout << N[i];
        ++i;                            
        cin.get(sentence);                    
    }
    
    cout << endl << endl;
    
    cout << "Your sentence has " << i << " characters." << endl << endl;
    
    for (i = 0;N[i] != '\0'; i++)
    {
        cout << N[i];
    }
    
    cout << "\n\nThis is the sentence backwards.\n";
    
    for(i = --i; N[i] == 0; i--)
    {
        cout << N[i];
    }
    
    }

  2. #2
    Join Date
    Sep 2005
    Beans
    1,603
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: displaying array and reversing an array in c++

    The only way I could get it to work was the change the last for loop to this:

    for(i = i - 2; i >= 0; i--)
    {
    cout << N[i];
    }

    The reason it doesn't work is because a for is like a while, while i >= 0 then continue the for loop. If it says while N[i] == 0 it won't work cause N[i] never equals 0.
    Lenovo 330 - Win 10 64-bit - Ryzen 5 2500U 8GB 120GB SSD
    Dell XP 8700 - Windows 10 64-bit - i7 4790 8GB 1GB 250GB SSD 1TB HDD
    HP x360 - Elementary OS Juno - Pentium 4x1.10 4GB 500GB HDD

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: displaying array and reversing an array in c++

    Quote Originally Posted by Crash525 View Post
    I am just tinkering around
    There's nothing wrong with tinkering, however if you are trying to get back into the swing of programming, I would suggest that you learn the proper way to gather/store user input. Using an array to store a string is unwarranted in C++. Use the STL string instead.

    For example:
    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string input;
    
        std::cout << "Enter a long sentence: ";
        std::getline(std::cin, input);
    
        std::string reverseInput(input.rbegin(), input.rend());
    
        std::cout << "\n\n"
                  << "Your sentence has " << input.size() << " characters in it.\n"
                  << "This is what you entered: " << input << "\n"
                  << "Here's that sentence in reverse order: " << reverseInput
                  << std::endl;
    }

  4. #4
    Join Date
    Aug 2012
    Beans
    12

    Re: displaying array and reversing an array in c++

    I was trying to stay away from having an actual number size given in the for loop such as '2' and so on. I wanted it to be universal, but it works.

  5. #5
    Join Date
    Aug 2012
    Beans
    12

    Re: displaying array and reversing an array in c++

    I have never messed with much of the string library. All of my previous course work had been to manipulate it on your own. Its ridiculous how much I forgot after not having wrote any program for a year or so, I feel like a beginner all over again. Thanks for the info, I will have to do some research in finding how to display it backwards and research on the library's functions. I there a way to have a loop check for a negative in a number?

  6. #6
    Join Date
    Feb 2009
    Beans
    1,469

    Re: displaying array and reversing an array in c++

    Quote Originally Posted by Crash525 View Post
    All of my previous course work had been to manipulate it on your own.
    This is a pervasive mentality in academia and quite harmful to practical programming IMO. It's better to learn a language the way it's used in real life. (One reason I really liked Learning Perl -- it takes real-world problems and shows you how to handle them realistically.)

  7. #7
    Join Date
    Aug 2012
    Beans
    12

    Re: displaying array and reversing an array in c++

    I finally got it figured out but I had to change the for loop to a while loop to do so. I could not find a universal character to terminate the for loop the second time. With my school work I realize now that its is much harder than it has to be. Since most everything has a specific function written I might as well use it and not waist my time trying to figure it out on my own.


    To note their is a word count function in here and its not being used so please ignore it.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    
    {
    
    //int WordCount();
    void Hello();
    void Age();
    void Sentence();
    
    
    
    string myString = "You did it!!!!";
    const int SIZE = 50;
    int Word = 0;
    int Length = 0;
    
    
    
    
    Hello();
    Age();
    Sentence();
    
    cout << endl;
    cout << myString;
    cout << endl << endl;
    
    return 0;
    
    }
    
    
    /************WordCount************/
    
    int WordCount()
    {
    
    int N[50];
    int i = 0;
    int Word = 0;
        
        for(i=0;N[i] != '\n';++i)
        {
            ++Word;
        }
    
    ++Word;
    
    return Word;
    }
    /**********************************/
    
    
    
    
    /***************Hello**************/
    
    void Hello()
    
    {
    
        cout<< "Hello World" << endl << endl;
    }
    
    /***********************************/
    
    
    
    /*************Age*****************/
    
    void Age()
    {
    
    int age;
    int i;
    
    cout << "How old are you? ";
    
    cin >> age;
    
    cout << "\nYou are " << age << " years old." << endl << endl;
    
    for(i = 1; i <= age; ++i)
    {cout << i << " ";}
    
    cout << endl << endl;
    
    cin.clear();
    
    }
    
    /************************************/    
    
    
    
    /***********Sentence*****************/
    
    void Sentence()
    {
    
    
    const int SIZE = 50;
    char sentence;
    char N[SIZE];
    int i = 0;
    
    cout << "Write a sentence up to 50 characters long. --> ";
    
    cin >> sentence;
    
    cout << endl;
    
    while(sentence != '\n')                
    {
        N[i]=(sentence);    
        //cout << N[i];
        ++i;                            
        cin.get(sentence);                    
    }
    
    cout << endl;
    
    cout << "Your sentence has " << i << " characters." << endl;
    
    cout << "\n\nThis is the sentence backwards.\n\n";
    
    while(i != 0 )
    {--i;
        cout << N[i];
        
    }
    
    cout << endl;
    
    }

  8. #8
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: displaying array and reversing an array in c++

    Quote Originally Posted by Crash525 View Post
    I finally got it figured out...

    Code:
    #include <iostream>
    ...
    
    cout << "Write a sentence up to 50 characters long. --> ";
    
    cin >> sentence;
    
    cout << endl;
    
    while(sentence != '\n')                
    {
        N[i]=(sentence);    
        //cout << N[i];
        ++i;                            
        cin.get(sentence);                    
    }
    
    ...
    Oftentimes, asking a user to "do this, but not this" is begging for disaster.

    You should have inserted guards in your while-loop to ensure that the user does not enter more than 49 characters (the 50th character needs to be reserved for the terminating null-character).

    Also, ask yourself this... does N seem like a good variable name? How about renaming it to 'sentence', and in lieu of the existing variable with that name, perhaps change it to 'ch' or 'input'.

  9. #9
    Join Date
    Aug 2012
    Beans
    12

    Re: displaying array and reversing an array in c++

    I am starting to come back to my senses with writing programs. Its easy for me to remember what each variable name is for but that is not a good habit to pick up. Thanks for pointing that out because I did forget to add a check for to much input.

    Right now I am trying to pass an array to a function, but its not working out to well. I'm going to see if I can find the correct syntax for it before I ask for help.

    One question though, after the loop does the string stored in sentence get deleted?

    while(sentence != '\n') { N[i]=(sentence); //cout << N[i]; ++i; cin.get(sentence); }

  10. #10
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: displaying array and reversing an array in c++

    Quote Originally Posted by Crash525 View Post
    One question though, after the loop does the string stored in sentence get deleted?
    A variable, and its contents, will live throughout the scope in which it is declared. Thus if you declare the array 'N' in a function, that's the scope for which is is defined. It will not be visible outside of the function.

    If however you allocate a region of space on the heap, and maintain a pointer to that region, then it can be accessed outside of the function where it was allocated. The key is to ensure that a reference to that region is maintained at all times (otherwise you will have a memory leak).

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •