-
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
-
#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.
-
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;
}
-
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
-
>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);