if and else statement question

This is a discussion on if and else statement question within the C++ Programming forums, part of the General Programming Boards category; I was assigned to input a 4 digit by the user and output each number in every line. My question ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    11

    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. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    This statement:
    Code:
     if(noko1 == noko1)
    Will always evaluate to true. What do you even have it?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    else isn't compulsory, if that is what you're asking.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    if and else statement question

    Quote Originally Posted by elpedoloco View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about if statement
    By 127.0.0.1 in forum C Programming
    Replies: 2
    Last Post: 03-13-2011, 08:29 AM
  2. A question on what i think is my if statement
    By brian75 in forum C++ Programming
    Replies: 8
    Last Post: 12-17-2008, 05:04 PM
  3. If Statement Question
    By thekautz in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2008, 03:06 PM
  4. Question about if-statement.
    By omnificient in forum C Programming
    Replies: 11
    Last Post: 12-21-2007, 02:54 PM
  5. if statement question
    By unclebob in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2006, 08:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21