My simple program is broken! D:

This is a discussion on My simple program is broken! D: within the C++ Programming forums, part of the General Programming Boards category; Hey all, I am trying to fix this so a string entered like "I like cake" will be displayed on ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    Question My simple program is broken! D:

    Hey all, I am trying to fix this so a string entered like "I like cake" will be displayed on separate lines such as:
    I

    L
    I
    K
    E

    C
    A
    K
    E

    I can't seem to get it to work. If I enter a space in the string then the output becomes gibberish, also I have to keep setting the defined limit of the string with the max variable....which is not good.

    Can someone tell me what I am missing in this piece of code?

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main()
    {
        string input = "";
        int i = 0;
        int max = 10; //make loop not go on forever
    
    
        cout << "Enter in a string to separate!" << endl;
        cin >> input;
    
    
        for(i=0; i<=max; i++) //Take each char element in the array and display it on it's own line
        {
            cout << input[i] << endl;
        }
    
    
        cout << "End...";
        return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Use "getline(cin, input);" instead.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Nice that solved the space problem, now what about not having to set a predetermined limit on the amount of characters the user can enter?

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by logitechz View Post
    Nice that solved the space problem, now what about not having to set a predetermined limit on the amount of characters the user can enter?
    Characters? .... well.. as standard I/O is line buffered, your only way would be to take all the characters and ignore those outside the limit.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Quote Originally Posted by logitechz View Post
    Nice that solved the space problem, now what about not having to set a predetermined limit on the amount of characters the user can enter?
    Well, std::string can hold as long a string as the available memory they say. So all you have to do is take its lenght with "input.lenght()", or use iterators, which is a little more complex.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Alright thx for all the help, looks like I need to get further along before I can do such things.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    std::string has a member size() that gets its length.
    Or you could iterators:

    Code:
    for (auto it = str.begin(); it != str.end(); ++it)
        std::cout << *it;
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 12:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 09:28 AM
  5. How is a C++ Program broken down?
    By Fool in forum C++ Programming
    Replies: 14
    Last Post: 09-29-2001, 09:27 PM

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