Having lots of trouble using cin robustly in C++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I can't seem to come up with a chunk of code that traps the user until it gets a positive int and prompts the user appropriately if the user accidentally types a character or random garbage like "-1-3-4-39-32xfi3/". I just need a neat failproof cin loop structure and can't figure it out. Thanks.
I'm just wondering what other people do regarding console input to make draw ideas from that..how you get around bad input.

Do this:
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
int main()
{
for (std::string line; std::getline(std::cin, line); )
{
std::istringstream iss(line);
unsigned int n;
if (!(iss >> n >> std::ws) || iss.get() != EOF)
{
std::cout << "Bad input ('" << line << "'), ignoring.\n";
continue;
}
std::cout << "Success! We read: " << n << "\n";
return EXIT_SUCCESS;
}
std::cout << "Premature end of input!\n";
return EXIT_FAILURE;
}
In other words, continue reading input from the user until you are able to parse the input as the kind of data you want. I chose line-based processing, which is often the most natural, but any notion of "record" that you can skip if it isn't right will do. Note that it's possible the program never succeeds, namely if the user refuses to provide any good input.

I typically read an entire line with std::getline, then attempt to convert it to an integer with strtol. If the result is less than 0, they typed a leading -. strtol returns a pointer to the first character of input that couldn't be converted as an integer, so if it points to anything but \0, you know the user typed in something that couldn't be converted.
I really should switch to using std::stoi instead of strtol though. It supports pretty much the same capabilities, but its input is a std::string instead of a C-style string like strtol uses. I only continue to use strtol out of long habit, not because it's really a better solution.
Depending on what you have available, and how you want to respond to bad input, another possibility to consider would be boost lexical_cast. This throws an exception if it can't convert the entire input to the target type, so if you want an exception it may be more convenient (but if you don't want exceptions, it'll probably just cause you extra work).

Related

Count numbers in text file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am writing a simple C ++ program as my homework to count the number of numbers in a text file. Below is my code, but unfortunately it counts words and numbers. I have no idea how it should count the number of numbers, where is the error?
My text file:
lorem ipsum 5 87 451 13
My code:
#include <iostream>
#include <cstdio>
#include <fstream>
using namespace std;
int main()
{
int n=0;
char plik[100], liczby[100];
string nazwapliku;
nazwapliku="file.txt";
ifstream we("file.txt");
if (!we)
{
cout<<"Can't read file'";
cin.ignore();
getchar();
return 1;
}
while (!we.eof())
{
we>>liczby;
if(we)
n=n+1;
}
we.close();
cout<<"Numbers count in "<<nazwapliku<<" : "<<n;
getchar();
return 0;
}
Let's start off with two very important things which apply to all programs you are going to write:
Never ever read unconstrained into a buffer - ever! That is the primary attack vector for hackers trying to subvert your program by overwriting what comes after buffer. Always limit the input the maximum space allowed, e.g., setting up the width of the buffer using std::setw(liczby). In nearly all cases in C++ you'd want to use std::string instead of a fixed size character buffer. These don't have problem of buffer overruns (it may still be desirable to limit the maximum amount of space they take which can be done with allocators).
Always test whether input was successful before you use the input. Doing so does not involve stream.eof() as that condition only holds when the program has exhausted all input. Also you need to check after reading (as the loop isn't written idiomatic, I didn't notice that you actually do check if (we); it is worth writing the loop idiomatic, though). For example, your loop could reasonably look like (if it weren't for the second point):
while (we >> std::setw(sizeof(liczby) >> liczby) {
// ...
}
As your program only tries to read words, it obviously only counts the number of words. To count "numbers" which seem to be sequences of digits for your assignment you'll need check whether a given word consists of only numbers (and in case you are trying to use std::isdigit() from <ccctype> please be aware that this function only accepts positive int while char is normally signed, i.e., you'd need to cast your char to unsigned char before feeding them into std::isdigit()). I leave the actual implementation as an exercise, though.

Convert text (words) to integer [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
So I was making guessing game to learn something (I'm a beginner) and at the start I faced the problem that I didn't know how to do rng or something like that so I came up with idea where I just ask user to give his name and something he hates and I need to make this input somehow change to number (any kind except binary).
So in short I need a way to change any inputted text (string) to any integer number.
Here is an example of how to use a hash function, that already exists for the standard library.
#include <cstdio>
#include <string>
#include <iostream>
#include <functional>
int main() {
std::cout << "Starting Process!" << std::endl;
std::cout << "Enter Name: ";
std::string name;
std::cin >> name;
std::hash<std::string> hash_fn;
size_t str_hash = hash_fn(name);
std::cout << str_hash << '\n';
return 0;
}
I would suggest you to take a look at the "random" library if you want rng or to use hashing as other suggested.
If you want to generate yourself pseudo-random numbers from an input string, you could do the following:
To acces a character, you could use the [ ] operator, something like that:
std::string str = "The Name";
str[index]; // to acces a character
Cast the characters into integers to work with them. Keep in mind that every character has an ASCII value, so you could do something like this:
static_cast<int>(str[2]);
Now you have accesed the third character, according to the string above, 'e'. Which happens to be 101 when transformed into an int (remember, the ASCII value).
You can then create some algorithm using that.

c++ fstream when see string in file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I made a code but it doesn't work.Can you help? In code, I wanted to take a line and if code see //n, end line.
Here is my example.
File:
I love C++! //n
My Code:
ifstream file("file.txt");
char text[250];
while(file >> text){
cout << text << " ";
if(text == "//n"){
cout << endl;
}
}
Thanks for your help.
I am unsure as to what you are trying to do, however it seems that you want to get a line worth of text. If so, you would want to change
char text[255];
....
if(text == "//n")
to
std::string text;
...
if(text == "\n")
The array comparison will not compare strings. So use the std::string to allow you to use == operator.
As C++ special character codes use a backslash rather than two forward slashes.
However, I'd also suggest using a single char rather than an array (as indexing doesn't seem to be a concern since you only access the first character in it).
If you want to read a file a word at a time:
ifstream file("file.txt");
std::string line;
while(std::getline(file, line)) {
std::stringstream linestream(line);
std::string word;
while(linestream >> word) {
std::cout << "Word: " << word << " ";
}
std::cout << "EOL\n";
}
Basically your code has a couple of issues.
'//n' is not a special character we all assume you meant '\n'.
Most operators that read text from a file will disgard the '\n' character.
operator>> will disguard white space (including \n).
getline() reads the line but drops the \n.
Thus '\n' is never in text to be compared too.
Arrays char text[255] will convert themselves into pointers easily.
Thus the comparison you are doing compares two pointers. This will never be equal.
You need a type that does something smart with == so that you compare the text.
For this you should use std::string.
As a good style guide.
Never put using namespace std; in your code. It causes more trouble when you have anything but a simple bit of throwaway code. And using it in simple throwaway code is a bad habit that will catch up to you someday.
The reason std (as well as others) is short and not standard is so that prefixing it items from the standard library is not burdensome.
std::cout << text << " "; // not hard.
std::cout << std::endl;
There is no real reason to use std::endl (debugging being an exception I suppose). In normal situations the extra flush it adds will generally cause the output to slow down perceptibly. So prefer to use '\n' unless you specifically want to force a flush.
The problem here is, your code compares pointers, instead of comparing strings (as noted also in the other answer):
if(text == "//n")
{
...
}
Here text is a pointer (actually, an array of char, but in this context it's equivalent to a pointer), and "//n" also is a pointer (also an array). Comparing two pointers is not what you want.
To fix your code so it compares strings, one of the things your code compares should be a string instead of a pointer. To mark "//n" as a string, append s to it:
if(text == "//n"s)
{
...
}
This is a bit obscure; another solution is to make text a string (as described in the other answer):
std::string text;

Everytime I read in through ifstream for a specific input, the input changes, possible int overflow [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Every time I use ifstream for input to my program for large inputs, I get something weird. I have a feeling this has to do with integer overflow, but my program still doesn't work with unsigned long long. Here is a simplified version of my code that still exhibits the error:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream fout ("namenum.out");
ifstream fin ("namenum.in");
unsigned long long serial;
fin >> serial;
ifstream myReadFile;
cout << serial << endl;
return 0;
}
Here is the strange input (or larger inputs):
5747867437
Here is the output I get from cout:
1452900141
I have no idea what is causing this. Any help would be awesome.
Here is advice I have hardly given ever before: always check your inputs after you attempted to read (it feels, I have given this advice only a few thousands times so it is easy to miss). The stream can't predict what you are going to read and make sure it will work:
if (fin >> searial) {
fout << serial << '\n';
}
else {
std::cerr << "failed to read the value\n";
}
Looking at your code, I'd be about 100% certain that either the file failed to open (i.e. the stream is in bad state prior to the attempt to read) or the claimed content isn't in the file.
First of all, the number you supplied is certainly within the limit of unsigned long long.
Second, using >> for unsigned long long requires C++ 11 support. C++0x supports unsigned long.
I copied your code and made a file called "namenum.in", typed "5747867437" into "namenum.in" using UTF8 encoding.
Then the output is exactly 5747867437.

How to cover all possible data types when declaring a function parameter?

I'm attempting to construct a function that will perform a sanity check on the user's response to a number of questions, each of which would ideally be a non-zero integer. How can I construct a function that would be able to accept a parameter of any data type, but only have a single parameter? For example:
bool SanityCheck(<type id> number)
where <type id> would cover any data type.
It's not clear exactly what you really want here. Unverified input from a user normally comes in the form of a string. Typically you read in a string, verify that it has the desired form (e.g., for an integer, all digits). If it has the right form, you convert that to the desired type, and use it. If it doesn't, you ask the user to re-enter their data, usually with a prompt like "Please enter an integer between 1 and 10".
A function template is sort of a direct answer to the question you asked, but I have a hard time imagining it being of any help in a situation like you've described. A function template is most often of use in cases where you have to carry out some operations that are syntactically the same across a number of types. For example, it lets you add two numbers, regardless of whether those happen to be of type short, int, long, float, double, long double, etc. That only works because they're really all numbers, and you can reasonably use + to add any of them together.
When you're dealing with some unknown input, that doesn't apply though -- you need to verify enough about the data to be sure the operation is sensible and meaningful before you can do much else with it; it's pretty hard to get a meaningful result from comparing (for example) 7 to a sunset.
C++ is a statically typed language. What type a variable is of will be fixed at compile-time and cannot be changed at run-time. What users enter, however, will only be known at run-time, and cannot be known at compile-time. Therefore your question makes no sense.
When you expect an integer from a user, then the best way would be to try to read an integer, and check whether this succeeds:
int i;
std::cin >> i;
if(!std::cin)
throw "Stupid user blew it!"; // or some real error handling
However, the catch with this is that, once an input operation fails, an input stream enters a bad state and the data that couldn't be read stays in the input buffer. If you want to handle this gracefully, would have to clear the stream's error state flags, and make it ignore whatever is in the input buffer.
So sometimes it might be easier to first read a string
std::string input;
std::cin >> input; // either read up to any whitespace, or
std::getline(std::cin, input); // newline, or
std::getline(std::cin, input, '\t'); // tab, or whatever you want
because this always succeeds, and then try to convert it into whatever data you need. The way to do this is via string streams:
std::istringstream iss(input);
int i;
iss >> i;
Now you can check the string stream's state
if(!iss)
and if the conversion failed, std::cin will still be usable and the erroneous input read from its buffer.
However, there's one more catch: If a user inputs '"42 thousand"', then this won't catch the error. The remaining characters will be in the string streams input buffer and silently ignored. So what you usually need to do for such a conversion is to test whether the string stream's buffer is fully read, that is: reading reached EOF. You can check for this by invoking iss.eof(). However, if you read a whole line, there might be extra whitespace at the end, which you wouldn't want to make the conversion fail, so you need to read extra whitespace before you check for EOF: iss >> std::ws. (std::ws is a stream manipulator that "eats" consecutive whitespaces.)
by now, the conversion would look like this:
std::istringstream iss(input);
int i;
iss >> i >> std::ws; // you can chain input
if(!iss.eof())
throw invalid_input(input);
Of course, this is pretty elaborated for a one-time conversion and I wouldn't exactly swear by the life of my kids that there isn't a nice improvement left that I hadn't thought of yet. So you would at least want to wrap this into a function and put that into your toolbox for reusing it (and improving on it, if you find an error):
bool convert_to_int(const std::string& str, int& result)
{
std::istringstream iss(input);
iss >> result >> std::ws;
return iss.eof();
}
Or, generic for any type:
template< typename T >
bool convert_from_string(const std::string& str, T& result
{
std::istringstream iss(input);
iss >> result >> std::ws;
return iss.eof();
}
Even better would be to use a ready-made off-the-shelf solution for this. Boost has just such a thing with its lexical_cast.
Here's a skeleton algorithm for the whole input routine:
int i;
do {
read string input
convert to int i
while(!conversion succeeded);
With the bits from further above, you should be able to fill in the missing parts.
Use templates:
template <typename T>
bool SanityCheck(T number);
The sanity check may vary for different types. As this is a homework, I won't post any more code just hint you with a Google search term "partial template specialization".
Ok, I think I get what you actually want now.
I imagine your situation is something like this:
Read some user input (maybe using std::cin).
Check to make sure it is an int.
Use the int if it is one.
If this is the case then you do not want a function that can handle different data types, because the user cannot enter different data types, he can only enter characters and you have to choose what datatype you want to store that as.
I think this is what you need:
bool valid = false;
int input = 0;
while (!valid)
{
std::string inputStr;
std::cin >> inputStr;
valid = isInteger(inputStr);
if (!valid)
std::cout << "Please enter an integer." << std::endl;
else
input = atoi(inputStr.c_str());
}
std::cout << "You entered " << input << "!" << std::endl;
You're going to have to write isInteger yourself, but hopefully you get the idea.
Option 1: use boost::variant if you want it to be a single function
Option 2: overload this function for all types that you need
Making your function a template function would achieve this.
template<typename T>
bool SanityCheck(T number);
A lot of online surveys that I'm asked to fill out don't ask me to enter data but only select an option from 1 to 5. 1 = Totally Agree, 5 = Totally Disagree. This seems a more efficient way of collecting user input since you have total control over data type and all I have to do is highlight an option box.