Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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
  • 876 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
  • 876 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
  • 876 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