Taking an unspecified number of inputs 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 5 years ago.
Improve this question
I came across a question whereby you are told there will be unspecified number of queries hence you got to keep on taking input for this unspecified number of queries
all i know is that in c++ or even in another programming language, when the program needs to take unspecified number of input, we prompt the user to enter a certain value which will be used to terminate the infinite loop e.g
for (;;)
{
cout<<"enter 0 to stop taking input"<<endl;
int value;
cin>>value;
if (value==0)
{break;}
}
my question is how will i handle the question stating the input will be unspecified and its in an online environment

for this kind of problem you will use a loop control variable and a while loop for user input validation
while loop is use for input validation while for loop is use for specific
number of times only and it is not really suggested for unknown number of times
same with while loop, do while loop can also perform input validation the
only difference is, it will prompt the user first before evaluating the test condition
int myNum; // this is the loop control variable
cout << "enter a number, enter 0 to stop taking input " << endl;
cin >> myNum;
while(myNum!=0)
{
cout << "cograts you did not enter zero digit" << endl;
cin >> myNum;
}

use while (cin >> a) :
to see how if it works create an input file 1.in and write a bunch of numbers in it, then pipe it to your executable fle ./a.out <1.in
#include <iostream>
using namespace std;
int main() {
int a;
while (cin >> a) {
cout << a << "\n";
}
return 0;
}

Related

money converter c++ gives bracket errors [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Here is my code:
#include"std_lib_facilities.h"
int main()
{
cout << "Which currency do you wanna exchange for, $? €? or ¥?:\n";
string currency;
string dollars;
string euros;
string yen;
char $;
char €;
char ¥;
if (cin >> $);
cout << "You have chosen dollars.\n";
cout << "Please enter your amount:\n";
int amount;
cin >> amount;
cout << "Your amount is:" << amount*153 << "PKR" << endl;
keep_window_open();
return 0;
}
I tried making a money converter but my code till dollar wont run so I haven't wrote euro or yen yet. thank you for the response.
First of all, don't use those symbols as variable names, since they're not ASCII characters and could get messed up.
Secondly, if (cin >> $); doesn't do what you think it does. All it does reads something into the $ variable, and moves on regardless of what cin actually read because the if statement is terminated by a semicolon. The code after that is going to get executed, regardless of what cin actually read. Instead, you should do
if (condition) {
thing to execute if condition is true
}
Note the use of curly braces.
Thirdly, this is not how you check if the input string matches the dollar sign. What you should do instead, is read the input into a variable, and then compare that variable to the currency symbols.

If statement not excepting line of text? [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 4 years ago.
Improve this question
This code should ask for in put with "What is If (selection)?"
and this part it does. but once you input the answer as "Provides the ability to either do a block of code or skip that block of code." the out come should be "Correct!" but instead it either asks to press key to end or re asks the question. does anyone have and advice as to how i could fix this?
srand((unsigned)time(0));
int random_interger;
int lowest = 2, highest = 18;
int range = (highest - lowest) + 1;
for (int index = 0; index < 20; index++) {
random_interger = lowest + int(range*rand() / (RAND_MAX + 1.0));
if (random_interger == IF) {
cout << " What is If (selection)?" << endl;
cin >> IFs;
if (IFs == "Provides the ability to either do a block of code or
skip that block of code.") {
cout << "Correct!" << endl;
}
string IFs;
cin >> IFs;
Even if the user types exactly the long line you expect (he won't), this wouldn't work. cin >> into a string will read just one word. You would get only "Provides".
Look up the getline API. But even then, I doubt anyone would type those lines exactly.

g++ compiling and while loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Prerequisites: Atom as code editor with gpp-compiler(plugin), that uses g++ to compile and run cpp files in the editor.
Some not real example code to understand the problem:
int main()
{
int number;
while(cin >> number)
{
cout << "Your number is " << number << endl;
}
}
So this program can easily compile by g++ compiler, the problems appears at runtime, when compiled program starts in terminal and... It's just don't work. There is no something else but "Press any key to continue..." There is no even mistakes.
So the compiler cannot support this loop argument? (while(cin>>number))
And yes, gpp-compiler in Atom works fine with other types of scripts.
Sorry, if this question is stupid but i just want to know why this happens. Thank you!
Some edits:
So yeah. I can't competently explain my problem. So my problem is not the while loop argument, i just don't understand why program starts in empty terminal(with message above), while on my phone it also compiles by g++ and the program works perfectly .-.
The (cin >> number) condition always evaluates to true until you send an EOF character to it. On Windows it is Ctrl + Z. The reason you are not seeing anything on standard output is that the program waits for your to enter a value and press Enter. Afterwards it enters the endless loop. Modify your program to include some simple logic:
#include <iostream>
int main() {
char choice = 'y';
int number;
while (std::cin && choice == 'y') {
std::cout << "Enter the number: ";
std::cin >> number;
std::cout << "Your number is " << number << std::endl;
std::cout << "Repeat? y / n: ";
std::cin >> choice;
}
}

Cant cin after terminating the while loop [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 9 years ago.
Improve this question
int n;
while(cin>>n)
cout << n; // Run by the program if received an int value
cout << "Break from loop"; // Run by the program
cin >> n; // Skipped by the program
cout << n; // Run by the program
cant accept another input after terminating the while loop using characters.
How to accept another input if the input within the loop has been terminated using non-integer/floating-point values.
If you are not terminating the program by returning end of file (i.e. Ctrl-D) or terminating the program altogether (i.e. Ctrl-C).
That is, if you exit the loop via incorrect data type, such as typing in the letter d instead of an integer, you can follow the while loop with cin.clear() and getline(cin, str), where str is some string you declare ahead of time.
You should be able to accept input for the second cin at after this.
So,
string str;
int n;
while(cin>>n)
cout << n << endl;
cin.clear();
getline(cin, str);
cout << "Break from loop" << endl;;
cin >> n;
cout << n;
int n;
while(cin>>n) // Keep asking for value to input
cout<<n<<"\n"; // This loop will never terminate for any Supplied val
//Above loop will terminate only when no more valued is supplied to code
// Hence once, we stopped entering the value, code will execute next line
// And end without asking for anymore value.
cout<<"Break From Loop \n";
Assuming your question is "How do I resume input after the stream state has been set?" then there is a simple explanation:
The while loop in which you performed the extraction terminated only until the extraction failed; this also includes when the stream reaches the end of the input stream (EOF). When that happens the eofbit will be set in the steam state (as well as the failbit). A stream can't be used for I/O when its stream state is set. In order to use it again, the stream state must be cleared. This is done using the member function clear(std::ios_base::iostate err = std::ios_base::goodbit).
std::cin.clear();
That call will clear the bits in the stream state and assign them to 0 (std::ios_base::goodbit). After this call the stream can be used for I/O again.
This is assuming the stream read all the characters until it reached EOF. It's not sufficient for a pervious read that terminated upon the acquisition of invalid data. One would also have to ignore() the remaining characters.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

finding numbers between 2 numbers [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 9 years ago.
Improve this question
I just started learning c++ and very new to it and trying to write a simple for loop to print all numbers between two numbers.
e.g 1-4
numbers between 1,4
output
2
3
the for loop.
int main() {
int firstNumber;
int secondNumber;
std::cout << "Enter first number" << std::endl;
std::cin >> firstNumber;
std::cout << "Enter second number" << std::endl;
std::cin >> secondNumber;
for (int i=firstNumber; i<secondNumber; i++) {
std::cout << i << std::endl;
}
}
At the first step of for loop execution, i++ is not applied - and i is still equal to 1 (its initial value, defined in int i = firstNumber statement. Quoting the doc:
for (initialization; condition; increase) statement;
It works in the following way:
initialization is executed. Generally it is an initial value setting
for a counter variable. This is executed only once.
condition is
checked. If it is true the loop continues, otherwise the loop ends and
statement is skipped (not executed).
statement is executed. As usual,
it can be either a single statement or a block enclosed in braces { }.
finally, whatever is specified in the increase field is executed and
the loop gets back to step 2.
In your case you can just start the loop from firstNumber + 1.
Why not just set i to 2 to start the loop?