Now, before this question gets marked for duplicate. I have already gone through most of the questions and their relative answers of C++. These are the links that I have tried and none of them work for me. It maybe because they are using an older version of C++, and I have the latest version of C++. Here are the links that I have tried:
Detecting ENTER key in C++
https://www.sololearn.com/Discuss/1863352/how-can-i-check-that-user-press-enter-key-in-c
http://www.cplusplus.com/forum/beginner/2624/
https://www.dreamincode.net/forums/topic/398680-detect-enter-key/
Now, with the duplicates out of the way. I am making an expression calculator. So, for example if the user input is: 2+2*6*9/9, then the output should be 14.
The code where I suspect that the problem lies is in:
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::string;
using std::vector;
void clear();
void error(string message);
int main() {
cout << "Enter an expression: ";
double l_Value = 0, r_Value = 0, result = 0, count = 0, previous_number;
char op;
while (cin >> l_Value) { // 1+2*3+6-4/2+3
if (!cin) {
error("Invalid operand entered!");
}
else {
bool is_Error = 0; // false
vector<double> numbers;
numbers.push_back(l_Value);
previous_number = l_Value;
while (cin >> op) {
if (op == '\0') {
break;
}
cin >> r_Value;
switch (op)
{
case '+':
numbers.push_back(r_Value);
previous_number = r_Value;
break;
case '-':
numbers.push_back((-1 * r_Value));
previous_number = (-1 * r_Value);
break;
case '*':
numbers.pop_back(); // take out the number
r_Value *= previous_number;
numbers.push_back(r_Value);
previous_number = r_Value;
break;
case '/':
if (r_Value == 0) {
error("Sorry, division by zero has occured. Please re-evaluate your expression!\n");
is_Error = 1; // true
break;
}
else {
numbers.pop_back(); // take out the number
previous_number /= r_Value;
numbers.push_back(previous_number);
break;
}
}
}
if (!is_Error) {
for (int i = 0; i < numbers.size(); i++) {
result += numbers[i];
}
cout << result << '\n';
}
numbers.clear();
result = 0;
l_Value = 0;
r_Value = 0;
}
cout << "Enter an expression: ";
}
clear();
return 0;
}
None of the links above seemed to work for me.
When I press the Enter key, it expects me to give another input, and that is not supposed to happen. So when I used cin.get() == 'n' or cin.get() == (int)'\n', it expects for another input. But, when I have an 'x' at the end of the expression, it works perfectly fine. So, I need the "cin" operator to help me detect an Enter character at the end of the expression and then terminate the program.
Here, is a sample run of a program with 'x':
[![running as x-terminator][1]][1]
[1]: https://i.stack.imgur.com/ORPQa.png
When I try the above solution such as "cin.get() == '\n':
Then, I thought that maybe it is reading the null character and so, I tried if (op == '\0'):
For the enter key and null character I had to press Ctrl+Z to terminate the program. Please help!
As, mentioned by user #idclev, I already have a string program that works, but I am trying to avoid using string to calculate any expressions! So, if I could detect an enter key pressed using a character datatype that would be great!
I avoided strings to avoid parsing through the text
That argument is moot. What you can read from cin you can also read from a std::string, no difference whatsoever. You just need to add one step:
#include <iostream>
#include <string>
#include <sstream>
int main( ){
std::string x;
std::cin >> x;
if (x == "") {
std::cout << "user pressed enter (and nothing else)";
} else {
double y;
std::stringstream ss{x};
ss >> y;
std::cout << y;
}
}
This will read one std::string. If user only hit enter then the string will be empty. If the user entered something the else branch will be taken and you can extract the number from the string in the same way you did extract the number from cin (via using a std::stringstream).
If you have more than one number in the input you need to use getline to read the string, because cin will (by default) only read till the next whitespace.
Again...
If I used a string, I would have a tough time in extracting single-digit and two-digit or n-number of digits in a string. The double data type does that for me
You can read single-digit or any number of digits from a stringstream in exactly the same way as you read them from cin.
I already made a program with string in it. I was trying to avoid string to see how much faster would it be without string.
It won't be any faster. Constructing the string and the stringstream is maybe in the order of microseconds. A user entering input is in the order of seconds, maybe milliseconds when they are typing very fast.
Your approach cannot work because hitting enter is not considered as a character. Trying to read a character when there is none in the stream will fail. It will not set the character to \n or \r.
On the outer loop, you are trying to read a double, but you keep pressing enter. There is no input to evaluate, so it keeps trying to read a double. You can get out of it by ending the input stream with ^Z, or you can give it any actual content to read, and it will try to make it into a double (which is what your code explicitly told it to wait for).
Basically, when you press enter, it's ignoring it because
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
Extracts as many characters as possible from the stream and inserts them into the output sequence controlled by the stream buffer object pointed by sb (if any), until either the input sequence is exhausted or the function fails to insert into the object pointed by sb.
Try experimenting with this to see what is happening.
#include <iostream>
#include <vector>
#include <string>
int main() {
double x;
std::cin >> x;
std::cout << "read this value: " << x << std::endl;
// this is what while or if will look at
bool success = !std::cin.fail();
if (success)
std::cout << "success" << std::endl;
else
std::cout << "failure, loop will exit" << std::endl;
return 0;
}
What you should want (in my opinion) is a function that takes the expression as a string, and returns the result, so you can write unit tests, and make sure the function works. You can use this function with any expression that you can put in a string. It doesn't HAVE to be typed in by a user.
If you want the user to type in the experession, it's a lot easier to just use getline() then pass the string to your function. The big problem with using cin on each variable and character is that the user has no idea which datetype is expected right then. Granted, it's not hard to guess with an expression, but you wrote it and debugged it and still didn't know which cin you were failing to get the right datatype to. (this is normal, btw -- been there, which is why I getline and parse separately)
Related
I am learning C++, and I am doing some exercises in the book I am using. One of them asks to write a program that asks a user how many numbers they want to add up. Then prompt for the numbers the user wants to add or to enter '|' once finished. The numbers are then pushed into a vector. Part of the program asks to check if the size of the vector is equal to the original number of input items and that is where I keep getting an error.
cout << "Please enter the numbers and | once you are done: ";
while(true)
{
for(int num; cin >> num; )
{
if(num == '|')
{
break;
}
ints.push_back(num);
}
if(ints.size() != n)
{
cout << "There are more or less numbers in the vector than originally specified\n"
<< "Vector will be cleared; please re-enter the values: ";
ints.clear();
continue;
}
else
{
break;
}
}
The problem is that if the number of input is off, the message goes into an infinite loop and I am not sure how to fix it.
EDIT: n is the variable that holds in the number of values user wanted to enter.
Thanks!
num is an integer and cin >> num won't extract | symbol. Comparison num == '|' may not work as expected because num could have the numeric value of | ascii symbol even when user did not input any | symbol. You should properly handle end marker reading:
// loop will break when user enters `|` because it is not an integer
// setting failbit of cin
for(int num; cin >> num;)
{
ints.push_back(num);
}
cin.clear(); // reset failbit making cin able to read again
// check the end marker entered by user
{
string end_marker;
cin >> end_marker;
if("|" != end_marker)
{
// use of endl will flush the stream ensuring that
// printed text won't stuck in the buffer
cout << "Please use | as end marker" << endl;
continue;
}
}
Here is how I implemented it. I am worried about the logic in your while loop. I had been taught to avoid while(true) whenever possible. You know the logic behind how your code should work. With more practice you'll start to recognize the conditions you need to use. I am sure there are better ways to do it. But this is the way I tried it.
But to answer your question, the main reason it is failing is because integers cannot compare themselves with characters.
if(num == '|')
That does not work since num is an integer and not a character.
Normally I would implement this in a class and since global variables are not highly looked upon I created my own namespace. You'll have to finish the rest of the logic yourself however:
#include <iostream>
#include <vector>
#include <string>
namespace global
{
std::vector<std::string> strings;
std::vector<int> ints;
std::string a = " ";
int num = 0;
}
void doWork()
{
std::cout << "Please enter the number of integers you would like to add up: ";
std::cin >> global::num;
std::cout << "Please enter the numbers and | once you are done: ";
while (global::a != "|")
{
std::cin >> global::a;
global::strings.push_back(global::a);
}
global::strings.pop_back();
for(auto &e : global::strings)
{
global::ints.push_back(std::stoi(e));
}
}
int main()
{
doWork();
if(global::ints.size() != global::num)
{
std::cout << "Size of vector does not match the size specified. Clearing vector" << std::endl;
global::ints.clear();
global::strings.clear();
global::num = 0;
global::a = " ";
doWork();
}
}
I made a vector of char's and converted those into integers so that way you could add them up. The while loop should be checking for | rather than always running true. It then will check the size of the vector in the end, clear it if it does not match, and ask you to do it again. This is the best way that I could think of doing it.
EDIT: as VTT pointed out, char can only do one character at a time. I have converted it into a string in order to handle the conversion.
EDIT 2: reset the values of global::num and global::a to their default at the end of the failure in order to prevent crashing.
I'm new to programming, and I'm trying to learn C++ by myself, and I'm following "Programming principles and practice using C++".
I was trying to do an exercise that, after various other steps, asked me to
" ...change the body of the loop so that it reads just one double each time around. Define two variables to keep track of which is the smallest and which is the largest value you have seen so far. Each time through the loop write out the
value entered. If it’s the smallest so far, write the smallest so far after the number. If it is the largest so far, write the largest so far after the number".
I wrote the following code so far:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open() { cout<<"\nType a character to exit: "; char ch; cin>>ch; }
int main()
{
double val1 = 0, smallest = 0, largest = 0;
int flag = 0;
while (cin >>val1) {
if (val1=='|')
break;
else
cout <<val1 <<'\n';
if (flag==0) {
smallest = val1;
cout <<smallest <<" it's the smallest value so far.\n";
}
if (val1<smallest) {
smallest = val1;
cout <<smallest <<" it's the smallest value so far.\n"; }
else if (val1>largest) {
largest = val1;
cout <<largest <<" it's the largest value so far.\n"; }
++flag;
}
keep_window_open();
return 0;
}
My problem is that when I input a character, e.g. 'c', the program ends, although the program should end, hypothetically, only when I enter '|', and I get:
c
Type a character to exit:
Process returned 0 (0x0) execution time : ...
Press any key to continue.
and "keep_window_open()" doesn't wait for a character to be entered. I just can't understand what happens, and why. Does anyone have a clue?
Well, I think the problem is in the way you defined your loop expression.
Neither cin nor its operator ">>" return a true / false value that you can work with. They return an iStream object which could be a dubious translation to true or false via an automated casting that happens behind the curtains. They will however return null when the input cannot be set inside your variable like in the case of trying to put 'c' into a double, and null translates into false.
What I suggest for you is to create a simple while(true) loop and when you get the "|" character from the user(as a string) you break the loop. until then the loop continues. then inside the loop parse your value and work on it according to your logic(minimum / maximum)
Problem
while (cin >>val1) reads input into a double. If you input something that cannot be read as a double like "|" then this will fail and cause your loop to exit.
You then try to read more input while cin is in an error state.
Solution
Call cin.clear() to first clear any error flags and then cin.ignore(1000, '\n') to throw away anything left hanging in the input stream.
inline void keep_window_open()
{
cout << "\nType a character to exit: ";
char ch;
cin.clear();
cin.ignore(1000, '\n');
cin >> ch;
}
Note
if (val1=='|')
break;
This code will never be true because if you try to input the pipe character your while loop will fall through before ever getting to it.
I have this simple code that duplicate a string by using either a reference or a pointer (I'm a beginner), and I would like to make it determine whether the input is integral or not because if it wasn't, it keeps looping for some reason. I've tried using cin.clear() but it doesn't seem to work..
Here is the code:
#include <iostream>
#include <string>
#include "test.h"
using namespace std;
int main() {
while (true) {
cout << "Type something: ";
string hi;
cin >> hi;
cout << "Choose a method to change it:\n1. By reference.\n2. By using pointers.\n";
start:
int i;
cin >> i;
if (cin.fail())
cin.clear();
switch (i) {
case 1:
changestringref(hi);
cout << hi << '\n';
break;
case 2:
changestringptr(&hi);
cout << hi << '\n';
break;
default:
cout << "Choose by typing either 1 or 2: ";
goto start;
break;
}
}
}
Whenever I type a string (at the second input), it keeps looping this sentence: "Choose by typing either 1 or 2: Choose by typing either 1 or 2: Choose by typing either 1 or 2: "etc.
How can I solve this problem? And why isn't cin.clear working? isn't it supposed to clear the input from the last time cin was used or am I wrong? O.o
Thanks in advance :D
And why isn't cin.clear working? isn't it supposed to clear the input from the last time cin was used or am I wrong?
Not quite. It's supposed to Set[s] a new value for the stream's internal error state flags.. clear() without argument sets goodbit and clears the fail() state, but leaves any unprocessed input untouched.
Let's assume you entered 'x'. Since 'x' is not a number, cin >> i sets failbit, and does not consume input (actually, it pushes the already consumed x back into the input buffer, but that's another story).
This is behavior documented somewhat obscurely in num_get::get(). The returned iterator (i.e. the stream) points right before the first character that could not be parsed.
So next time, after the goto start; you read x again. Same input, same result, hence the endless loop.
I have a question about input validation in C++. Here is my code I am having trouble with:
#include <iostream>
using namespace std;
int main()
{
int in;
while(true)
{
if(cin >> in)
{
if(in < 0 || in > 2)
{
cout << "Invalid input. Number needs to be 0, 1, or 2.";
cin.clear();
while(cin.get() != '\n');
cin.ignore();
}
else
{
cout << "Output: " << in;
break;
}
}
else
{
cout << "Invalid input. Please enter a number.";
cin.clear();
while(cin.get() != '\n');
cin.ignore();
}
}
}
This code works fine unless two invalid entries are made in a row with the second input of the form '12hfhd'. Then it accepts this as input and I can't figure out why. I have searched on SO and have found a bunch of questions regarding input validation but can't seem to find any about their code accepting certain input.
The main problem is that, when requesting an int from std::cin using the >> operator, a sequence of numeric chars from the beginning of the input will be converted. Examples:
2 will convert to 2
75$ will convert to 75
12asdfgh will convert to 12
hello,world will convert to 0, because the first char is already not a number
The best thing to do is to use some char operations:
int getNumber() {
char input;
std::cin >> input;
if(input > '2' || input < '0') { // yes, chars act like ASCII numbers
// handle this problem
return -1; //error
} else {
return int(input - '0'); // input's code - '0''s code = number
}
}
I'd use the following approach when dealing with user input:
string l;
if(!getline(cin, l))
/* handle error that no input could be read at all */
try
{
int const res = boost::lexical_cast<int>(l);
/* numerically validate input here */
return res;
}
catch(exception const&)
{
/* handle parsing error here */
}
In words, read a line and then parse and validate it using Boost's lexical_cast() function template. Note that the first error, that getline() fails, happens if someone reads input from a file (e.g. via shell redirect), but this can also be achieved with certain keypresses, depending on the shell. This state can't be recovered from, so prompting for a different answer will result in an endless loop.
If you look at the documentation of the >> extraction operator, for example here:
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
you will notice the following quote:
(1) arithmetic types
Extracts and parses characters sequentially from the stream to interpret them as the representation of a value of the proper type,
which is stored as the value of val.
This essentially means that the program will try to treat all the data you pass as formatted in the format specified by the rvalue, in your case int. Simpler: in your case it will try to make ints of what you're passing into the stream and make the data as 'integerish' as it gets.
The following program should read some integer values from input and store them into an array until the user presses "Esc" key, but i cannot figure out how to do that. I have tried to use it's ASCII value as a condition but it doesn't work, or I'm doing something wrong.Could somebody please help me? Here is the code :
void main(){
int i=0, a[MAX];
cout <<"Please enter several integer numbers and press Esc when you want to finish" << endl;
while ((i != char(27)) && (i < MAX)) {
cin >> a[i];
i++;
}
There are several problems here. The first, and most important,
is that most systems will do line oriented input, which means
that they will pick up a lot of control characters (including,
probably, ESC) and do something with them internally, rather
than returning them to you; it might not even be possible to
ever see an ESC in the input.
For the rest, there are several problems with your code. The
first is that you're testing against the index into the array
(where you'll put the next character), not against anything
you've read. The second is that you're inputting int, so you
won't see any individual characters; just the results of the
conversion of your input. (In fact, if an ESC character does
get through, you'll end up with std::cin in a error state.)
The third is that you never test whether the input has
succeeded.
A better solution would be to adopt some other convention for
the input: say one number per line, with an empty line signaling
the end. In that case, you could write something like:
std::vector<int> a;
std::string line;
std::cout << "..." << std::endl;
while ( std::getline( std::cin, line ) && !line.empty() ) {
std::istringstream parse( line );
int value;
parse >> value >> std::ws;
if ( !parse ) {
std::cout << "That wasn't an integer, try again" << std::endl;
} else if ( parse.get() != EOF ) {
std::cout << "Extra garbage at end of line, ignoring it" << std::endl;
} else {
a.push_back( value );
}
}
(You'll note that I've used std::vector, since you don't know
up front how many inputs you'll get.)
If you really want to use ESC, you'll probably need a third
party library, along the lines of curses, to do raw input,
reading one character at a time, and collecting the digits into
a string, for conversion using std::istringstream.
Do not do this use James method its much better and will run on evrything but just for the sake of answering a question you can do this. This will not work on all platforms It works on visual c++ compiler.
# include <iostream>
# include <conio.h>
# include <vector>
using namespace std;
int main()
{
char key;
vector<int> a;
int temp = 0;
while ((key = getch()) != 27)
{
cin >> temp;
a.push_back(temp);
}
for (int i = 0 ; i < a.size() ; i++)
{
cout << a.at(i) << endl;
}
printf("Exit");
cout << endl;
system("Pause");
return 0;
}
Edit: Programs clearer now. Note this program should not be used it is very platform and compiler dependant.