How to terminate an integer-input loop with a character? - c++

New to Stack and C++.
I want to terminate a loop that regurgitates numbers fed to it with a character. Say, Q for Quit. The following program is functional and free of syntax errors. How can I terminate this loop without editing the input parameter?
#include <iostream>
#include <string>
using namespace std;
int main()
{
bool run = true;
while(run)
{
cout<<"Enter your two favorite numbers."<<endl;
int num1;
int num2;
cin>>num1>>num2;
cout<<"You entered "<<num1<<" and "<<num2<<"."<<endl;
}
return 0;
}

You want the break statement. But you'll also then need to read a string for the first input, rather than an integer.
while(run)
{
cout<<"Enter your two favorite numbers, or 'Q' to exit."<<endl;
string input;
int num1;
int num2;
cin>>input1;
if (input == "Q")
{
break;
}
else
{
num1 = stoi(input);
}
cin>>num2;
cout<<"You entered "<<num1<<" and "<<num2<<"."<<endl;
}

Related

C++ Loop back beginning if the result is false

I am very new to C++ and still studying. The below scenario just came to my mind and I was trying to figure it out how to do this.
Scenario is as below:-
User inputs a number
then I store it in x
next is to check whether the input number is an int or float
if int, then pop up a message "Entered Number is not a Decimal Number" and go back to the beginning and inform the user to re-enter a number
if the entered number is float then I round it to the nearest int and pop up a message cout<<"Nearst Rounded Number is : "<<round(x)<<endl;
I assume this can be done with a loop, but I cannot figure it out.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x,y;
cout<<"Enter Number Here : ";
cin>>x;
{
if ( (x- int(x) == 0))
cout<<"Entered Number is not a Decimal Number"<<endl<<endl;
else
cout<<endl;
cout<<"Nearst Rounded Number is : "<<round(x)<<endl;
}
}
while(true) {
//(your existing code goes here)
cout << "Do you want to run the program again either types yes or no" << endl;
cin >> answer;
if (answer != 'yes' && answer != 'Yes')
break;
}
It should work. Else you can keep one bool variable in if else part and validate them below and break the while loop until your condition satisfy.
I believe this is what you wanted:
int main() {
string input_str;
float input_float = 0;
while (true) {
cout << "Enter number here: ";
cin >> input_str;
input_float = stof(input_str); //stof converts string to float
if (input_float != 0) {
if (input_float == int(input_float)) cout << "Entered number is not a decimal, please try again." << endl;
else break;
}
//TODO: Catch any exceptions thrown by stof (Ex: User inputs a letter)
}
cout << "Nearest Rounded number is: " << round(input_float)<<endl;
return 0;
}
Bye!
Edit: Changed the code a bit and removed a bug.
I have changed your code a bit to take input continuously. while(cin>>x) means the program is taking input constiniously until EOF. Then when you find a decimal number, it breaks.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x,y;
cout<<"Enter Number Here : ";
while(cin>>x)
{
if ( (x- int(x) == 0))
cout<<"Entered Number is not a Decimal Number"<<endl<<"Enter Number Here : ";
else
{
cout<<"Nearst Rounded Number is : "<<round(x)<<endl;
break;
}
}
}
By the way I will advise you to spend a bit more time to find out the solution on your own before posting here.
first of all Why is “using namespace std;” considered bad practice?
second - use a loop with a boolean flag to indicate when you want the exit the loop
#include <iostream>
#include <cmath>
int main()
{
float x,y;
bool flag = true;
while(flag)
{
std::cout<<"Enter Number Here : ";
std::cin>>x;
{
if ( (x- int(x) == 0))
std::cout<<"Entered Number is not a Decimal Number"<<std::endl;
else{
std::cout<<std::endl;
std::cout<<"Nearst Rounded Number is : "<<round(x)<<std::endl;
flag = false;
}
}
}
}
Your code needs improvements like indentations and the use of loop conditions otherwise your program will not rerun again.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x,y;
bool correctinputFlag=false;
do()
{
cout<<"Enter Number Here : ";
cin>>x;
if ( (x- int(x) == 0))
{
cout<<"Entered Number is not a Decimal Number"<<endl<<endl;
}
else
{
cout<<endl;
cout<<"Nearst Rounded Number is : "<<round(x)<<endl;
correctinputFlag=true;
}
}while(correctinputFlag==false);
}

Program finding product of two numbers

In this program what is the null case condition I have to put into it when the input is null so that the program in this case
//*product of two no.s*//
#include<iostream>
using namespace std;
int main()
{
double firstnumber,secondnumber,productoftwonumbers;
cout<< "Enter two numbers :";
cin>> firstnumber>>secondnumber;
productoftwonumbers=firstnumber*secondnumber;
cout<< "Product ="<<productoftwonumbers<<endl;
return 0;
}
If what your asking is finding out if there are no 0's in the input then here's the solution:
Add this after cin>> firstnumber>>secondnumber;
if(firstnumber == 0 || secondnumber == 0) return 1;
If you're looking for something else we need a better explanation.

I need to limit my program not to accept any decimal values in my program

I am instructed that I have to reject any decimal and I need to re enter the number again.I tried this code but still it just goes to the whole process before acknowledging the error. Try the program and judge me :D here's my code:
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<limits>
using namespace std;
int getInt()
{
int m=0;
while (!(cin >> m))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Please input a proper 'whole' number: " ;
}
return (m);
}
int main()
{
double x;
int q,w,e,choice;
cout<<"Welcome! This program will sort out the integers you will input!\nPlease input number of integers: ";
cin>>q;
cout<<endl<<endl;
int* inc= new int[q];
int* dec= new int[q];
for(int p=1;p<=q;++p)
{
w=p;
e=p;
cout<<"Input integer number "<<p<<": ";
x =getInt();
while(e>0 && inc[e-1]>x)
{
inc[e]=inc[e-1];
e--;
}
while(w>0 && dec[w-1]<x)
{
dec[w]=dec[w-1];
w--;
}
inc[e]=x;
dec[w]=x;
}
cout<<endl;
cout<<"What order do you prefer? Input 1 for increasing and 2 if decreasing.\nChoice: ";
cin>>choice;
while(choice<1 || choice>2)
{
cout<<"Please input a correct choice! Try again!\nChoice: ";
cin>>choice;
}
if(choice==1)
{
for(int i=0;i<q;++i)
cout<<inc[i]<<"\t";
cout<<endl;
}
else
{
for(int i=1;i<=q;++i)
cout<<dec[i]<<"\t";
cout<<endl;
}
system("PAUSE");
}
hoping for your help :)
You can try using modulo.
Just an idea, hope it helps.
bool flag = false;
While (flag == false){
cin>>number;
if ((number % 1) != 0){
flag = false;
}
else{
flag = true;
}
cin.clear();
}
Try making a copy of the number you want to test and casting it to an int and then back to a double, and then check for equality. If they are equal, you have an int, if they are not, you have a decimal:
#include <iostream>
using namespace std;
int main()
{
double a = 5;
double c = 5.5;
double b = a;
bool test1 = (double)((int)b) == a; //true!
b = c;
bool test2 = (double)((int)b) == c; //false!
cout << test1 << endl;
cout << test2 << endl;
return 0;
}
Wrote this answer a long time ago, it is very hacky and will not work on all inputs. Use std::stoi and check if it throws as the comment suggests instead.

Why does this go into an infinite loop on not giving integer input?

#include <iostream>
using namespace std;
int main (void)
{
int n;
label0:
cout<<"Please an integer value\n";
cin>>n;
if ( cin.fail() )
{
cout<<"Please enter proper value\n";
cin.clear();
goto label0;
}
else
{
cout<<"You entered"<<n<<"\n";
}
return 0;
}
It works perfectly on giving an integer input but when I enter a non integer input, why does it go in an infinite loop?

How do we break the loop without adding the sentinel to the average?

As the code stands now, the sentinel is being included in the calculation for the average. Any pointers on how to break the loop without including the sentinel?
#include <iostream>
using namespace std;
int main ()
{
int fahr=0,cent=0,count=0,fav=0;
while (fahr!=-9999)
{
count ++;
cout<<"Input the fahrenheit temp to be converted to centigrade or enter -9999 "<<endl;
cin>>fahr;
cent=(float)(5./9.)*(fahr-32);
cout<<"The inputed fahr "<<fahr<<endl;
cout<<"The cent equivalent "<<cent<<endl;
}
fav=(float)(fav+fahr)/count;
cout<<"Average"<<fav<<endl;
return 0;
}
Make the code run in an infinite loop, and use break to terminate the loop if you see -9999.
#include <iostream>
using namespace std;
int main ()
{
int fahr=0,cent=0,count=0,fav=0;
while (true)
{
count ++;
cout<<"Input the fahrenheit temp to be converted to centigrade or enter -9999 "<<endl;
cin>>fahr;
if (fahr == -9999)
break;
cent=(float)(5./9.)*(fahr-32);
cout<<"The inputed fahr "<<fahr<<endl;
cout<<"The cent equivalent "<<cent<<endl;
}
fav=(float)(fav+fahr)/count;
cout<<"Average"<<fav<<endl;
return 0;
}
Probably you need to add one more line after
cout<<"The cent equivalent "<<cent<<endl;
add:
fav += cent;
and change
fav=(float)(fav+fahr)/count;
to:
fav=(float)fav/count;