assigning a value to a string

I am trying to assign values to strings. For instance, I want to say
If (apple value > pear value){
cout <<apple;
}
1
2
string apple;
string pear;
What sort of value are you trying to assign? Can you give us a clearer, more complete and precise example of what it is you're trying to do?
1
2
3
4
5
6
7
8
9
10
11
string apple = "Apple";
string pear = "Pear";

if (apple > pear)
{
	cout << apple;
}
else
{
	cout << pear;
}


This program prints "Pear" because the ASCII code for 'P' is greater than the ASCII code for 'A'.
Is this what you want?
There are multiple ways to assign data:
1
2
3
4
string apple = "Hello"; // initialization
apple = "Dolly"; // assignment
pear = apple; // copy assignment
std::cin >> apple; // from input stream 



A string is text. When is one word less than an another word?
Sorting words to lexicographical order uses the 'less than'.
String has relational operators. Hence this is valid:
1
2
3
if ( pear < apple ) {
  std::cout << apple;
}


However, you do mention value as in a number?
In other words you have a word and you want to associate a number with it?

You could define and use a struct:
1
2
3
4
5
6
7
8
9
10
11
struct Data {
  std::string name {};
  int count {};
};


Data apple { "Dolly", 5 };
Data pear;
if ( pear.count < apple.count ) {
  std::cout << apple.name;
}
thank you so much ! struct is exactly what I need
Topic archived. No new replies allowed.