Nov 2, 2017 at 1:02am Nov 2, 2017 at 1:02am UTC
I am trying to read from a file that contains sentences and output them two letters at a time.
1 2 3 4 5 6 7
IE:
>hello world
>how are toy
Output:
he ll ow or ld
ho wa re to y
Here is what I have.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
string input;
getline(cin, input);
for (int i = 0; i < input.lenght(); i++)
{
cout << input[i] << input[i+1] << " " ;
}
return 0;
}
and this is what it currently outputs for lets say for example, "hello world"
1 2 3
output:
h he el ll lo ow eo or rl ld d
How do I fix it? will I have to somehow use vectors (I am awful at those so I am dreading it)
Last edited on Nov 2, 2017 at 1:05am Nov 2, 2017 at 1:05am UTC
Nov 2, 2017 at 3:24am Nov 2, 2017 at 3:24am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <string>
int main()
{
std::string input;
getline(std::cin, input);
for (std::string::size_type i = 0; i < input.size(); i+=2)
{
std::cout << input[i] << input[i+1] << " " ;
}
}
Last edited on Nov 2, 2017 at 3:25am Nov 2, 2017 at 3:25am UTC