problem with program that takes in lines of text

Hey guys, I'm trying to make a program that will take in a line of text and output it back, while using a period as a sentinel. I have it working and using the period as a sentinel, but I need to code it so when the period is entered it will output everything up until that period.So if I enter "hello." it would output only "hello". I know its probably something really stupid I'm missing, but I've been coding other programs for a few hours now and my brain is on the fritz!:(
Anyways here's what I got so far:

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
char user_input;

cout << "Enter some text. Enter a period to exit.\n";
do
{
user_input = getchar();
cout << user_input;
} while (user_input != '.');
cout << "\n";
}
You want a SINGLE period to indicate termination of program but when the single period is part of the string then the program should continue is it ?

1
2
3
4
5
6
7
8
9
10
11
//char user_input;
string user_input;

cout << "Enter some text. Enter a period to exit.\n";
do
{
//user_input = getchar();
getline(cin, user_input);
cout << user_input << "\n";
} while (user_input != ".");
cout << "\n";


Use C++ string and getline instead of getchar function to achieve what you want.
Last edited on
Yeah that's sort of what I'm trying to do. I tried running that code and the program still continued even when a period was entered. All in all what I'm trying to do is get the program to work so that whatever is typed it will echo it as long as a period isn't entered. So if I were to type "hello" it would echo back "hello" and so on, but once I type let's say "Hello, my name is Johnny." it would still echo back, but only up until the period so it would say "Hello, my name is Johnny." then the program terminates.
sorry, I meant to say it would still echo back "Hello, my name is Johnny" without the period.
sorry, I meant to say it would still echo back "Hello, my name is Johnny" without the period.


But what if user enter something with period in-between

e.g
User key in ".Hello, my name is K. F. Johnny."

Above have period in between or even in the first character. How should you expect the program behavoir to be like then ?
Yeah that would be a bug with the program and I can see what your saying, but I'm just following the examples from the book of what would be entered by the user and all of examples have the period at the end of line of text. So for this matter, let's just assume the user would only enter a period at the end of whatever it is their typing. Sorry for the confusion.
1
2
3
4
5
6
7
8
9
10
11
do
{

getline(cin, user_input);

if (user_input[user_input.length()-1]=='.')
  cout << user_input.substr(0,user_input.length()-1) << "\n";
else
  cout << user_input << "\n";

} while (user_input != ".");
Wow thanks that works...the only issue left though is when the period is entered it doesnt end the program. It echoes back the word without the period, but the program keeps going.
I found the answer for my problem finally. It's very similar to your code.
Here it is:
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string input;
    cout << "Enter some text.  Enter a period to exit.\n";
    for(int counter = 0;counter<100000;counter++)
    {
        getline(cin, input);
        if(input[input.length()-1] == '.')
        {
            for(int count = 0;count<input.length()-1;count++)
                {
                    cout << input[count];
                }
            cout << "\n";
            break;
        }
        else
        {
            cout << input;
            cout << "\n";
            input.clear();
        }
    }
}

Thanks for your help though, I appreciate it.
Topic archived. No new replies allowed.