Newbee

HI guys! I need a lil help. Im new at C++ Programming and Im stuck at creating a simple program that will have this output:
if i enetered any word say alpha, it will be dislpayed as:
a
l
p
h
a

Thanks!
Please post your questions in one place only; http://www.cplusplus.com/forum/general/242282/
Sorry bro.
Hello Fortem,

Repeater explained in the other forum as to what you need to do.

Without any code no one has any idea of what exactly you need.

Write some code and post it even if it is wrong or does not work. It is some place to start from.

As Repeater said loop over characters in word:. I think a for loop would be best. This may be of some use http://www.cplusplus.com/doc/tutorial/control/

Your input method will depend on what you want to input, either a single word or something with spaces in it, this will determine the method used for input.

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

int main()
{
    // std::string: https://cal-linux.com/tutorials/strings.html
    // a string can hold all the characters in any word
    // (we always use std::string to represent a sequence of characters)
    std::string word ;

    std::cout << "enter a word: " ;
    std::cin >> word ; // reads a single word entered by the user into the string

    // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
    // a range based loop is the simplest, and the least error prone looping construct
    // so we favour this kind of loop over others, whenever it can do the job for us
    for( char c : word ) // for each character c in the word
        std::cout << c << '\n' ; // print it out, followed by a new line
}
Topic archived. No new replies allowed.