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.
Related
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);
}
This question already has answers here:
How to make cin take only numbers
(2 answers)
Closed 2 years ago.
I want to block all the letters for input in the following code, can you help me with that?
#include <iostream>
using namespace std;
int main()
{
cout<<"To close this program you need to type in -1 for the first input"<<endl;
int m, n;
do{
int counter1 = 0;
int counter2 = 0;
cout<<"Now you need to input two seperate natural numbers, and after that it calculates the difference of both numbers factors!"<<endl;
cout<<"First Input"<<endl;
cin>>m;
if(m==-1){
break;
}
cout<<"Second Input"<<endl;
cin>>n;
if(m<0 or n<0){
cout<<"ERROR - Only natural numbers are allowed!"<<endl;
}
else{
...
The rest of the program is just the math.
When you declare a type of a variable, the variable can't contain anything else than what you have declared. So: You can’t use an int m to input a float. You could however use cin.ignore() (more details here) to accept a user input of "4.1" as "4". Here you go:
#include <iostream>
#include <limits>
using namespace std;
int main() {
cout << "Enter an int: ";
int m = 0;
while(!(cin >> m)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input!\nEnter an int: ";
}
cout << "You enterd: " << m << endl;
}
I need to be able to input a series of integers on the same line, I cannot make them characters because I need to add the integers up.
#include <iostream>
#include<string>
using namespace std;
int main(){
int a; //1
int b; //2
bool sit = true;
cout << "Enter a ten digit date" <<endl;
cin>>a>>b;
cout<<a<<b<<endl;
if (sit == true){
b = b+a;
cout << b<<endl;
}
return 0;
}
So if I enter
12
it waits till I enter another two digits then adds both two digit numbers.
12
45
57
What can I do?
Thanks
i dont quite understand your problem , but this is how you read a series of integers .
int x;
cin>>x;
//keep reading x until 0 is entered
while(x!=0)
{
//do whatever you need here
cin>>x;
}
if you knew how many integers you had to read , you could use a for loop.
for (int i=0;i<n;i++)
{
cin>>number;
}
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;
}
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
void addRecordstoFile();
void readRecordsFromFile();
int main(){
addRecordstoFile();
readRecordsFromFile();
}
void addRecordstoFile(){
This adds the integers to the file but I want when it exits (when the user enters -1) not be displayed in the .txt file
int num=1;
char response;
ofstream outFile("numbers.txt",ios::out);
if (!outFile){
cerr<<"File was not opened correctly"<<endl;
exit(1);
}
cout<<"\t\t\tSurvey Response Analyzer"<<endl;
while(num!=-1){
cout<<"\n Enter a survey response from 1 to 10 (-1 to end): ";
cin>>num;
if(num<1 || num>10){
cout<<"\n Please enter a valid response from (1-10): ";
cin >>num;
}
outFile << num <<" ";
}
outFile.close();
cout<<"Saved to file..."<<endl;
system("pause");
}
This is where the major problem lies, I should extract the integers from the text file and place them in an array of counters to count the number of times the user enterred a number between 1 and 10. I am completely lost because I would need the size of the array to determine my loop.
void readRecordsFromFile(){
ifstream inFile("numbers.txt",ios::in);
if(!inFile){
cerr<<"File was not found";
exit (1);
}
How can I make an array of counters here without knowing the size of the array from the text file?
1. First query:
This adds the integers to the file but I want when it exits (when the
user enters -1) not be displayed in the .txt file
Replace
if (num < 1 || num > 10) {
cout<<"\n Please enter a valid response from (1-10): ";
cin >>num;
}
outFile << num <<" ";
with
if (num < 1 || num > 10) {
cout<<"\n Please enter a valid response from (1-10): ";
cin >>num;
} else {
outFile << num <<" ";
}
2. Second query:
I would need the size of the array to determine my loop
No you don't. You may use a while loop to check the end.
1st thing first, in your void addRecordstoFile() function, you could design your while loop as,
while(num!=-1){
cout<<"\n Enter a survey response from 1 to 10 (-1 to end): ";
cin>>num;
if(num<1 || num>10){
cout<<"\n Please enter a valid response from (1-10) ";
}
else{
outFile << num <<" ";
}
}
Next, for your void readRecordsFromFile() function, if your ultimate goal is only to count the number of appearances of the numbers entered, then you could use a 10 element counter array. and while reading from the file any digit you got, you just increase the element at the particular index of the counter array. say, you read 3 from the file, so you will just increase counter[3] as counter[3]++ to count the appearance of 3 in the file. Think about it.
while(/*end of file condition*/){
int a;
inFile>>a;
counter[a]++;
}
Hope it helped...
Well, the good news is you have tons of options to fix your issues. Here are some suggestions:
In order to avoid having the '-1' in the .txt file, I might change your loop to look like this:
while(num!=-1){
cout<<"\n Enter a survey response from 1 to 10 (-1 to end): ";
cin>>num;
if (num == -1) break; // will break out of loop before adding num!
if (num>0 && num<=10) outFile << num <<" "; // add only a valid number
// Note: if number is not 1 thru 10 or -1, loop simply repeats!
}
As for your second function, you have a variety of options, but I would suggest using a vector to contain the numbers. To use vectors, you need the library:
#include <vector>
Vectors are dynamic arrays -- you can add or delete elements to them as you wish. Your function might look like this:
void readRecordsFromFile(){
ifstream inFile("numbers.txt",ios::in);
if(!inFile){
cerr<<"File was not found";
exit (1);
}
vector <int> vNums; // a container for your int values
int inNum; // hold int for loading into vector
// Read each number to inNum, then add it to the vector
while (!inFile.eof()) {
inFile >> inNum; // load your input
vNums.push_back(inNum); // "push_back" adds an element
}
// Output the numbers... you may want to use a vector iterator for this
int x = 0;
while (x < vNums.size() ) {
cout << x+1 << " - " << vNums[x] << "\n";
x++;
}
}
Another option would be to use dynamic memory (new / delete), with linked lists. Here's a tutorial:
http://www.cprogramming.com/tutorial/lesson15.html
I think using a vector is a better solution, though! Best of luck.
return static_cast<double>(numerator/denominator);
Probably you prefer to cast each variable to double first and divide later. This is the difference:
If int: 2/4 == 0, so that static_cast<double>(numerator/denominator) returns 0.
If double (or float): 2./4. == 0.5, so that static_cast<double>(numerator) / static_cast<double>(denominator) returns 0.5.