Thread: Compiler error or human error?

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    67

    Compiler error or human error?

    I am using Dev-C++ version 4.0 and I have some errors when I try to compiler this:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    int main()
    {
          int testcommand;
          cout<<"Welcome Please Type Hello";
          cin>>testcommand;
          if (testcommand=="Hello");
          {
          cout<<"I guess it works";
          }
          system("PAUSE");
          return 0;
    }
    But then I tried this:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    int main()
    {
          int testcommand;
          cout<<"Welcome Please Type Hello";
          cin>>testcommand;
          if (testcommand==Hello);
          {
          cout<<"I guess it works";
          }
          system("PAUSE");
          return 0;
    }
    Could any one give me some tips?
    This is my first attemp at trying to make a game and I need to know about this before I start. Thanks in advance


    Are any of you vivid gamers? Then go here
    www.gamefaqs.com

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    67
    Sorry I meant to say that the second code didn't work either
    [edit]
    Stupid o key keeps sticking....
    [/edit]

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    You are comparing an int to a string. You cannot do this. Instead, make testnumber a char array.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    67
    Could you refresh me on how to do that?
    Thanks in advance :-)

  5. #5
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    For starters since you're trying to use the C++ iostream get rid of the .h's in your STL header includes. ie:

    Code:
    //this
    #include <iostream.h>
    
    //should be this
    #include <iostream>
    and put the following under your last include file listing:

    Code:
    using namespace std;
    And to create a char array use:

    Code:
    char testcommand[10];
    //or
    char *testcommand;
    But since you're using the STL for your input you may want to use strings for your text data, that way you can use your "==" check, which won't with a C char array (you'd have to use strcmp).

    To do this try:

    Code:
    #include <iostream> //for cout/cin
    #include <string> //for string
    #include <stdlib.h> //for system()
    using namespace std; 
    
    int main()
    {
          string testcommand;
          cout<<"Welcome Please Type Hello";
          cin>>testcommand;
          if (testcommand=="Hello");
          {
               cout<<"I guess it works"<<endl;
          }
          else
          {
              cout<<"You didn't enter hello."<<endl;
          }
          system("PAUSE");
          return 0;
    }
    Last edited by jdinger; 09-05-2002 at 04:40 PM.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Yep...but ditch the semi-colon after
    Code:
    if (testcommand=="Hello");
    (You'll avoid that "misplaced 'else' " error).

    (Jdinger works long hours and can be excused for this since the rest of his post is excellent...and I'm not just sucking up here. )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    67
    Thanks, I was wondering why I was getting that error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM