help with C++ode(n00b question)

This is a discussion on help with C++ode(n00b question) within the C++ Programming forums, part of the General Programming Boards category; ok, i am new at C++ and i am not sure why my code wont compile. now i probebly wrote ...

  1. #1
    Registered User staticx57's Avatar
    Join Date
    Oct 2004
    Posts
    4

    help with C++ode(n00b question)

    ok, i am new at C++ and i am not sure why my code wont compile. now i probebly wrote it wrong, and i have tried many times to correct it, however i dont know what the problem is. yea, it is a simple program.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
        
        using std::cin;
        using std::cout;
    
        int number1
        int number2
        cout << "enter the first number" ;
        cin >> number1
        
        cout << "enter the second number" ;
        cin >> number2
        
        if (number1 > number2)
        cout << "number 1 is bigger";
        
        if (number1 < number2)
        cout << "number 2 is bigger" ;
        
        if (number1 == number2)
        cout << "they are the same";	
      return 0;
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    iostream.h -> iostream
    The latter is newer (standard) and has namespaces. The former is old and does not.
    stdlib.h -> cstdlib
    This also has the namespaces. You don't appear to need it, though.

    In the future, please post compiler error messages to help us out.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User staticx57's Avatar
    Join Date
    Oct 2004
    Posts
    4
    the compiler i was using required the .h

    compiler i am using. http://www.digitalmars.com/

    COMPILER ERRORS.

    Code:
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.
    
    C:\Documents and Settings\staticx57>c:\dm\bin\dmc.exe \dm\bin\numbcomb
        using std::cin;
                ^
    \dm\bin\numbcomb.cpp(7) : Error: undefined identifier 'std'
        using std::cout;
                ^
    \dm\bin\numbcomb.cpp(8) : Error: undefined identifier 'std'
        int number2
          ^
    \dm\bin\numbcomb.cpp(11) : Error: '=', ';' or ',' expected
        cout << "enter the second number" ;
           ^
    \dm\bin\numbcomb.cpp(15) : Error: ';' expected following declaration of struct m
    ember
        if (number1 > number2)
         ^
    \dm\bin\numbcomb.cpp(18) : Error: undefined identifier 'number2'
    Fatal error: too many errors
    --- errorlevel 1
    Last edited by staticx57; 10-08-2004 at 06:55 PM.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    What compiler are you using? The errors seem to be what I pointed out (with old headers not having the namespace).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User staticx57's Avatar
    Join Date
    Oct 2004
    Posts
    4
    sorry for not saying what compiler i am using. i am using the free one from digital mars. http://www.digitalmars.com/download/freecompiler.html

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    i see at least four instances where you forgot your semicolon ;

    also, you should declare your 'using' directives right after your preprocessor directives (not inside the main() function)
    Last edited by The Brain; 10-08-2004 at 08:35 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    Registered User staticx57's Avatar
    Join Date
    Oct 2004
    Posts
    4
    thanks alot.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The placement of 'using ...' in this example doesn't matter. What you have done, though, is give those symbols function scope only, which is probably not what you want to do in this case, so listen to The Brain, but it is legal, and there are valid reasons why you might want to do that at times.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    you are right... what was i thinking
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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