|
You last visited: Today at 14:15
Advertisement
cin C++ If Statement
Discussion on cin C++ If Statement within the C/C++ forum part of the Coders Den category.
06/08/2012, 19:02
|
#1
|
elite*gold: 0
Join Date: Jun 2011
Posts: 80
Received Thanks: 0
|
cin C++ If Statement
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
|
#2
|
elite*gold: 0
Join Date: May 2009
Posts: 827
Received Thanks: 471
|
Please please use the Code-Tags..
|
|
|
06/08/2012, 19:22
|
#3
|
elite*gold: 0
Join Date: Jun 2011
Posts: 80
Received Thanks: 0
|
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
|
#4
|
elite*gold: 0
Join Date: May 2009
Posts: 827
Received Thanks: 471
|
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:
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
|
#5
|
elite*gold: 0
Join Date: Jun 2011
Posts: 80
Received Thanks: 0
|
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
and
instead...?
What would it check then?
|
|
|
06/08/2012, 19:49
|
#6
|
elite*gold: 0
Join Date: May 2009
Posts: 827
Received Thanks: 471
|
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
|
#7
|
elite*gold: 0
Join Date: Jun 2011
Posts: 80
Received Thanks: 0
|
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
|
#8
|
elite*gold: 0
Join Date: Feb 2012
Posts: 115
Received Thanks: 18
|
Quote:
Originally Posted by Dr. Shithead
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
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
|
#9
|
elite*gold: 0
Join Date: Jun 2011
Posts: 80
Received Thanks: 0
|
Quote:
Originally Posted by 2n0w
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
|
#10
|
elite*gold: 273
Join Date: Sep 2010
Posts: 1,831
Received Thanks: 786
|
Quote:
Originally Posted by 2n0w
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
|
#11
|
elite*gold: 0
Join Date: Jun 2011
Posts: 80
Received Thanks: 0
|
Thanks im a little bit confused but i think i got it^^ Thanks a lot buddies^^
|
|
|
06/09/2012, 09:59
|
#12
|
elite*gold: 0
Join Date: Feb 2012
Posts: 115
Received Thanks: 18
|
Quote:
Originally Posted by .SkyneT.
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
|
#13
|
elite*gold: 273
Join Date: Sep 2010
Posts: 1,831
Received Thanks: 786
|
Quote:
Originally Posted by 2n0w
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:
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
|
#14
|
elite*gold: 0
Join Date: Feb 2012
Posts: 115
Received Thanks: 18
|
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  First I really thought it would return integers (fail xD), but not var ^^
|
|
|
06/10/2012, 11:17
|
#15
|
elite*gold: 273
Join Date: Sep 2010
Posts: 1,831
Received Thanks: 786
|
Quote:
Originally Posted by 2n0w
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
|
|
|
 |
Similar Threads
|
Statement: Hardware-ID!
01/28/2011 - Main - 17 Replies
Da in der Shoutbox und auch hier in der Section ziemlich viel gefragt wird, was denn nun die "Hardware-ID" ist, die man im Profilfeld "Hardware-ID" eingegeben kann, dachte ich mir, ich erkläre es euch mal.
Die Funktion ist noch nicht komplett fertig, daher bringt es euch noch nichts.
Lasst das Feld einfach leer oder schreibt solange einen Random MD5 Hash rein.
Später wird es ein Programm geben, welches euch eure Hardware-ID anzeigt.
Diese wird nach einem eigenen Algorithmus erstellt....
|
Wir wollen STATEMENT!!
03/07/2010 - Metin2 Private Server - 32 Replies
Leute
wie ihr bestimmt bemerkt habt wurden in letzter zeit sehr viele thereads über FINALMT2 geöffnet.
Die Hauptursache dafür ist das der Server nicht Online ist.
Aber es gibt noch wichtigere gründe.
Die Admins haben mit absicht den Forum gesperrt damit wir nur in e*pvp spammen können und nicht in unserem forum.
|
Statement FinalMt2
03/07/2010 - Metin2 Private Server - 2 Replies
Hallo Leute gute Neuigkeiten :)
Soeben hat Infinity im TS gesagt ,dass der Server läuft und sie nur denClienten Uploaden müssen. Der Server wird um ca. 15:30 bis 16 uhr erreichbar sein. FinalMt2 hat eine neue Homepage ist wie die ganz alte nur mit .net hinten dran =) Ich bitte euch keine Neuen Threads zu eröffnen und einen MOD bitte den Thread hier zu closen damit kein Spam entsteht.Bei weiteren Fragen am besten auf den TS3 Server kommen. Die daten zu ihm findet ihr auf den neuen Hp.
...
|
[Statement]
09/05/2009 - Metin2 Private Server - 10 Replies
Hi
mein root sollte ja eigentlich seit 0.15uhr laufen , aber das tut er nicht wegen problemen. Der Root läuft schon aber hält soviel auslastung von 75 spielern nicht aus.(hab klump gekauft) Da ich nicht bereit bin einen neuen root zu kaufen werde ich den server starten, KEINE regiseite erstellen und ihn "non-pub" lassen und nur spieler von euch die mir sympatisch wirken darauf spielen lassen
mfg
Homer12345
Garuga bitte gib mir keine verwarnung wegen "spamen", denn ich will nicht 87 PNs...
|
All times are GMT +1. The time now is 14:16.
|
|