Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

my code isnt working


  • Please log in to reply

#1
lilhawk2892

lilhawk2892

    Member

  • Member
  • PipPipPip
  • 128 posts
wow this forum is so great anyways
my code isnt working the code on itself is working but the part where it says if your 50 i added so tell me why wont it wokr im using dev-c++
#include <iostream>	

using namespace std;
  
int main()                            // Most important part of the program!
{
  int age;                            // Need a variable...
  
  cout<<"Please input your age: ";    // Asks for age
  cin>> age;                          // The input is put in age
  cin.ignore();                       // Throw away enter
  if ( age < 100 ) {                  // If the age is less than 100
     cout<<"You are pretty young!\n"; // Just to show you it works...
  }
  else if ( age == 100 ) {            // I use else just to show an example 
     cout<<"You are old\n";           // Just to show you it works...
  }
  else {
    cout<<"You are really old\n";     // Executed if no other statement is
  }
  else if ( age == 50 ) {
    Cout<<"live your life now\n";
 }
 else {
      cout<<"live your life now\n";
}
  cin.get();
}

Edited by lilhawk2892, 16 August 2005 - 12:02 AM.

  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
else {
   cout<<"You are really old\n";     // Executed if no other statement is
 }
 else if ( age == 50 ) {
   Cout<<"live your life now\n";
}

try this
else if ( age == 50 ) {
Cout<<"live your life now\n";
}
else {
cout<<"You are really old\n"; // Executed if no other statement is
}

do you understand the difference?

Edited by bdlt, 16 August 2005 - 12:13 AM.

  • 0

#3
lilhawk2892

lilhawk2892

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 128 posts
im sorry i had that before it still didnt work
  • 0

#4
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
the following is executed before the == 50 code.
if ( age < 100 ) {                  // If the age is less than 100
    cout<<"You are pretty young!\n"; // Just to show you it works...
 }

try this:
if( age == 50 ) {
Cout<<"live your life now\n";
}
else if ( age == 100 ) { // I use else just to show an example
cout<<"You are old\n"; // Just to show you it works...
}
else if ( age < 100 ) { // If the age is less than 100
cout<<"You are pretty young!\n"; // Just to show you it works...
}
else {
cout<<"You are really old\n"; // Executed if no other statement is
}
[code=auto:0]
  • 0

#5
lilhawk2892

lilhawk2892

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 128 posts
ok so a lower number always goes first
  • 0

#6
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
if ( age < 100 ) { // If the age is less than 100
cout<<"You are pretty young!\n"; // Just to show you it works...
}

no.

the code above takes care of any age < 100 and everything that follows is not executed.

when you have
if{}
else if{}
else if{}
else if{}

as soon as ANY if or else if is satisfied, none of the other else if statements are executed.

make sense???????
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP