How to keep taking values from the user in C but stop when the user does not enter any value and presses enter? - c++

int k;
vector<int>v;
while ((scanf("%d", &k)) != EOF) {
if (v.size() > 20) break;
else {
if (k > 0 && k <= 120) v.push_back(k);
}
}
**The above snippet is taken from codechef blogs **

<rant>Never tag a question both C and C++ on SO again. It is reserved for questions about interoperations or very specific language-lawyer points, and should not be used for code that could be more or less used on both languages. In that latter case choose one in your first question and if you later need it ask a new question for the other language</rant>
Your problem is that for the scanf family questions spaces and newlines are just ignored when you use a %d conversion character. And scanf returns the number of elements that could be decoded and only stops on an end of file, read error or conversion error.
If you want to be able to detect an empty input, you will have to use fgets + sscanf from the C standard library, or getline + stringstream from the C++ one.

Related

Why does getline behave weirdly after 3 newlines?

I'll preface this by saying I'm relatively new to posting questions, as well as C++ in general, my title is a little lame as it doesn't really specifically address the problem I am dealing with, however I couldn't really think of another way to word it, so any suggestions on improving the title is appreciated.
I am working on a relatively simple function which is supposed to get a string using getline, and read the spaces and/or newlines in the string so that it can output how many words have been entered. After reaching the character 'q' it's basically supposed to stop reading in characters.
void ReadStdIn2() {
std::string userInput;
const char *inputArray = userInput.c_str();
int count = 0;
getline(std::cin, userInput, 'q');
for (int i = 0; i < strlen(inputArray); i++){
if ((inputArray[i] == ' ') || (inputArray[i] == '\n')){
count += 1;
}
}
std::cout << count << std::endl;
}
I want to be able to enter multiple words, followed by newlines, and have the function accurately display my number of words. I can't figure out why but for some reason after entering 3 newlines my count goes right back to 0.
For example, if I enter:
hello
jim
tim
q
the function works just fine, and returns 3 just like I expect it to. But if I enter
hello
jim
tim
bill
q
the count goes right to 0. I'm assuming this has something to do with my if statement but I'm really lost as to what is wrong, especially since it works fine up until the 3rd newline. Any help is appreciated
The behaviour of the program is undefined. Reading input into std::string potentially causes its capacity to increase. This causes pointers into the string to become invalid. Pointers such as inputArray. You then later attempt to read through the invalid pointer.
P.S. calculating the length of the string with std::strlen in every iteration of the loop is not a good idea. It is possible to get the size without calculation by using userInput.size().
To fix both issues, simply don't use inputArray. You don't need it:
for (int i = 0; i < userInput.size(); i++){
if ((userInput[i] == ' ') || (userInput[i] == '\n')){
...

Check scanf formating without conversions

Is there a way how to check if some specific characters were given on input using scanf without using character conversions?
int main(void)
{
if(scanf("{ ["))
printf("GOOD INPUT\n");
else
printf("BAD INPUT\n");
return 0;
}
This code always gives the bad input option (it expects scanf return value to be 1) but interestingly if I enter other than the desired characters it gives the bad input imediately but if I enter it as its specified in the scanf it blows the bad input at me AFTER I enter the whole input.
So it must be awaiting the input to be in that specified format but my question is: How can I check it, without any conversions, and make according action depending on wether the input was entered correctly or not?
You might use the %n conversion of scanf (it sets below into pos the number of characters read so far)
int pos= -1;
if (scanf("{ [%n", &pos) >=0 && pos>0)
printf("GOOD INPUT\n");
else
printf("BAD INPUT");
Beware, the return count of scanf might be implementation specific (it probably stays at 0). But pos get assigned to a positive offset of scanf did get { followed by some (zero or more) space-like characters followed by a [.
However, what you probably want is some lexical analysis and parsing, then scanf is not a good solution. You'll better read the entire line (e.g. with fgets or getline) and parse it later.
See also the man page of scanf(3)
scanf (and cousins) returns a count of the number of successful conversions. If you specify 0 conversions, then its return value will always be 0.
To use scanf for this task, I'd probably use a couple of scanset conversions:
char a[2], b[2];
if (scanf("%1[{] %1[[]", &a, &b) == 2)
printf("Matched");
Or, you could simplify this a little bit:
char a[2];
if (scanf("{ %1[[]", &a) == 1)
Either way, we've specified each scan set to match only one specified character, but it's still a conversion, so we can see whether it succeeded or failed.
Unfortunately, we still have to assign the result somewhere. scanf does support using * like this: "%*s", to tell it to read a string, but not store the result anywhere--but when you do so, that conversion doesn't get counted in the return value, so (much like before) we can't use it to determine whether we got a match or not.
If good input needs to exactly 3 characters: { space [, use "%*1[ ]" to scan a space and "%n", which saves the scan character count, to insure scanning reach the expected end.
int main(void) {
int n = -1;
scanf("{%*1[ ][%n", *n);
if (n >= 0) {
printf("GOOD INPUT\n");
} else {
printf("BAD INPUT\n");
}
return 0;
}
I'd recommend to read a line of input with fgets() first and then parse the buffer. That can leave stdin in a better known state when bad input happens.

Detecting if the user enters space or enter in a conditional with cin.get()

I'm trying to get this input loop to stop if the user enters a space ' ' or enter \n but the space part doesn't seem to work, even when I replace in != ' ' with in != 32 which is the ASCII code for space.
#include<iostream>
using namespace std;
int main()
{
int temp, cho = 0;
char in = '0';
while (in != '\n' && in != ' ')
{
in = cin.get();
temp = in - '0';
if (temp >= 0 && temp <= 9)
cho += temp;
}
}
Is it even possible to achieve with cin.get() and a console application?
You actually have three problems here that won't be independently distinguishable from each other to begin with, but which become clear once you apply the reasoning below:
I. Boolean conditional is incorrect
while (in != '\n' || in != ' ')
This is wrong! You probably meant &&.
(A != B || A != C) is always true (assuming B and C are different, as they are in your example) because A cannot possibly equal both of them at the same time.
II. Program logic is in the wrong order
Furthermore, you're checking this in the wrong place. You're doing this:
Set input #0 to '0' to get us going
Does the input #0 meet my exit criteria? (no)
Take input #1
Deal with input #1
Does the input #1 meet my exit criteria? (no)
Take input #2 (say this is a space)
Deal with input #2
Does the input #2 meet my exit criteria? (YES!)
End the loop
You see how you check input #2 too late? It's already been "dealt with". You'll exit the loop just fine after implementing the above fix, but you've already appended the character to cho.
How about this:
int temp, cho = 0;
// Get input for the first iteration
char in = cin.get();
while (in != '\n' && in != ' ')
{
temp = in - '0';
if(temp >=0 && temp <=9)//so only the ASCII of digits would be entered
cho += temp;
// Now get input for the next iteration
in = cin.get();
}
The duplication isn't nice, but you can fiddle with it as you please once the logic's correct.
III. Your terminal has line buffering turned on
Finally, even with this code you may experience problems due to line buffering in your terminal: your program will be functioning absolutely correctly, but since your characters are often by default not sent to the program until a whole line is provided, there is no "live"/"instant" reaction to the act of pressing a space. Only once you hit enter are all those characters finally submitted to your program by your terminal, at which point the backlogged spaces trigger the loop exit; this makes it look like your program is only terminating on the newline condition, but it's not. You might have spotted this if you had generated some output from within your program to see how many characters it was actually processing before quitting.
You can resolve this by turning off line buffering in your terminal emulator, or by removing the ability to use spaces to terminate the loop and instead just rely on newlines — the latter is the convention as then you do not have to ask your users to configure their terminal specially to run your program: it'll already function properly in all the usual cases.
Bootnote — general advice
It's important not to assume that, if applying solution A for problem 1 doesn't immediately make your program work perfectly, that solution A must be wrong. You should consider the possibility that you also have as-yet-unknown problems 2 and 3.
It's really important to keep an open mind and gather evidence, such as writing output from your program to track its execution... or use a debugger to step through it and analyse what it is doing. As far as I can tell, you haven't really gathered any evidence at all about how your program executes… beyond cursory empirical observations, that is.
Keeping Lightness's great answer in mind, it should be noted that you're reading a single whitespace-separated token, which the built-in formatted I/O operators already do. For example, operator>>() is designed to pull out a token of input (say a number) until it reaches whitespace which is a space character and newline character.
A cleaner way of doing it would be to use the standard algorithms and classes from the standard library such as std::istream_iterator and std::accumulate():
#include <iterator>
#include <string>
#include <iostream>
#include <numeric>
int main()
{
typedef std::istream_iterator<std::string> iter_t;
iter_t it(std::cin);
int cho(0);
if (it != iter_t{})
{
auto s = *it;
cho = std::accumulate(std::begin(s), std::end(s), 0,
[] (int v, unsigned char c) { return v + c - '0'; });
}
std::cout << cho;
}
std::istream_iterator uses operator>>() internally and std::accumulate() will go through the characters, convert them to integers, and accumulate their sum.

C++ User enters a non-integer value for integer variable

I am working on program where a list of options is displayed to the user and he would then enter an integer to specify which option he wants to select.Now I have pre-empted the situation where the user might enter an integer value apart from the valid ones. But if he enters any other value, say a character or a string, the program goes into an infinite loop with the list of options being printed infinitely. How can i rectify this? I mean, I should be able to give my user an error when for a predefined integer variable he enters a value that is not an integer.
Any help appreciated. Here is a sample code.
do{
printf("Select appropriate option.\n");
printf("Press 1 to do this");
printf("Press 2 to do that.\n");
printf("Press 3 to exit the program.\n");
scanf("%d",&choice);
if(choice!=1 && choice!=2 && choice!=3)
printf("You entered an invalid choice. Please enter again.\n");
switch(choice){
case 1:
//do this
break
case 2:
//do that
break;
case 3:
exit(1);
}}
while(choice!=3);
So basically when a user enters a non-integer value for choice I want the program to notify the user and ask him for another input.
It cannot be done with direct scanf into an integer variable. Such scanf will not only accept 1.23, it will also accept 1abcde and other inputs. scanf (and other conversion functions) reads as much as it can in accordance with the requested format. It stops when it finds something that does not satisfy format requirements and simply leaves it untouched.
If you want to perform this sort of analysis, you have to read the input as string and then parse and analyze that string manually.
A C-style code sketch (since you insist on C-style code, despite having tagged it as [C++]) might look as follows
char buffer[100]; /* 100 should be enough for everyone :) */
int n = scanf("%s", buffer);
if (n < 1)
/* ERROR, can't read */;
char *end;
long choice = strtol(buffer, &end, 10);
if (end == buffer || *end != '\0' || errno == ERANGE)
/* ERROR, bad format */;
/* ... */
scanf will not consume any non-digits when converting %d. It will return 0 because it didn't convert anything, and the "bad input" will still be there waiting to be consumed. You have to consume it in some way to be ready for a valid input.
(also note you're excluding 3 in your if before testing for it in your switch)
Use iostream - see http://www.cplusplus.com/reference/iostream/
Having said that if you insist on using scanf - check the return value - i.e. read http://linux.die.net/man/3/scanf
isdigit will check for any input that is a digit(number) type.
For this include header ctype.h.
Also terminate your program using exit(0) if input is incorrect.
For this include header stdlib.h.
#include<ctype.h>
#include<stdlib.h>
char c;
scanf("%c", &c);
if (isdigit(c))
{
case statement...
...
...
}
else
{
printf("Wrong input");
exit(0);
}

How to put the string entered into a character array in C++?

Now, I am facing with such a problem: Compare two strings without using "strcmp" in library function.
I have defined the function "mystrcmp" correctly, but I also have to put the string entered into a character array. How can I realize it?
Here is my wrong codes:
char a1[100],a2[100];
int j=0;
do
{
cin>>a1[j];
j=j+1;
}while(getchar()!=10);
int k=0;
do
{
cin>>a2[k];
k=k+1;
}while(getchar()!=10);
cout<<j<<" "<<k<<"\n";
I want to see if the loops are correct through j and k. Unfortunately, the results are wrong.
For example, when I enter "abcdefg" and "gfedcba", I get the result "j=4, k=4".
What's wrong with my codes? How can I correct it?
I'm looking forward to your answers. Thank you.
Why are you using the value 10 in your code? Don't use integer literals in place of character constants, because when you attempt to run this code on a computer that uses the EBCDIC character set you'll notice that '\n' has the value 37, not 10. Use '\n' instead of 10.
Don't mix getchar and cin code. That's a pretty bad idea, because they both consume one character each. In other words, getchar() is consuming one byte, and cin is consuming one byte, so you're consuming two bytes per loop and only storing one of those bytes. If you're going to use getchar, I think you mean something like this:
for (int c = getchar(); c >= 0 && c != '\n'; c = getchar()) {
a1[j++] = c;
}
a1[j] = '\0';
The same sort of thing using C++'s cin:
for (int c = cin.get(); cin.good() && c != '\n'; c = cin.get()) {
a1[j++] = c;
}
a1[j] = '\0';
This is dangerous code. You can write out of a1 & a2 bounds. Use functions made for this, for example cin.getline
http://www.cplusplus.com/reference/istream/istream/getline/