Thread: string manipulation

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20

    string manipulation

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	string productID;
    	char reply = 'y';
    	
    	
    	do
    	{
    		cout << "Enter The Product ID: ";
    		cin >> productID;
    
    		if (productID.length()==5)
    
    
    	
    
    	
    
    	cout << "Would You Like To Enter Another Product ID [Y/N] ? ";
    	cin >> reply;
    
    	}while (reply == 'y' || reply == 'Y');
    
    	return 0;
    
    }
    the programs needs to

    1. allow user to enter a product id,
    2.the product id needs to be only 5 digits long
    3.the first 2 numbers of the product id need to be 14 or 15
    4. the price for product ids beginning with 14 is 10$
    the price for product ids beginning with 15 is 15$
    5.all other product ids should display a " invalid Id " message

    how can i code this using string manipulation?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Assuming you are asking about how to compare the first two characters of the string, you can use the compare member function (e.g the fourth overload would do nicely).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20
    ok but how do i write it?
    and display the price and make sure the user enters 5 digits and if not display invalid id and makes sure it knows 14 and 15 are the begginers

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    if (productID.length()==5)

    Don't use magic numbers. Even if the number of digits is fixed you should use a constant or a #define to specify MAX_DIGITS or something like that. In the real world you will often be faced with scenarios where your customer suddenly changes one of his specifications : "Oh you know what, it should actually be 6 digits not 5" and then you find yourself wasting your company's money on editing every single value of 5 in your loop conditions throughout a long program.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    and display the price and make sure the user enters 5 digits and if not display invalid id and makes sure it knows 14 and 15 are the begginers
    You mostly use ifs and elses to direct the flow of the program. This is not string manipulation.

    You display price with cout << price, you check the length of the string as you do, you display invalid ids with cout and you test the first two characters with, say, aforementioned compare method.

    And you decide what to do by testing a condition and using it with the if statement. You have good start there, why don't you go and fill in the missing part (what happens if the length is 5).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

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, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:41 AM