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 1 year ago.
Improve this question
I have a school task that I need to complete. I'm an entry level programmer, really just for some school stuff so it isn't that complicated. I just need to find out, what's the easiest way to cover all of the answers. Here is my code.
float e,d, m,n,d, no;
cout << "Enter the numerator of the first radian or if it doesn't have one, type no: ";
cin >> e;
cout << "Enter the denominator of the first radian or if it doesn't have one, type no: ";
cin >> m;
cout << "Enter the numerator of the second radian or if it doesn't have one, type no: ";
cin >> d;
cout << "Enter the denominator of the second radian or if it doesn't have one, type no: ";
cin >> n;
Then I need to solve an equation with them.
I would like it to work in every possible way but it's diffcult to cover all of the possible answers. Any tips, how should I start. I know it's probably confusing, I don't know if it's correct or not.
How to read number or some text from stream.
When you read a number, but something else is encounter, stream is set int invalid state (failbit flag is set). So to handle case first you have to clear error flag and then read a string.
Since you are expecting "no" text so if something else is encounter then failbit flag can be restored.
std::istream& read_value_or_NO(std::istream& in, std::optional<double>& x)
{
double y;
if (in >> y) {
x = y;
return in;
}
std::string s;
in.clear();
x = {};
if (in >> s && s == "no") {
return in;
}
in.setstate(std::ios::failbit); // error if it is not "no" string
return in;
}
https://godbolt.org/z/rPdTTMbvT
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 months ago.
Improve this question
The folowing code tells user to input their age, it is set to be input interger between 0 and 120, it is capable to deal with wrong input like 'M' or 133 or -1. Warning message goes like this:Warning message
case 1: // input age
cout << "Enter your age: ";
cin >> age;
if(age <= 0 || age > 120){ // if the input type or number was wrong, it goes here
while(1){
cout << "Invalid input! Please enter again" << endl << ">>>";
age = -1;
cin >> age;
if(age > 0 && age <= 120) {break;}
}
}
However, it'll go wrong if I input something like \ or [.
Repeating Warning message
How can I solve this?
By emptying the keyboard buffer before a new entry.
#include <iostream>
#include <limits>
using namespace std;
int main()
{
int age;
cout << "Enter your age: ";
cin >> age;
while(age <= 0 || age > 120)
{
cout << "Invalid input! Please enter again" << endl << ">>>";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> age;
}
return 0;
}
lets walk through this.
You type in something wrong and enter the if clause.
In here you enter the while loop and ask the user again for a input. This input is instantly answered because there is still something in the input stream.
Therefore you need to flush the cin stream before asking again for an input (You can place the flush in for example line 4).
You should be able to clear the stream with a command like this:
How do I flush the cin buffer?
Unfortunately I'm not able to test that out by myself but you can give it a try and I hope it helps!
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 5 years ago.
Improve this question
It's really complicated to me as a beginner to do it so I tried this code :
int x,sum=0;
while (x)
cin >> x;
sum+=x;
cout << sum ;
I want to let the program when the user enters "0" the program should print the sum of these numbers entered by the user.
One way to do it is here:
#include <iostream>
int main()
{
int x;
int sum = 0;
std::cin >> x;
while(x)
{
sum += x;
std::cin >> x;
}
std::cout << "Sum: " << sum << std::endl;
return 0;
}
You are missing {}s around the statements in your while loop, so only cin >> x gets executed.
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 5 years ago.
Improve this question
OK so i know the question is a bit confusing (dont downvote right away, let me explain...)
I have a text file like this:
dim
coins
oponent
I want to read this text file but while reading it, ask the user for specific responses, for example:
"reads line with the word dim" -> asks user the dimensions -> next line -> "reads line with coins" -> asks user how many coins and so forth until the EOF.
Is there anyway to do this? if yes, can you show me how?
Thanks and plz dont downvote, just tell me what's wrong and i will edit the post..
EDIT: This is the way i'm reading the file and asking the user input
void LeitorFich::lerFicheiro(string nomeFich)
{
int i, j, t;
string linha, nome, nome1;
ifstream fich(nomeFich);
while(getline(fich, linha))
{
istringstream iss(linha);
iss >> nome;
if(nome == "dim")
{
cout << nome << " ";
iss >> i >> j;
}
}
cin.get();
fich.close();
}
A simple example will look like this:
Consider I have a file called "test.txt" which contains the very content as yours
string sLine;
ifstream inFile("test.txt");
int Dim1, Dim2, coins, oponent;
while(inFile >> sLine ){
if("dim" == sLine){
std::cout << "Dim1: ";
std::cin >> Dim1;
std::cout << std::endl;
std::cout << "Dim2: ";
std::cin >> Dim2;
std::cout << std::endl;
}
else
if("coins" == sLine){
std::cout << "coins: ";
std::cin >> coins;
}
else
if("oponent" == sLine){
std::cout << "oponent: ";
std::cin >> oponent;
}
}
inFile.close();
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 5 years ago.
Improve this question
When I tried to give the string input like "Hello" it is producing an error. How can I check that when I give string input string it should ask me to give correct input?
int y,m,d,h,min,s;
do
{
cout<<" Please enter the year: ";
cin>>y;
}while(y < 1970 || y > 2020);
#include "iostream"
#include<limits>
using namespace std;
int input()
{
int y;
do
{
std::cin.clear();
std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
std::cout << "Give the year" << std::endl;
std::cin >> y;
}
while (std::cin.fail() || y < 1970 || y > 2020);
return y;
}
main()
{
int x = input();
}
If the input cannot be converted to a int (in your case), then the failbit will be set for std::cin. This can be retrieved by calling cin.fail().
std::cin >> y;
if (std::cin.fail()) {
std::cout << "data entered is not of int type";
}
You can also use !std::cin instead of std::cin.fail().
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 6 years ago.
Improve this question
I have written this code that reads 10 integers from the user, stores them in an array, then calculates and prints the average. It is also supposed to print the numbers in the array that are greater than or equal to the average. Instead, my code prints out every number in the array. How can I fix this?
Also, if anyone has any tips on how to simplify this code, that would be much appreciated, as well.
#include <iostream>
using namespace std;
int main()
{
const int ENTER_NUM = 10;
int integer[ENTER_NUM];
cout << "Enter "<<ENTER_NUM<<" numbers: "<<endl;
cin >> integer[0];
cin >> integer[1];
cin >> integer[2];
cin >> integer[3];
cin >> integer[4];
cin >> integer[5];
cin >> integer[6];
cin >> integer[7];
cin >> integer[8];
cin >> integer[9];
int sum;
sum = integer[0]+integer[1]+integer[2]+integer[3]+integer[4]+integer[5]+integer[6]+integer[7]+integer[8]+integer[9];
int average;
average = sum/ENTER_NUM;
cout<<"Average is: "<<average<<endl;
for(int i=0; i<10; i++)
{
if (integer[i]>=average);
cout<<"Number in array greater than or equal to the average: "<<integer[i]<<endl;
}
return 0;
}
You have a spurious ; after the if in the loop.
When you always use {} on ifs and similar control structures instead of relying on the "one line exception", this error is less likely to happen.
if (integer[i]>=average); contains a semicolon at the end, removing its ability to control the print statement after it. Turn up the warning level on your compiler and you'd have a message about this