read more than one line

Write your question here.
I am trying to find a way to make the program read more than one line..
1
2
3
4
5
6
7
8
9
10
11
12
13
  #include<iostream>
using namespace std;
int main()
{
	   int c;
	   c = cin.get();
	   while ( ! cin.eof())
	   {
	   	   cout.put(c);
	   	   c= cin.get();
	   	   
	   }
}
How will you know if it worked if there is no output?
right now it reads the input from the user then after hitting return it prints out the same thing. i'm trying to find a way to make it read everything up until the user enters a blank line.
Try this out:

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

int main()
{
    std::string input;

    std::getline(std::cin, input);

    while (input != "")
    {
        std::cout << input << std::endl;
        std::getline(std::cin, input);
    }

    return 0;
}
This works for normal input/output but the reason I'm using int c is because I'm planning on shifting the letters forward by 5.
What are you trying to create, a Caesar cipher?
Yes. I'm using cout.put() to print the result.
The code I gave you does not need to be changed too much:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

int main()
{
    std::string input;

    std::getline(std::cin, input);

    while (input != "")
    {
        for (int i = 0; i < input.length(); i++)
        {
            if (input[i] >= 'a' && input[i] <= 'z')
            {
                input[i] = (input[i] - 'a' + 5) % ('z' - 'a' + 1) + 'a';
            }
            if (input[i] >= 'A' && input[i] <= 'Z')
            {
                input[i] = (input[i] - 'A' + 5) % ('Z' - 'A' + 1) + 'A';
            }
        }

        std::cout << input << std::endl;
        std::getline(std::cin, input);
    }

    return 0;
}

This program will only shift the letters a - z and A - Z.
Last edited on
That works! Thank you.
I just made a change in lines 16 and 20.
Topic archived. No new replies allowed.