I should make a single thread so I can post all my questions, lol.
So I did an excersis out of the book and it worked well. It's a program that asks the user to input three number, then the program orders it from least to greatest.
I decided to add an if statement so incase the user inputs the same number for the three variable, it'll prompt the user to input another three numbers. I also added a while loop so this would be possible, because I didn't add that bit until the last few lines of the program. I included a break so it wouldn't be an infinite loop.
Now I'm trying to add another if statement incase the user enters the wrong data type (letter instead of a number). The problem is, I'm not sure how to compare data types. I've tried using a bool data type and adding the variables, so if it comes out to false, the clear function would activate and have the user input numbers again. Here's the program, and sorry for all the braces. I'll highlight the problem in red.
I was thinking of adding something like:
bool check;
check= ( (a+b+c)==int) );
if (check)
cin.clear();
.
.
.
or:
if ( (a+b+c)==int) );
cin.clear();
.
.
.
I'd appreciate any sort of help.
So I did an excersis out of the book and it worked well. It's a program that asks the user to input three number, then the program orders it from least to greatest.
I decided to add an if statement so incase the user inputs the same number for the three variable, it'll prompt the user to input another three numbers. I also added a while loop so this would be possible, because I didn't add that bit until the last few lines of the program. I included a break so it wouldn't be an infinite loop.
Now I'm trying to add another if statement incase the user enters the wrong data type (letter instead of a number). The problem is, I'm not sure how to compare data types. I've tried using a bool data type and adding the variables, so if it comes out to false, the clear function would activate and have the user input numbers again. Here's the program, and sorry for all the braces. I'll highlight the problem in red.
Code:
//This program will ask the user to input three number and ordered from least to greatest
#include <iostream>
using namespace std;
int main()
{
double a,b,c,check;
[COLOR="Red"]a=0, b=0, c=0;[/COLOR]
cout<<"Enter two numbers. They will be ordered from least to greatest."<<endl;
cin>>a>>b>>c;
[COLOR="red"] check = a+b+c;
bool clear;
clear = check;[/COLOR]
while (cin) //as long as there is input, the loop will begin
{
[COLOR="red"] {
if (!clear)
{
cin.clear();
cout<<"You can't enter letters. Enter three numbers"<<endl;
cin>>a>>b>>c;
}
}[/COLOR]
if (a>b && a>c)
{
if (b>c)
cout<<"The numbers, from least to greatest are "<<c<<" "<<b<<" "<<a<<endl;
else
cout<<"The numbers, from least to greatest are "<<b<<" "<<c<<" "<<a<<endl;
break; //If a>b>c || a>c>b, break will end the loop
}
if (b>a && b>c)
{
if (a>c)
cout<<"The numbers, from least to greatest are "<<c<<" "<<a<<" "<<b<<endl;
else
cout<<"The numbers, from least to greatest are "<<a<<" "<<c<<" "<<b<<endl;
break; //If this is the situation, the program will end the loop
}
if (c>a && c>b)
{
if (a>b)
cout<<"The numbers, from least to greatest are "<<b<<" "<<a<<" "<<c<<endl;
else
cout<<"The numbers, from least to greatest are "<<a<<" "<<b<<" "<<c<<endl;
break; //If this is the situation, the program will end the loop
}
if (a==b && b==c) //Incase alike numbers are inputted
{
cout<<"The numbers are the same, enter them again."<<endl;
cin>>a>>b>>c; //Reinput the numbers and start the loop over again
}
}
system("pause");
return 0;
}
I was thinking of adding something like:
bool check;
check= ( (a+b+c)==int) );
if (check)
cin.clear();
.
.
.
or:
if ( (a+b+c)==int) );
cin.clear();
.
.
.
I'd appreciate any sort of help.
Last edited: