Thread: Help with varable type

  1. #1
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77

    Help with varable type

    I'm using Devc++ to make my programs. When I try using the varable type string it won't reconize it. Is string not a varable type anymore or is there a bug? And if it isn't a varable type then ow do I get input setentases(sorry for my bad spelly :-\) from the user?



    ~StormTrooper

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    #include <string>

    Use the std namespace.

    EDIT:
    Note that string is a type defined in the standard library. It is not a language type, but a user-defined type.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    string is a class defined in the standard library. First make sure that the <string> header is included and that you've taken the std namespace into account (string resides in std::). If all of this doesn't work, your version of Dev-C++ is too old, upgrade. Here's a working program that uses std::string's that you can use to test with:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string name;
    
        std::cout<<"Enter your name: ";
        std::getline(std::cin, name);
        std::cout<<"Hello, "<< name <<'!'<<std::endl;
    }
    My best code is written with the delete key.

  4. #4
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    okay what I'm Trying to do is the user inputs an item name. It stores the name. It loops for a product name till he/she inputs 'done'. So how would I store the value he/she inputed?


    Thanks


    ~StormTrooper

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So how would I store the value he/she inputed?
    Theoretically, you would use an array or vector of string objects:
    Code:
    #include <string>
    #include <vector>
    
    std::vector<std::string> products;
    std::string name;
    
    while (std::getline(std::cin, name) && name != "done")
      products.push_back(name);
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM