Thread: If statements

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    Angry If statements

    If statements.

    Hi i am an absolute nebie when it comes to C++, i still cant figure out how to say, if "this" then print "this" to the screen.
    whith all the turtorials i ahve read, it says:
    if (true)
    {
    whatever
    }
    i dont get that, and also on some it says:
    #include <iostream.h>
    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
    if(age<100) //If the age is less than 100
    {
    cout<<"You are pretty young!"; //Just to show it works
    }
    else if(age==100) //I use else just to show an example
    {
    cout<<"You are old"; //Just to show you it works...
    }
    else if(age>100)
    {
    cout<<"You are really old"; //Proof that it works for any condition
    }
    return 0;
    }


    how do you say this:

    #include <iostream.h>
    int main()
    {
    int i;
    cout<<"state your intensions: ";
    cin>>i;
    if(i = "bad")
    {
    cout<<" you may not proceed ";
    }
    else if(i ="good")
    {
    cout<<" you may proceed";
    }
    else if(i = "indeclared")
    {
    cout<<"Why not?";
    }
    return 0;
    }

    can i please have some one debug this!!!
    and i use Bloodshed Dev C++ compiler.

  2. #2
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86
    Aloa he,

    1st: You declared i as an int. An int can never have a string value like "bad" but only whole-numeric values. So if you want the user to input his intention as "good", "bad", "dont know" or whatever you'll have to use a string.

    2nd: When you want to compare the input string to any value like in your case "good" "bad", etc. you cannot use the = operator, which would only compare the adress of both. Use strcmp () for this.

    Give it a try...
    bye
    cody
    #include "reallife.h"

  3. #3
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    226

    Talking

    Hi,
    If you don't ever know what's the different b/w an int and a char*, then you're really a newbie. Better, try a good book or a tutorial....

    My favourite first C book is :

    Al-Stevens Teaches C

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    C++ is not necesarily a good starting point. Master C first, at least moderately, and then pick up C++ after that! It's as if you were to try to learn algebra and so took up a course in differential equations! The Ox comes before the Cart they say, and this is especially true of C++! Not many C++ books will teach you the basis of C, which it is built upon, so learn C first!

    By the way, any book on C will show you that if you, say set a flag to 1, such that good_val == 1, then reset it to zero or even decrement it you could do: if(!good_val)// process erroneous data. Alternately, reset a bad_val to 0 during some qualifying statement and so: if(bad_val) would return false and thus serve to fork to the piece of code that handles "good" execution. The possibilities are limitless!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    12
    i totally agree, trust me.....
    i tried to learn C++ before C, it didnt work to well.
    goto
    www.google.com and search for free C Books or some free C Tutorials, then practice
    THE Dark_Knight_506

  6. #6
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    Thumbs up Teach Yourself C++ for Linux in 21 Days.

    I think that is the boook u are using, are u?

    u should try the tutotial at:

    http://www.cprogramming.com/tutorial or tutorials

    and why are you assigning 'i' to bad, good~~ anywhy!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  4. Class Function Members and If Statements
    By Team Shadow V2 in forum C++ Programming
    Replies: 10
    Last Post: 02-03-2005, 03:00 PM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM