-3

I'm trying to limit the string item1, item2, and item3 to ten letters and if they are longer than ten letters I want the program to produce a statement and terminate the program. I also want to do this with price1, price2, price3, but I haven't tried this yet because I'm stuck on the first part. I assume it should be similar to limiting the first items. How do I do this with a conditional statement? Here's what I have so far:

#include <iostream> 
#include <iomanip>

using namespace std;

int main () {

    string item1, item2, item3;
    int ten;
    ten=10;
    item1<"10";
    item2<"10";
    item3<"10";
    cout << "Enter names of 3 one-word items to purchase (each name must be less than 10 letters long): " <<endl;
    cin >> item1 >> item2 >> item3;

    if (item>=10)
    {
        cout<< "This item is more than 10 letters long" <<endl;
        return 0;
    }

    float price1, price2, price3, thousand;
    thousand=1000;
    cout << "You have purchased 3 items. Enter their prices in US Dollars (must be less than $1,000): " <<endl;
    cin >> price1 >> price2 >> price3;

}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
QM Adams
  • 73
  • 7
  • 5
    `item1<"10";`, `item2<"10";` and `item3<"10";` have no effect. What are they supposed to do? – Jabberwocky Sep 07 '18 at 14:30
  • 3
    `if (item>=10)` Where is item defined? – dustytrash Sep 07 '18 at 14:31
  • 3
    Consider learning C++ from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of whatever you are using to learn it currently. – Algirdas Preidžius Sep 07 '18 at 14:32
  • 1
    I just started, give me a break. I got rid of item1<"10";, item2<"10"; and item3<"10"; – QM Adams Sep 07 '18 at 14:36
  • Unrelated: When you think you want `float price1, price2, price3;`, what you really want is an array `float price[3];`. – user4581301 Sep 07 '18 at 14:37
  • @user4581301 a C array in C++?! – user1810087 Sep 07 '18 at 14:38
  • Usually you do not `return 0;` when input is incorrect, non zero value considered as program terminated with error code – Slava Sep 07 '18 at 14:41
  • 1
    @QMAdams "_I just started, give me a break._" The book, you are learning from, should have explained the correct syntax for doing this. If you are not using one: you cannot learn C++ by trial and error. – Algirdas Preidžius Sep 07 '18 at 14:41
  • C arrays have their place. For an array of known size that's not leaving the current function, they are light weight, easy for even old guys like me to read and out of your way quickly. – user4581301 Sep 07 '18 at 14:45
  • @user4581301 trwtf is using floats for amounts of money. – Swordfish Sep 07 '18 at 14:57
  • @Swordfish that's good to keep in mind for working code (Anyone wondering why, read [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken)), but right now teaching the asker how to write a fixed point parser is stretching things a bit off topic. – user4581301 Sep 07 '18 at 16:14

2 Answers2

2

std::string::size() is what you are looking for:

if (item1.size() > 10)
{
    std::cout << "This item is more than 10 letters long" << std::endl;
    return 0;
}
Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
1

You probably want this:

#include <iostream> 
#include <string> 

using namespace std;

int main() {
  string item1, item2, item3;

  cout << "Enter names of 3 one-word items to purchase (each name must be less than 10 letters long): " << endl;
  cin >> item1 >> item2 >> item3;

  if (item1.size() >= 10 || item2.size() >= 10 || item3.size() >= 10)
  {
    cout << "One of the items is more than 10 letters long" << endl;
    return 0;
  }
}

or maybe this:

#include <iostream> 
#include <string> 

using namespace std;

int main() {
  string item1, item2, item3;

  cout << "Enter names of 3 one-word items to purchase (each name must be less than 10 letters long): " << endl;
  cin >> item1;
  if (item1.size() >= 10)
  {
    cout << "This item is more than 10 letters long" << endl;
    return 0;
  }
  cin >> item2;
  if (item2.size() >= 10)
  {
    cout << "This item is more than 10 letters long" << endl;
    return 0;
  }
  cin >> item3;
  if (item3.size() >= 10)
  {
    cout << "This item is more than 10 letters long" << endl;
    return 0;
  }
}

but this is very awkward.

This solution uses an array and it's probably what you actually want:

#include <iostream> 
#include <string> 

using namespace std;

const int nbofitems = 3;   // modify here for changing the number of items

int main() {
  string items[nbofitems];

  cout << "Enter names of 3 one-word items to purchase (each name must be less than 10 letters long): " << endl;

  for (int i = 0; i < nbofitems; i++)
  {
    string item;
    cin >> item;
    if (item.size() >= 10)
    {
      cout << "This item is more than 10 letters long" << endl;
      return 0;
    }

    items[i] = item;
  }

  cout << "Items entered:\n";

  for (int i = 0; i < nbofitems; i++)
  {
    cout << "item " << i << ": " << items[i] << "\n";
  }
}

However there is still much room for improvement, for example using std::vector instead of a raw array.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115