modify number of variables in istringstream - c++

I need to get input in a line based on a modifiable number of variables that are determined from an earlier input.
If the input you get in an earlier function is 1, the the code would be like
std::string istring;
std::getline(std::cin, istring);
std::istringstream stream(istring);
while (stream >> a)
{
statement;
}
is it possible to make a condition for while loop that changes depending on what your input is ? so that if the input was for example 5, it would behave like
while (stream >> a >> a >> a >> a >> a)
{
statement;
}
I tried doing while ((stream>>a)*number) but that doesn't work

Specific Solution
First to answer your specific question:
I need to get input in a line based on a modifiable number of variables that are determined from an earlier input.
The solution in C++ to that is a combination of a istringstream, a while loop and a vector:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
int main() {
unsigned int earlierInput = 5; // this is the earlier input
std::vector<int> numbers; // The type of vector also controls what
// data type you want the user to give
// you. Because the stream will try to
// the user input into that type.
while (numbers.size() != earlierInput) {
// Here we ask the user for input and convert the input
// to a stream, like that we can try to convert the user
// input into the data type we want.
std::string istring;
std::cout << "Please type in " << earlierInput << " numbers: ";
std::getline(std::cin, istring);
std::istringstream reader(istring);
// read as many numbers as possible.
for (int number; reader >> number;) {
numbers.push_back(number);
}
if (numbers.size() != earlierInput) {
// if not enough numbers empty vector and print
// error message
std::cout << "Not the right amount of numbers!";
std::cout << "Try again!" << std::endl;
numbers.clear();
}
}
// If we get here the user typed in the right amount of numbers
std::cout << "You typed the following numbers:" << std::endl;
for (const int& i : numbers) {
std::cout << i << std::endl;
}
}
This is for exactly one user input. If you want to ask the user an arbitrary amount of times to do this (e.g you want to ask the user 10 times for 5 numbers), then you need to apply the rule above again and as such stack up:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
int main() {
unsigned int amountOfInputs = 2; // this is the amount of times
// you want the user to type
// something in
unsigned int earlierInput = 5; // this is the earlier input
std::vector<std::vector<int>> allNumbers; // Now we need a vector in
// a vector to store the results.
// The type of the inner vector also
// controls what data type you want the
// user to give you.
while (allNumbers.size() != amountOfInputs) {
std::vector<int> numbers; // This is the vector as in the example
// above.
while (numbers.size() != earlierInput) {
// Here we ask the user for input and convert the input
// to a stream, like that we can try to convert the user
// input into the data type we want.
std::string istring;
std::cout << "Please type in " << earlierInput << " numbers: ";
std::getline(std::cin, istring);
std::istringstream reader(istring);
// read as many numbers as possible.
for (int number; reader >> number;) {
numbers.push_back(number);
}
if (numbers.size() != earlierInput) {
// if not enough numbers empty vector and print
// error message
std::cout << "Not the right amount of numbers!";
std::cout << "Try again!" << std::endl;
numbers.clear();
}
}
// If we get here the user typed in the right amount of numbers
// and we can save them and clear the array for using it again.
allNumbers.push_back(numbers);
numbers.clear();
}
std::cout << "You typed the following numbers:" << std::endl;
unsigned int round = 1;
for (auto numbersOfRound : allNumbers) {
std::cout << "For round " << round++ << ": ";
for (auto i: numbersOfRound) {
std::cout << i;
}
std::cout << std::endl;
}
}
Theory
Why Vectors
To save an arbitrary amount of data and be able to access it, you must use a dynamic allocated array or anything alike. That is because at compile time you don't know how many variables you will need at runtime and as such you cannot give all of them a name.
Why streams
In theory a stream is a possibly infinit long string or data (see also here). A user input is therefor a stream as it may theoretically be infinit long. Though this does not apply in praxis.
To extract informations from a stream one has to use the >> operator, also known as the extraction operator. This operator does not support vectors as an (rhs) operand as stated in the documentation here. It supports only basic datatypes (that is why we need a temporary variable int number in the examples above).

Related

What do you do in C++ if you ask for an integer and the user inputs "b"?

I'm new to statically typed C++. In JavaScript, I could just check the data type first, but that seems to be very complicated, and the answers all seem to imply that you aren't "getting" the language.
here's the code I was testing out rand() with, where I came upon the issue of converting strings to integers:
int main(){
std::string input;
cout <<endl<< "What to do?"<<endl;
cin >> input;
if (input == "rand")
{
cout << "what is the max?" << endl;
cin >> input;
int number;
if (stoi(input) > 1) {
number = stoi(input);
}
else {
number = 10;
cout << "using 10"<<endl;
}
cout << rand() % stoi(input);
return main();
}
}
so in Javascript, I would just check the type of input or result, but what do people do in C++?
Not allowed to say thank you in the comments so I'm saying thank you here!
Well, let's try out what happens: https://godbolt.org/z/1zahbW
As you can see, std::stoi throws an exception if you pass it invalid input or its input is out of range.
You should, however, be aware that std::cin >> some_string; is somewhat non-obvious in that it reads in the first "word", not a line or anything like that, and that std::stoi does the same thing (again).
One way to perform the check, could be like this:
#include <string>
#include <iostream>
int main(){
std::cout << "Please give me a number: " << std::flush;
std::string input;
std::getline(std::cin, input);
try {
auto value = std::stoi(input);
std::cout << "Thanks for the " << value << " (but the string was \"" << input << "\")\n";
} catch(std::invalid_argument const&) {
std::cout << "The provided value is not an integer\n";
} catch(std::out_of_range const&) {
std::cout << "The provided value is out of range\n";
}
}
https://godbolt.org/z/rKrv8G
Note that this will parse " 42 xyz" as 42. If that is a problem for your use case, you may wish to use std::strtoi directly, or to check if your input is valid before parsing (e.g., using a regex)
Regarding to the documentation of std::stoi it throws an std::invalid_argument.
What you could do is to place your std::stoi call inside a try and then catch the std::invalid_argument, but personally i wouldn't do that.
Instead, it is (most likely) a lot better to check if the first character of your input is an int, because if it is one, it can simply be parsed by std::stoi.
You can do that by e.g. doing the following:
int max = 0;
std::string input;
std::cin >> input;
if(std::isdigit(input[0]))
max = std::stoi(input);
EDIT: Please note that this would not respect the case of a too big number, to handle that case you would need an additional check.

How to properly terminate input

Currently working on an exercise from Stroustrup's intro to C++ book and it asks: Write a function that given two vectors price and weight computes a value (an “index”) that is the sum of all price[i]*weight[i]. Make sure to have weight.size()==price.size()
vector<double> weight;
vector<double> price;
void read_weight() {
cout << "Input weight" << '\n';
for (double weights; cin >> weights; ) {
weight.push_back(weights);
if (!(cin >> weights)) {
cin.clear();
cin.ignore(numeric_limits<double>::max(), '\n' );
break;
}
}
}
void read_price() {
cout << "Input price" << '\n';
for (double prices; cin >> prices; ) {
price.push_back(prices);
if (!( cin >> prices)) {
cin.clear();
cin.ignore( numeric_limits<double>::max(), '\n' );
break;
}
}
}
void calculate() {
double sum = 0;
for (int i = 0; i < price.size(); ++i)
sum += price[i] * weight[i];
cout << "Sum is " << sum << '\n';
}
int main() {
read_weight();
read_price();
calculate();
}
This is the code I currently have, but I cannot figure out how to terminate input properly. In previous chapters, his book mentions you could use an input besides a double to terminate (like |). However, I learned that this only puts cin into the failed state and does not allow me to move onto the price function. Unfortunately, the book does not cover how to handle this as of yet so I just copied code about cin.clear and cin.ignore in hopes that it would work. But this did nothing. I noticed that I was actually allowed a single input if I were to change the vector to int instead of double and am perplexed for the difference in behavior. I was wondering if anyone could give me tips on how to fix this?
You were halfway there already. I've written a commented replacement for read_weights(); you should be able to take it from there.
#include <vector>
#include <limits>
#include <iostream>
// Use return values instead of working on global variables.
// Avoid using global variables whenever possible, they are a
// maintenance pain.
std::vector< double > read_weights()
{
std::vector< double > weights;
double weight;
// You can embed the \n right in the string, no need to put it as a
// separate character.
std::cout << "Input weights; enter a non-number to terminate input.\n";
// If anything not double is entered, cin goes into fail state --
// that is our terminating condition right there, so use it!
while ( std::cin >> weight )
{
weights.push_back( weight );
}
// Clean up cin
std::cin.clear();
// Use the correct type for max(); you had 'double' here...
cin.ignore( numeric_limits< std::streamsize >::max(), '\n' );
// Don't worry about the apparent copying of the vector upon return.
// Any modern compiler should be able to optimize this away.
return weigths;
}
A simple main() for testing:
int main()
{
std::vector< double > weights = read_weights();
std::cout << "Vector contents:\n";
for ( auto & v : weights )
{
std::cout << v << "\n";
}
}
Now all you have to add is a read_price()... now wait, you don't, do you? Because all you're actually doing is the very same thing as in read_weights(), entering doubles! So move the input prompt out of read_weights() and make it one function, read_values(), which you call twice, once to get weights and once to get prices...
int main()
{
std::cout << "Enter weights; enter a non-number to terminate input.\n";
std::vector< double > weights = read_values();
std::cout << "Enter prices; enter a non-number to terminate input.\n";
std::vector< double > prices = read_values();
// ...
}
For the calculate function, use references for the parameters so the vectors don't have to be copied:
void calculate( std::vector<double> & weights, std::vector<double> & prices )
And once you got this all running, keep in your mind that, later on, you will (or at least should) be learning about <algorithm>, functors, and lambdas... which should remove the need for calculate and replace it with an elegant one-liner... but that's yet to come, and I don't want to confuse you with that at this point.
You can use std::getline to read a whole line from cin into a string. Thereafter, you need to determine whether the line that has been entered is a double or not. I normally wouldn't recommend going too wild with regular expressions (I don't use them a lot in C++), but in this case I reckon it is a pretty simple and sound solution. If the string is in the format "32.23" (digits dot digits), you convert it to a double using std::stod, push that to the vector and continue to read from cin. If it is not, you break the loop and go on with the program flow.
Stay away from using global variables, use local ones and pass them arround.
Also, notice that your functions read_price and read_weight are almost identical. In such a case, you definitely only want to write a single (parameterized) function. In this case, you don't even need parameters, you can just use the same function for both.
(You could also directly read your values into double variables from a stream (std::cin), which might be considered more elegant as you need less conversion, however the method below is easy and you don't need to worry what is being entered into std::cin)
#include <vector>
#include <string>
#include <iostream>
#include <regex>
std::vector<double> get_doubles_from_cin(){
std::vector<double> doubles;
std::regex double_regex ("\\d+\\.\\d+");
std::string input;
while(std::getline(std::cin, input)){
if (std::regex_match(input, double_regex)){
doubles.push_back(std::stod(input));
}
else{
break;
}
}
return doubles;
}
void calculate(std::vector<double>& weights, std::vector<double>& prices) {
double sum = 0;
for (int i = 0; i < prices.size(); ++i) {
sum += weights[i] * prices[i];
}
std::cout << "Sum is " << sum << '\n';
}
int main() {
std::cout << "Enter weights" << std::endl;
auto weights = get_doubles_from_cin();
std::cout << "Enter prices" << std::endl;
auto prices = get_doubles_from_cin();
calculate(weights, prices);
}

How to determine number of values added to vector in C++?

I'm trying to build a little task using C++ in which I need to allow the user to determine up front how many gross_paychecks they would like to place in a vector called 'gross_paychecks_vector'.
So far this is what I have:
vector<double> gross_paychecks_vector (5);
double gross_paychecks;
// Add 5 doubles to vector
cout << "Please enter an integer" << endl;
cin >> gross_paychecks;
for(gross_paychecks = 0; gross_paychecks <= gross_paychecks_vector; ++gross_paychecks ){
cin >> gross_paychecks;
}
Right now I'm somewhat lost because I'm not sure whether to switch the vector to something like vector<double> gross_paychecks {} because it throws an error in the for loop.
Also I'm not sure how to go with the for loop (should I actually use a for-loop or something else?). I need to accept input from the user as long as it has not met the numbers of gross_paychecks that he/she has specified.
You probably want this:
vector<double> gross_paychecks_vector; // initially the vector is empty
...
cout << "How many paychecks:" << endl;
cin >> gross_paychecks;
for (int i = 0; i < gross_paychecks; i++)
{
double value;
cin >> value;
gross_paychecks_vector.push_back(value); // add new value to vector
}
// display values in vector
for (auto & value : gross_paychecks_vector)
{
cout << value << "\n";
}
Additionally. If you want to use modern C++ features, you would use:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
int main()
{
std::vector<double> grossPaychecks{};
std::cout << "How many paychecks:\n";
size_t numberOfPaychecks{0};
std::cin >> numberOfPaychecks;
// Read all data
std::copy_n(std::istream_iterator<double>(std::cin),numberOfPaychecks, std::back_inserter(grossPaychecks));
// Print all data
std::copy(grossPaychecks.begin(), grossPaychecks.end(), std::ostream_iterator<double>(std::cout,"\n"));
return 0;
}

Converting string to number when using getline()

I've picked up a book on C++ and I'm basically at the very beginning of it (just started). For some of the problems I had to solve within the book I used the input stream cin the following way -->
cin >> insterVariableNameHere;
But then I did some research and found out the cin can cause a lot of problems, and so found out about the function getline() within the header file sstream.
I'm just having some trouble trying to wrap my head around what's happening in the following code. I don't see anything that uses the extraction operator (>>) to store the number value in. Its (my problem) further explained in the comments I left.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// Program that allows a user to change the value stored in an element in an array
int main()
{
string input = "";
const int ARRAY_LENGTH = 5;
int MyNumbers[ARRAY_LENGTH] = { 0 };
// WHERE THE CONFUSION STARTS
cout << "Enter index of the element to be changed: ";
int nElementIndex = 0;
while (true) {
getline(cin, input); // Okay so here its extracting data from the input stream cin and storing it in input
stringstream myStream(input); // I have no idea whats happening here, probably where it converts string to number
if (myStream >> nElementIndex) // In no preceding line does it actually extract anything from input and store it in nElementIndex ?
break; // Stops the loop
cout << "Invalid number, try again" << endl;
}
// WHERE THE CONFUSION ENDS
cout << "Enter new value for element " << nElementIndex + 1 << " at index " << nElementIndex << ":";
cin >> MyNumbers[nElementIndex];
cout << "\nThe new value for element " << nElementIndex + 1 << " is " << MyNumbers[nElementIndex] << "\n";
cin.get();
return 0;
}
stringstream myStream(input): Creates a new stream that uses the string in input as "input stream" so to speak.
if(myStream >> nElementIndex) {...): Extracts number from the stringstream created using the line above into nElementIndex and executes ... because the expression returns myStream, which should be non-zero.
You were probably confused by using the extraction as the condition in the if statement. The above should be equivalent to:
myStream>>nElementIndex; // extract nElement Index from myStream
if(myStream)
{
....
}
What you probably wanted was
myStream>>nElementIndex; // extract nElement Index from myStream
if(nElementIndex)
{
....
}

How to make cin >> not convert float to integer?

I have the following simple code:
#include <iostream>
int main()
{
int a;
std::cout << "enter integer a" << std::endl;
std::cin >> a ;
if (std::cin.fail())
{
std::cin.clear();
std::cout << "input is not integer, re-enter please" <<std::endl;
std::cin >>a;
std::cout << "a inside if is: " << a <<std::endl;
}
std::cout << "a is " << a <<std::endl;
std::cin.get();
return 0;
}
When I run the above code and input: 1.5, it outputs: a is 1. FYI: I compile and run the code with gcc 4.5.3.
This means that if cin expects an integer but sees a float, it will do the conversion implicitly. So does this mean that when cin sees a float number, it is not in fail() state? Why this is the case? Is it because C++ does implicit conversion on >> operator?
I also tried the following code to decide whether a given input number is integer following idea from this post: testing if given number is integer:
#include <iostream>
bool integer(float k)
{
if( k == (int) k) return true;
return false;
}
int main()
{
int a;
std::cout << "enter integer a"<< std::endl;
std::cin >> a ;
if (!integer(a))
{
std::cout << "input is not integer, re-enter please" ;
std::cin.clear();
std::cin >> a;
std::cout << "a inside if is: " << a <<std::endl;
}
std::cout << "a is " << a <<std::endl;
std::cin.get();
return 0;
}
This block of code was also not able to test whether a is integer since it simply skip the if block when I run it with float input.
So why this is the case when getting user input with cin? What if sometimes I want the input to be 189, but typed 18.9 by accident, it will result in 18 in this case, which is bad. So does this mean using cin to get user input integers is not a good idea?
thank you.
When you read an integer and you give it an input of 1.5, what it sees is the integer 1, and it stops at the period since that isn't part of the integer. The ".5" is still in the input. This is the reason that you only get the integer part and it is also the reason why it doesn't seem to wait for input the second time.
To get around this, you could read a float instead of an integer so it reads the whole value, or you could check to see if there is anything else remaining on the line after reading the integer.
When reading user input I prefer not to use operator>> as user input is usally line based and prone to errors. I find it best to read a line at a time and validate:
std::string line;
std::getline(std::cin, line);
This also makes it easy to check for different types of numbers.
std::stirngstream linestream(line);
int val;
char c;
if ((linestream >> val) && !(linestream >> c))
{
// Get in here if an integer was read.
// And there is no following (non white space) characters.
// i.e. If the user only types in an integer.
//
// If the user typed any other character after the integer (like .5)
// then this will fail.
}
Of course boost already supports this:
val = boost::lexical_cast<int>(linestream); // Will throw if linestream does
// not contain an integer or
// contains anything in addition
// to the integer.
Boost of course will convert floats as well.
I have some snippet which is kind a poor coding, but it works.
This method is pretty simple, but doesn't handle case when input value is invalid.
See more: https://en.cppreference.com/w/cpp/string/byte/atof
static float InputFloat(std::string label)
{
std::string input;
std::cout << label;
std::cin >> input;
return atof(input.c_str());
}
int main()
{
float value = InputFloat("Enter some float value: ");
std::cout << "value = " << value;
return 0;
}