Thread: if and else statement question

1. if and else statement question

I was assigned to input a 4 digit by the user and output each number in every line. My question is in the code as comments that follows:

Code:
```#include <iostream>
#include <string>

using namespace std;

void a();
void b();
void c();
void d();

bool die( const string & msg );

int main(){

a();
b();
c();
d();

}

void a(){
unsigned noko1;
cin >> noko1;
if( !cin )  die( "non-numeric input" ); // if person inputs a non digit value complain and die
if( noko1 <= 0 || noko1 > 9999 ) // this states the range beetween 1 and 1000 if not it dies
die( "out of range");

if(noko1 == noko1)

cout<<(noko1%10000)/1000<<endl;
cout<<(noko1%1000)/100<<endl;
cout<<(noko1%100)/10<<endl;
cout<<(noko1%10)/1<<endl;

// else what?? if i input else <<cout"do nothing" endl;
// sends me an error
// is it ok to leave it like this without the else? thanks

}    //  a

void b(){

}    //  b

void c(){
}    //  c

void d(){
}    //  d

bool die( const string & msg ){
cerr <<endl <<"Fatal error: " <<msg <<endl;
exit( EXIT_FAILURE );
}   //  die```

2. This statement:
Code:
` if(noko1 == noko1)`
Will always evaluate to true. What do you even have it?

3. else isn't compulsory, if that is what you're asking.

4. if and else statement question

Originally Posted by elpedoloco
Code:
```	if(noko1 == noko1)

cout<<(noko1%10000)/1000<<endl;
cout<<(noko1%1000)/100<<endl;
cout<<(noko1%100)/10<<endl;
cout<<(noko1%10)/1<<endl;```
You may have tabbed those lines out a bit, but that doesn't mean that they are all only executed when the if-statement is true. To make that happen, you need to put curly brackets around all of those statements. The lack of these brackets is also why you are getting an error when trying to put an else in there.

You're going to need to fix that tautology though, before your program will do whatever you meant it to do.

5. thanks I am working on 5 different programs I've fixed most of my code I will post once I am done so I can get your opinion, as always, your help is appreciated.