cin C++ If Statement

06/08/2012 19:02 Dr. Shithead#1
Hey, im about to learn C++ : D
Now i am in the Chapter where i learn about cin...
(Yea i am not that far atm^^)
So i did everything exactly like it is written in the book:

Code:
// cin1.cpp   // Filename
#include <iostream>  //including the dll? Biblothek? iostream
using namespace std;  /* The following content uses The Namespace std, else we would have to wirte std::cout everytime */

int main(void) { /*Main Function, which has to be a number (int=integer) (i forgot what void meant would be nice if someone could explain it fast) */
    float wert; /* defines the Variable wert as a float (means a number with decimal point) */
    cout << "Bitte eine Fließkommazahl eingeben : "; /*writes the Text between the "*" out and then rests there */ 
    cin >> wert;  /* now it waits there for an input with cin for the variable of wert. It already checked that wert has to be a float or it will error */

    if (!(cin >> wert)) { /*New If stance where it checks something would be nice if you would explain it to me i dont entirly got it */
        cerr << "Fehler bei der Eingabe!\n"; /* it checked if cin is a float or not (whats the matter with the "!" ??  what does "cin >> wert" do? 
So atm it works if i wirte something else then a floatnumber it says : "Fehler bei der Eingabe!" Which means something like: "Wrong Input". So it checks if its a floutnumber and if not it says yo its wrong buddy*/
    }
    else { /* else statement beginnning */
    cout << "Die Eingabe war " << wert << "\n"; /* Gives out: "Die Eingabe war " which means something like: "Input was: ", if it is a floatnumber (was checked in IF statement). And after that it gives out the variable: "wert" which must be a floatnumber at this point. */
    }
    return 0; /*Ends the Process */
    }
So it works kinda fine yea
Would be nice if you would correct any faults.
My Problem is: I dont entirly get the If statement would be nice if you would explain it to me... and my second problem is:
"It now shows please insert a floutnumber: "
if u wirte anything else then a number it says: "Wrong Input" thats fine...
But if i write 5.5 for example it jumps to the next line but does nothing.
Then if i write it again it gives the wished "Input was: " + Variable Wert.
But why do i have to enter it 2 times????
I dont got it please help^^
06/08/2012 19:10 xNopex#2
Please please use the Code-Tags..
06/08/2012 19:22 Dr. Shithead#3
I would if you explain me how? im not the best one im really sorry... oO Would u please explain it to me?
06/08/2012 19:30 xNopex#4
Surround your code with [C O D E][/C O D E] without the blanks..

B2T:
Quote:
Main Function, which has to be a number (int=integer) (i forgot what void meant would be nice if someone could explain it fast
In this case 'void' means that there will no parameter be passed to the function. Unlike in C this is not necessary in C++. So optionally you can leave the brackets blank, too.

Quote:
if (!(cin >> wert))
The cin-expressions returns a integer value of zero, if it failed and something unequal to zero if it was successfull. A value of zero is interpreted as 'false' and something unequals to zero is interpreted as 'true'. So instead of if(!(cin>>blabla)) you can also say: if((cin>>blabla)==0) or if((cin>>blabla)==false).

Sorry for my English..
06/08/2012 19:40 Dr. Shithead#5
So That Means that ! just checks if it is an integer and thats because the main function (which gets no information (thats the void, else it would be a variable name written there?)) is an int function?

So if it wouldnt be
Code:
int main(void) {
and
Code:
main(void) {
instead...?
What would it check then?
06/08/2012 19:49 xNopex#6
O,ô ehm no?

the '!' is the boolean NOT. !true = false; !false = true. Now you have to know that in C++ zero is the same like false and something unequal to zero is the same like true. The cin expression gets some input from the console and returns some integer value (0 if cin failed or something unequal to 0 if it was successfull). So you can also say cin returns true if it was successfull and false if it failed:

Code:
float v;
bool bCin = cin >> v;
if(bCin == false) {
    cout << "FAIL";
}

Secondly:
int main(void) is equal to int main() [ONLY in C++ not in C!).
06/08/2012 20:06 Dr. Shithead#7
So int just means it returns wether a number if its like: 5.32 oing
it would return: 5.32
and if its no number but just: oing
then it would return 0
which means ! just checks if it is a 0? So "!" just always checks is it the correct thing or not and cause of the float before variable wert it checks if its a float and cause of the 0 which is saved instead of a number it says no its no floatnumber... Is it now correct?

And other question why do i have to enter a floatnumber, press enter, enter it again and press enter again for it to work?

Thanks By the Way^^
06/08/2012 20:45 2n0w#8
Quote:
Originally Posted by Dr. Shithead View Post
And other question why do i have to enter a floatnumber, press enter, enter it again and press enter again for it to work?
Because you use cin twice if you're doing it like this:
Code:
cin >> var; //doesn't save return value
if(!cin >> var) //uses cin again, and compares to return value
I think you want something like this:
Code:
boolean flag;
flag = cin >> var; //saves return value in flag
if(!flag) //tests if flag is false
Quote:
Originally Posted by Dr. Shithead View Post
So int just means it returns wether a number if its like: 5.32 oing
it would return: 5.32
and if its no number but just: oing
then it would return 0
"5.32 oing" is only correct because of the blank, I think, which does the same as newline. (I don't know exactly, maybe I'm mixing that up with java)
It doesn't return 5.32, it moves it in var/wert. How should it? It can only return integers.
0 on failure, value different from 0 on error.
06/08/2012 21:07 Dr. Shithead#9
Quote:
Originally Posted by 2n0w View Post
Code:
boolean flag;
flag = cin >> var; //saves return value in flag
if(!flag) //tests if flag is false
Thanks first
but in your code
Code:
flag = cin >> var;
if(!flag) {
....
}
that would mean
flag = cin >> var;
would test if var is a float...
but why does it test that?
i mean boolean flag says that flag is "True" or "False"
okay
but how does it decide if True or False?
I mean floats are True and chars are False...
or ????
and if ! just tests if its the negative
so ! tests if its False or am i too confused?^^

Thanks Thanks Thanks btw^^
06/08/2012 21:09 .SkyneT.#10
Quote:
Originally Posted by 2n0w View Post
I think you want something like this:
Code:
boolean flag;
flag = cin >> var; //saves return value in flag
if(!flag) //tests if flag is false
In c/c++ isnt anything like "boolean", only bool and BOOL.
btw flag isn´t necessary at all...

And this here:
Code:
flag = cin >> var; //saves return value in flag
is just totally wrong.
the return value of cin isnt var, it returns an istream object...

here some working code:
Code:
float var;
std::cin >> var;
if (!var)
{
    std::cout << "var = false";
}

//same as:
float var;
std::cin >> var;
if (var == false)
{
    std::cout << "var = false";
}
Edit:
Quote:
that would mean
flag = cin >> var;
would test if var is a float...
It doesnt test anything, this code would
save the input to var, and save an istream
object to flag (you dont have to understand this,
because the code is wrong)

Quote:
but why does it test that?
i mean boolean flag says that flag is "True" or "False"
okay
but how does it decide if True or False?
I mean floats are True and chars are False...
or ????
True = NOT zero
not more and not less.



Quote:
and if ! just tests if its the negative
so ! tests if its False or am i too confused?^^
if (!var) , tests if var is equal to zero (false).
so its right what you wrote.
06/08/2012 21:42 Dr. Shithead#11
Thanks im a little bit confused but i think i got it^^ Thanks a lot buddies^^
06/09/2012 09:59 2n0w#12
Quote:
Originally Posted by .SkyneT. View Post
here some working code:
Code:
float var;
std::cin >> var;
if (!var)
{
    std::cout << "var = false";
}
This doesn't test if a valid float value was entered, just if 0 was entered.
Thanks anyway, I now realized that ! has nothing to do with the return value, but calls a method of the return value(the istream object):
bool operator ! () const;
equals
bool fail () const;
I hope this is correct now ^^
Code:
	
 float val;
 bool flag;
 flag = !(std::cin >> val); //equals flag = (std::cin >> val).fail();
 //also possible
 //std::cin >> val;
 //flag  = !std::cin; or flag = std::cin.fail();
 if(flag)
 {
    std::cout << "Fail" <<std::endl;
 }
That means, ! in this case means a method call instead of a negation.
Short version:
Code:
float val;
if(!(std::cin >> val)) //or if((std::cin >> val).fail())
06/09/2012 12:17 .SkyneT.#13
Quote:
Originally Posted by 2n0w View Post
Thanks anyway, I now realized that ! has nothing to do with the return value, but calls a method of the return value(the istream object):
bool operator ! () const;
equals
bool fail () const;
I hope this is correct now ^^
Code:
	
 float val;
 bool flag;
 flag = !(std::cin >> val); //equals flag = (std::cin >> val).fail();
 //also possible
 //std::cin >> val;
 //flag  = !std::cin; or flag = std::cin.fail();
 if(flag)
 {
    std::cout << "Fail" <<std::endl;
 }
That means, ! in this case means a method call instead of a negation.
Short version:
Code:
float val;
if(!(std::cin >> val)) //or if((std::cin >> val).fail())
No!

An Example:
Code:
float var;
std::cout << (std::cin >> var);
If its correct what you write the Output should be var.
But what happens is this:
[Only registered and activated users can see links. Click Here To Register...]

There is only one right way to do that:
Code:
float var; 
std::cin >> var; //save the input to var !  "var = std::cin >> var" would save something else...
if (!var)  //Tests if var is equal to zero
{
     std::cout << "var = 0 (false)"
}

//Now the wrong example:
float var; 
if (!(std::cin >> var))  //Tests if the return of cin is equal to zero...
{
     std::cout << "return of std::cin = 0 (false)"
}
I hope you can understand the point.


Quote:
This doesn't test if a valid float value was entered, just if 0 was entered.
The Input is always a valid value. Maybe its a not expected one, but it is one :)

Edit:
Code:
//Example for unexpected input
	float var, var_2;

	std::cin >> var;
	std::cin >> var_2;

	std::cout << std::endl << var << " " << var_2;
//Run...
Input 1: "1 text"
Input 2: takes "text" from the input buffer and saves it to var_2 (not wanted)

//Another example:
	float var, var_2;

	std::cin >> var;
	fflush(stdin);  //flush the input buffer

	std::cin >> var_2;
        fflush(stdin);

	std::cout << std::endl << var << " " << var_2;
//Run..
Input 1: "5 text"
-> whole input buffer gets deleted, so the user is able to write another value
Input 2: "6"

Output: "56"
06/10/2012 10:21 2n0w#14
Sry, I still don't get why this should be wrong:
Code:
float var;
std::cin >> var;
//Input: g, failbit is set because of type mismatch, if 456 was entered failbit wouldn't be set.
if(!std::cin) //equals cin.fail() -> true

//Same
float var;
if(!(std::cin >> var))
Quote:
If its correct what you write the Output should be var.
? The output is an istream object, thus it is the adress of it (guess so ^^).
bool operator ! () const;
Operator ! on istream object does return a bool value => overloaded operator.

Edit:
Ok :D First I really thought it would return integers (fail xD), but not var ^^
06/10/2012 11:17 .SkyneT.#15
Quote:
Originally Posted by 2n0w View Post
Sry, I still don't get why this should be wrong:
Code:
float var;
std::cin >> var;
//Input: g, failbit is set because of type mismatch, if 456 was entered failbit wouldn't be set.
if(!std::cin) //equals cin.fail() -> true

//Same
float var;
if(!(std::cin >> var))

? The output is an istream object, thus it is the adress of it (guess so ^^).
bool operator ! () const;
Operator ! on istream object does return a bool value => overloaded operator.
Ah, I was on the wrong way ...
Thought you think that (std::cin >> var) returns var :D