Thread: Compile error

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    11

    Question Compile error

    When i compile my program is should compile but instead it comes up with a bunch of errors. I am using Dev-C++ 4.
    here is my code:
    Code:
    #include <ctime>
    #include <time.h>
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    int main()
    {
        string allcards[52] = {"2c", "2d", "2h", "2s", "3c", "3d", "3h", "3s", 
                               "4c", "4d", "4h", "4s", "5c", "5d", "5h", "5s", 
                               "6c", "6d", "6h", "6s", "7c", "7d", "7h", "7s", 
                               "8c", "8d", "8h", "8s", "9c", "9d", "9h", "9s", 
                               "10c", "10d", "10h", "10s", "Jc", "Jd", "Jh", "Js", 
                               "Qc", "Qd", "Qh", "Qs", "Kc", "Kd", "Kh", "Ks", 
                               "Ac", "Ad", "Ah", "As"};
        srand(time(NULL)); 
        int cardnum = rand() % 52;
        string card = allcards[cardnum]; 
        cout << card;
        system("PAUSE");
        return 0;
    }
    And here are the errors:

    c:\my documents\trav's folder\c++ programs\cardss.cpp: In function `int main()':
    c:\my documents\trav's folder\c++ programs\cardss.cpp:8: `string' undeclared (first use this function)
    c:\my documents\trav's folder\c++ programs\cardss.cpp:8: (Each undeclared identifier is reported only once
    c:\my documents\trav's folder\c++ programs\cardss.cpp:8: for each function it appears in.)
    c:\my documents\trav's folder\c++ programs\cardss.cpp:8: parse error before `['
    c:\my documents\trav's folder\c++ programs\cardss.cpp: At top level:
    c:\my documents\trav's folder\c++ programs\cardss.cpp:15: ANSI C++ forbids declaration `srand' with no type
    c:\my documents\trav's folder\c++ programs\cardss.cpp:15: `int srand' redeclared as different kind of symbol
    C:\DEV-C_~1\INCLUDE\stdlib.h:272: previous declaration of `void srand(unsigned int)'
    c:\my documents\trav's folder\c++ programs\cardss.cpp:17: syntax error before `='
    c:\my documents\trav's folder\c++ programs\cardss.cpp:18: syntax error before `<'
    c:\my documents\trav's folder\c++ programs\cardss.cpp:19: ANSI C++ forbids declaration `system' with no type
    c:\my documents\trav's folder\c++ programs\cardss.cpp:19: `int system' redeclared as different kind of symbol
    C:\DEV-C_~1\INCLUDE\stdlib.h:283: previous declaration of `int system(const char *)'
    c:\my documents\trav's folder\c++ programs\cardss.cpp:19: initialization to `int' from `const char *' lacks a cast
    c:\my documents\trav's folder\c++ programs\cardss.cpp:20: parse error before `return'

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    #include <string>

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    11
    omg thank you i knew it would be something stupid

  4. #4
    Registered User justdoit22's Avatar
    Join Date
    Dec 2003
    Posts
    18
    replace this line
    cout << card;

    with this line

    cout<<card.c_str();

    i hope it will work fine now

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Originally posted by justdoit22
    replace this line
    cout << card;

    with this line

    cout<<card.c_str();

    i hope it will work fine now
    This really isn't necessary, since strings have an overloaded operator<< for iostreams. In some implementations, if you forget the #include<string>, the code with strings will compile and the .c_str() seems necessary. But the correct way to fix that is to include the string header and not use the .c_str().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM