Jump to content
  • Advertisement
Sign in to follow this  
Carbon101

Why aren't strings allowed in structures?

This topic is 2169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why aren't strings allowed in a structure. The only way a string is permitted in a structure is if this notation is used == std:: string random_var:

struct example
{
std::string random_var;
};

int main()
{

cout << "Enter name:" << endl;
cin >> company.random_var;
}

however the compiler doesn't allow user input for this object . Why is that?

I hope this is the right section for this question.

Share this post


Link to post
Share on other sites
Advertisement
What is company? You don't declare it anywhere.

Try adding:
example company;

Above your cout statement.

Works for me:
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::string;

struct testExample
{
string random_string;
};

int main()
{
testExample company;
cout << "Enter the name: ";
cin >> company.random_string;
cout << company.random_string;
return 0;
}
Edited by Dragonsoulj

Share this post


Link to post
Share on other sites
Sorry my mistake: that was a random example I made up. But my current program doesn't allow strings in my structure it says its undefined


#include <iostream>
#include <String>

using namespace std;

struct Pizza
{
String company_name;
int diameter;
double weight;

};

int main()
{
Pizza costumer;

cout << "Which Pizza compnay do you order from?" << endl;
cin >> costumer.company_name;

cout << " " << endl;

cout << "You order from: " << costumer.company_name << endl;

cout << " " << endl;

cout << "What is your desired size of Pizza (Diameter)?" << endl;
cin >> costumer.diameter;

cout << " " << endl;

cout << "Your favorite size, in terms of diameter is " << costumer.diameter << " inches" << endl;

cout << " " << endl;

cout << "Normally what is the weight of your pizza? " << endl;
cin >> costumer.weight;

cout << " " << endl;

cout << "Your pizza weight is normally " << costumer.weight << endl;


system("Pause");
return 0;

}

Share this post


Link to post
Share on other sites
Try using a lowercase "string". Capital "String" gives an error.

As for the errors I get these:
test.cpp:8: error: `String' does not name a type
test.cpp: In function `int main()':
test.cpp:19: error: 'struct Pizza' has no member named 'company_name'
test.cpp:23: error: 'struct Pizza' has no member named 'company_name'

I'm assuming you were referring to bottom two as the issue. The top one is the one to look out for showing something is amiss in the struct code. Top to bottom is the way I usually have to fix errors because the top ones tend to create the ones further down. Edited by Dragonsoulj

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!