String problems

This is a discussion on String problems within the C++ Programming forums, part of the General Programming Boards category; Hi everybody, am having problems with this program. Can anyone help me? Code: #include <string> #include <iostream> using namespace std; ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    18

    String problems

    Hi everybody, am having problems with this program. Can anyone help me?

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    class GroceryItem
    {
    private:
    	string itemStockNumber;
    	float price;
    	int quantityInStock;
    	float totalValue;
    public:
    	string setItemStockNumber();
    	float setPrice();
    	float setQuantityInStock();
    	float setTotalValue(float price, int quantity);
    	void displayGrocery();
    };
    void main()
    { 
    	 GroceryItem Grocery;
    
             Grocery.setItemStockNumber();
    	 Grocery.setPrice();
    	 Grocery.setQuantityInStock();
    	 Grocery.displayGrocery();
    	 system ("pause");
    }
    string GroceryItem::setItemStockNumber()
    {
    	int x = 0;
    
    	cout << "Please enter the item's stock number. " << endl;
    	cout << "The item's stock number must be a four digit number: ";
            cin    >> itemStockNumber;
    
    while(x < 1)
     {
      if(itemStockNumber == itemStockNumber.size(4))
        ++x;
      else
        cout << "The item's stock number must be a four digit number: ";
      }
    return itemStockNumber;
    }
    
    float GroceryItem::setPrice()
    {
    	cout << "Please enter the price of item # ";
    	cout << ": ";
    	cin    >> price;
    	return price;
    }
    float GroceryItem::setQuantityInStock()
    {
    	cout << "Please enter the quantity of item #";
    	cout << ": ";
    	cin    >> quantityInStock;
    	return quantityInStock;
            setTotalValue(price, quantityInStock);
    }
    float GroceryItem::setTotalValue(float price, int quantity)
    {
    	totalValue = price * quantity;
    	return totalValue;
    }
    void GroceryItem::displayGrocery()
    {
    	cout << "The item #" << itemStockNumber << "'s price is $" << price;
    	cout << " and has " << quantityInStock << " items in stock." << endl;
    	cout << "The total value of the items is $" << totalValue;
    }

    Thanks!
    Last edited by Conspiracy; 09-24-2007 at 04:03 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    *sigh* So are we supposed to read your mind or are you going to tell us the problems you're having?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    oh sorry

    if (itemStockNumber == itemStockNumber.length(4))

    Thats my problem, you have to put 4 digits, if you dont put 4 digits the program has to keep asking for it until you enter 4 numbers, but I dont know how to do it.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Meebie something like this:
    Code:
    string GroceryItem::setItemStockNumber()
    {
      cout << "Please enter the item's stock number: ";
      
      while ( cin >> itemStockNumber && itemStockNumber.size() != 4 ) {
        cout << "The item's stock number must be a four digit number: ";
      }
    
    #ifdef YOU_ARE_ME
      if ( !cin ) {
        // Handle stream errors
      }
    #endif
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    nope, it didint worked. I also tried this:
    Code:
    while(x < 1)
     {
      if(itemStockNumber == itemStockNumber.size(4))
    	  ++x;
      else
        cout << "The item's stock number must be a four digit number: ";
      }
    but nothing happens.
    Thanks anyway

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> nope, it didint worked.
    Post the entire code from the function when you tried her code. Tell us why you think it didn't work.

    Your attempt is not really close. The size and length functions don't take arguments, so passing 4 makes no sense. If you wanted to check the size of a string to see if it is 4 you would use itemStockNumber.size() == 4 or itemStockNumber.size() != 4. Even if it compiled, you have a loop but the code to read in the value is outside the loop so the user can only try once.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    Thanks for the advise Daved,
    Thanks to you the program runs but theres a problem.
    Heres the changed program:

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    class GroceryItem
    {
    private:
    	string itemStockNumber;
    	float price;
    	int quantityInStock;
    	float totalValue;
    public:
    	string setItemStockNumber();
    	float setPrice();
    	float setQuantityInStock();
    	float setTotalValue(float price, int quantity);
    	void displayGrocery();
    };
    void main()
    { 
    	 GroceryItem Grocery;
    
             Grocery.setItemStockNumber();
    	 Grocery.setPrice();
    	 Grocery.setQuantityInStock();
    	 Grocery.displayGrocery();
    	 system ("pause");
    }
    string GroceryItem::setItemStockNumber()
    {
    	int x = 0;
    	
    	cout << "Please enter the item's stock number. " << endl;
    	cout << "The item's stock number must be a four digit number: "; 
    	cin    >> itemStockNumber;
    	
    	while(x < 1)
    	{
    		if(itemStockNumber.size() == 4)
    			++x;
    		else
    		{
    			cout << "The item's stock number must be a four digit number: "; 
    			cin  >> itemStockNumber;
    		}
    	}
    return itemStockNumber;
    }
    
    float GroceryItem::setPrice()
    {
    	cout << "Please enter the price of item # ";
    	cout << ": ";
    	cin  >> price;
    	return price;
    }
    float GroceryItem::setQuantityInStock()
    {
    	cout << "Please enter the quantity of item #";
    	cout << ": ";
    	cin  >> quantityInStock;
    	return quantityInStock;
            setTotalValue(price, quantityInStock);
    }
    float GroceryItem::setTotalValue(float price, int quantity)
    {
    	totalValue = price * quantity;
    	return totalValue;
    }
    void GroceryItem::displayGrocery()
    {
    	cout << "Item #" << itemStockNumber << "'s price is $" << price;
    	cout << " and has " << quantityInStock << " items in stock." << endl;
    	cout << "The total value of the items is $" << totalValue << endl;
    }
    And this is how the program runs:

    Please enter the item's stock number.
    The item's stock number must be a four digit number: 78
    The item's stock number must be a four digit number: 1
    The item's stock number must be a four digit number: 222
    The item's stock number must be a four digit number: 5812
    Please enter the price of item # : 45.85
    Please enter the quantity of item #: 15
    Item #5812's price is $45.85 and has 15 items in stock.
    The total value of the items is $-1.07374e+008
    Press any key to continue . . .

    The problem is that it doesn't want to display the result of 45.85 * 15

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >	return quantityInStock;
    >	setTotalValue(price, quantityInStock);
    Since you have a return statement before call to function setTotalValue, the function is never called. A simple solution is place the call to setTotalValue before returning:
    Code:
    	setTotalValue(price, quantityInStock);
    	return quantityInStock;

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Does your compiler give you a warning about the problem swoopy pointed out? Make sure to read and understand all the compiler warnings since they usually point out problems in your code.

    Also,
    Code:
    	while(x < 1)
    	{
    		if(itemStockNumber.size() == 4)
    			++x;
    		else
    That is a convoluted way of making the loop. All you have to do is loop while itemStockNumber.size() != 4 (which is what Prelude's example did).

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    Quote Originally Posted by Daved View Post
    Does your compiler give you a warning about the problem swoopy pointed out? Make sure to read and understand all the compiler warnings since they usually point out problems in your code.

    Also,
    Code:
    	while(x < 1)
    	{
    		if(itemStockNumber.size() == 4)
    			++x;
    		else
    That is a convoluted way of making the loop. All you have to do is loop while itemStockNumber.size() != 4 (which is what Prelude's example did).
    No, that part is fine, the only warning the compiler gives is:

    Warning 1 warning C4244: 'return' : conversion from 'int' to 'float', possible loss of data h:\documents\c++\oop\program_5\program_5\program_5 .cpp 64


    but I changed this:
    Code:
        setTotalValue(price, quantityInStock);
        return quantityInStock;
    Like you told me and it works, here is how it looks:

    Code:
    Please enter the item's stock number.
    The item's stock number must be a four digit number: 7
    The item's stock number must be a four digit number: 7
    The item's stock number must be a four digit number: 545
    The item's stock number must be a four digit number: 22
    The item's stock number must be a four digit number: 7896
    Please enter the price of item # 7896: 15.50
    Please enter the quantity of item #7896: 65
    
    Item #7896's price is $15.5 and has 65 items in stock.
    The total value of the items is $1007.5
    
    Press any key to continue . . .
    Thanks EVERYONE for your help!!

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Thanks EVERYONE for your help!
    Glad it's working for you.

    >> No, that part is fine
    The code I posted doesn't give a warning, I'm just giving you a suggestion about how to write better code if you're interested in doing more than getting things to work for the moment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  4. io stream and string class problems
    By quizteamer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 01:07 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21