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.
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.
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.
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.