Thread: Simple program: no output.

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    14

    Simple program: no output.

    Hi all,

    I was wondering if someone here could help me. I don't understand why 'phrase_1' doesn't print. I hope you can see that this program's intentions are to reverse whats stored in the string 'phrase' and store the result in 'phrase_1'.

    Thank in advance:

    Code:
    #include <iostream>
    #include <string>
    #include <stdio.h>
    using namespace std;
    
    
    int main()
    {
        char phrase[11];
        char phrase_1[11] = "";
    
    
        cin.getline(phrase, 11, '\n');
        cout << "The phrase you entered was: " << phrase << endl;
    
    
        for (int i = 0; i <= strlen(phrase); ++i)
        {
            phrase_1[i] = phrase[strlen(phrase) - 1- i];
        }
        
        cout << "Phrase_1 is: " << phrase_1 << endl;
    
    
        return 0;
    
    
    }
    Last edited by hewlettuser; 02-24-2017 at 08:55 AM.

  2. #2
    Registered User
    Join Date
    Feb 2017
    Posts
    14
    Update: I've managed to reverse it (In the original I didn't include the -1 in the phrase[strlen(phrase) - 1- i] part to account for the null terminator) However I still get a bogus character at the end can someone help with getting rid of that?

    Thanks.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    You aren't properly zero-terminating the C-style string.
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main() {
        char phrase[100], phrase2[100];
    
        cin.getline(phrase, sizeof phrase);
        cout << "The phrase you entered was: " << phrase << '\n';
    
        size_t len = strlen(phrase);
        for (size_t i = 0; i < len; ++i)
            phrase2[i] = phrase[len - 1 - i];
    
        phrase2[len] = '\0';  // zero-terminate the string
    
        cout << "Phrase2 is: " << phrase2 << '\n';
     
        return 0;
    }
    Of course, if you really want to write a C++ program then you should use C++ strings.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string phrase, phrase2;
    
        getline(cin, phrase);
        cout << "The phrase you entered was: " << phrase << '\n';
    
        for (size_t i = 0; i < phrase.size(); ++i)
            phrase2 += phrase[phrase.size() - 1 - i];
    
        cout << "Phrase2 is: " << phrase2 << '\n';
     
        return 0;
    }
    Last edited by algorism; 02-24-2017 at 10:04 AM.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    You can also copy strings with direct assignment or some of the standard STL functions as well.

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    14
    Hello, just wanted to thank you all kindly for your answers. algorism thanks for showing me other ways to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple c program-please explain output.
    By hitesh.incept in forum C Programming
    Replies: 6
    Last Post: 09-13-2012, 12:04 PM
  2. Very simple program with unexpected output
    By Draylath in forum C++ Programming
    Replies: 5
    Last Post: 07-04-2012, 10:38 AM
  3. Output Problem, Very simple program
    By MrAlt in forum C Programming
    Replies: 6
    Last Post: 01-15-2012, 08:34 PM
  4. getting wrong output from simple program
    By d387420489 in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 06:21 PM
  5. Weird output simple program.
    By omnificient in forum C++ Programming
    Replies: 7
    Last Post: 11-03-2005, 01:53 PM

Tags for this Thread